]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
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 | ||
108 | WXDWORD 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 |
128 | void 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 |
143 | wxSize 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 |
156 | wxCalendarHitTestResult |
157 | wxCalendarCtrl::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 ) | |
154014d6 | 180 | date->SetFromMSWSysTime(hti.st); |
ee22a3a2 VZ |
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 | ||
206 | bool wxCalendarCtrl::SetDate(const wxDateTime& dt) | |
207 | { | |
208 | wxCHECK_MSG( dt.IsValid(), false, "invalid date" ); | |
209 | ||
210 | SYSTEMTIME st; | |
154014d6 | 211 | dt.GetAsMSWSysTime(&st); |
51317496 VZ |
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 | ||
224 | wxDateTime 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 | 234 | |
154014d6 | 235 | wxDateTime dt(st); |
a4fcd589 VZ |
236 | |
237 | wxASSERT_MSG( dt == m_date, "mismatch between data and control" ); | |
238 | #endif // __WXDEBUG__ | |
239 | ||
240 | return m_date; | |
51317496 VZ |
241 | } |
242 | ||
243 | bool wxCalendarCtrl::SetDateRange(const wxDateTime& dt1, const wxDateTime& dt2) | |
244 | { | |
245 | SYSTEMTIME st[2]; | |
246 | ||
247 | DWORD flags = 0; | |
248 | if ( dt1.IsValid() ) | |
249 | { | |
154014d6 | 250 | dt1.GetAsMSWSysTime(st + 0); |
51317496 VZ |
251 | flags |= GDTR_MIN; |
252 | } | |
253 | ||
254 | if ( dt2.IsValid() ) | |
255 | { | |
154014d6 | 256 | dt2.GetAsMSWSysTime(st + 1); |
51317496 VZ |
257 | flags |= GDTR_MAX; |
258 | } | |
259 | ||
260 | if ( !MonthCal_SetRange(GetHwnd(), flags, st) ) | |
261 | { | |
262 | wxLogDebug(_T("MonthCal_SetRange() failed")); | |
263 | } | |
264 | ||
265 | return flags != 0; | |
266 | } | |
267 | ||
268 | bool wxCalendarCtrl::GetDateRange(wxDateTime *dt1, wxDateTime *dt2) const | |
269 | { | |
270 | SYSTEMTIME st[2]; | |
271 | ||
272 | DWORD flags = MonthCal_GetRange(GetHwnd(), st); | |
273 | if ( dt1 ) | |
274 | { | |
275 | if ( flags & GDTR_MIN ) | |
154014d6 | 276 | dt1->SetFromMSWSysTime(st[0]); |
51317496 VZ |
277 | else |
278 | *dt1 = wxDefaultDateTime; | |
279 | } | |
280 | ||
281 | if ( dt2 ) | |
282 | { | |
283 | if ( flags & GDTR_MAX ) | |
154014d6 | 284 | dt2->SetFromMSWSysTime(st[1]); |
51317496 VZ |
285 | else |
286 | *dt2 = wxDefaultDateTime; | |
287 | } | |
288 | ||
289 | return flags != 0; | |
290 | } | |
291 | ||
292 | // ---------------------------------------------------------------------------- | |
293 | // other wxCalendarCtrl operations | |
294 | // ---------------------------------------------------------------------------- | |
295 | ||
296 | bool wxCalendarCtrl::EnableMonthChange(bool enable) | |
297 | { | |
7ec5c42e VZ |
298 | if ( !wxCalendarCtrlBase::EnableMonthChange(enable) ) |
299 | return false; | |
300 | ||
301 | wxDateTime dtStart, dtEnd; | |
302 | if ( !enable ) | |
303 | { | |
304 | dtStart = GetDate(); | |
305 | dtStart.SetDay(1); | |
51317496 | 306 | |
7ec5c42e VZ |
307 | dtEnd = dtStart.GetLastMonthDay(); |
308 | } | |
309 | //else: leave them invalid to remove the restriction | |
310 | ||
311 | SetDateRange(dtStart, dtEnd); | |
312 | ||
313 | return true; | |
51317496 VZ |
314 | } |
315 | ||
316 | void wxCalendarCtrl::Mark(size_t day, bool mark) | |
317 | { | |
82c6027b VZ |
318 | wxCHECK_RET( day > 0 && day < 32, "invalid day" ); |
319 | ||
320 | int mask = 1 << (day - 1); | |
321 | if ( mark ) | |
322 | m_marks |= mask; | |
323 | else | |
324 | m_marks &= ~mask; | |
325 | ||
326 | // calling Refresh() here is not enough to change the day appearance | |
327 | UpdateMarks(); | |
328 | } | |
329 | ||
330 | void wxCalendarCtrl::UpdateMarks() | |
331 | { | |
332 | MONTHDAYSTATE states[3]; | |
333 | const int nMonths = MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE, NULL); | |
c912c940 | 334 | wxCHECK_RET( nMonths <= (int)WXSIZEOF(states), "unexpected months range" ); |
82c6027b VZ |
335 | |
336 | for ( int i = 0; i < nMonths; i++ ) | |
337 | states[i] = m_marks; | |
338 | ||
339 | if ( !MonthCal_SetDayState(GetHwnd(), nMonths, states) ) | |
340 | { | |
341 | wxLogLastError(_T("MonthCal_SetDayState")); | |
342 | } | |
51317496 VZ |
343 | } |
344 | ||
db0b0942 VZ |
345 | void wxCalendarCtrl::UpdateFirstDayOfWeek() |
346 | { | |
347 | MonthCal_SetFirstDayOfWeek(GetHwnd(), HasFlag(wxCAL_MONDAY_FIRST) ? 0 : 6); | |
348 | } | |
349 | ||
51317496 VZ |
350 | // ---------------------------------------------------------------------------- |
351 | // wxCalendarCtrl events | |
352 | // ---------------------------------------------------------------------------- | |
353 | ||
354 | bool wxCalendarCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) | |
355 | { | |
356 | NMHDR* hdr = (NMHDR *)lParam; | |
357 | switch ( hdr->code ) | |
358 | { | |
a4fcd589 | 359 | case MCN_SELCHANGE: |
a4fcd589 | 360 | { |
82c6027b VZ |
361 | // we need to update m_date first, before calling the user code |
362 | // which expects GetDate() to return the new date | |
363 | const wxDateTime dateOld = m_date; | |
364 | const NMSELCHANGE * const sch = (NMSELCHANGE *)lParam; | |
154014d6 | 365 | m_date.SetFromMSWSysTime(sch->stSelStart); |
82c6027b VZ |
366 | |
367 | // changing the year or the month results in a second dummy | |
368 | // MCN_SELCHANGE event on this system which doesn't really | |
369 | // change anything -- filter it out | |
370 | if ( m_date != dateOld ) | |
371 | { | |
372 | GenerateAllChangeEvents(dateOld); | |
373 | } | |
374 | } | |
375 | break; | |
a4fcd589 | 376 | |
82c6027b VZ |
377 | case MCN_GETDAYSTATE: |
378 | { | |
379 | const NMDAYSTATE * const ds = (NMDAYSTATE *)lParam; | |
380 | for ( int i = 0; i < ds->cDayState; i++ ) | |
381 | { | |
382 | ds->prgDayState[i] = m_marks; | |
383 | } | |
a4fcd589 | 384 | } |
82c6027b VZ |
385 | break; |
386 | ||
387 | default: | |
388 | return wxCalendarCtrlBase::MSWOnNotify(idCtrl, lParam, result); | |
51317496 VZ |
389 | } |
390 | ||
82c6027b VZ |
391 | *result = 0; |
392 | return true; | |
51317496 VZ |
393 | } |
394 | ||
b3ed7020 VZ |
395 | void wxCalendarCtrl::MSWOnDoubleClick(wxMouseEvent& event) |
396 | { | |
397 | if ( HitTest(event.GetPosition()) == wxCAL_HITTEST_DAY ) | |
398 | { | |
399 | if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED) ) | |
400 | return; // skip event.Skip() below | |
401 | } | |
402 | ||
403 | event.Skip(); | |
404 | } | |
405 | ||
3ccd1b49 VZ |
406 | void wxCalendarCtrl::MSWOnClick(wxMouseEvent& event) |
407 | { | |
408 | // for some reason, the control doesn't get focus on its own when the user | |
409 | // clicks in it | |
410 | SetFocus(); | |
411 | ||
412 | event.Skip(); | |
413 | } | |
414 | ||
51317496 | 415 | #endif // wxUSE_CALENDARCTRL |