]>
Commit | Line | Data |
---|---|---|
2ef31e80 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: generic/calctrl.cpp | |
3 | // Purpose: implementation fo the generic wxCalendarCtrl | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 29.12.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
65571936 | 9 | // Licence: wxWindows licence |
2ef31e80 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
2ef31e80 VZ |
21 | #pragma implementation "calctrl.h" |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
9d9b7755 VZ |
32 | #include "wx/dcclient.h" |
33 | #include "wx/settings.h" | |
34 | #include "wx/brush.h" | |
4f6aed9c | 35 | #include "wx/combobox.h" |
5f89b543 | 36 | #include "wx/listbox.h" |
bc385ba9 | 37 | #include "wx/stattext.h" |
25f430fc | 38 | #include "wx/textctrl.h" |
2ef31e80 VZ |
39 | #endif //WX_PRECOMP |
40 | ||
1e6feb95 | 41 | #if wxUSE_CALENDARCTRL |
2fa7c206 | 42 | |
f0d5e7a2 VZ |
43 | #include "wx/spinctrl.h" |
44 | ||
ab9cf636 VZ |
45 | // if wxDatePickerCtrl code doesn't define the date event, do it here as we |
46 | // need it as well | |
47 | #if !wxUSE_DATEPICKCTRL | |
48 | #define _WX_DEFINE_DATE_EVENTS_ | |
49 | #endif | |
50 | ||
9d9b7755 | 51 | #include "wx/calctrl.h" |
2ef31e80 | 52 | |
882a8f40 VZ |
53 | #define DEBUG_PAINT 0 |
54 | ||
55 | // ---------------------------------------------------------------------------- | |
56 | // private classes | |
57 | // ---------------------------------------------------------------------------- | |
58 | ||
59 | class wxMonthComboBox : public wxComboBox | |
60 | { | |
61 | public: | |
62 | wxMonthComboBox(wxCalendarCtrl *cal); | |
63 | ||
64 | void OnMonthChange(wxCommandEvent& event) { m_cal->OnMonthChange(event); } | |
65 | ||
66 | private: | |
67 | wxCalendarCtrl *m_cal; | |
68 | ||
69 | DECLARE_EVENT_TABLE() | |
22f3361e | 70 | DECLARE_NO_COPY_CLASS(wxMonthComboBox) |
882a8f40 VZ |
71 | }; |
72 | ||
73 | class wxYearSpinCtrl : public wxSpinCtrl | |
74 | { | |
75 | public: | |
76 | wxYearSpinCtrl(wxCalendarCtrl *cal); | |
77 | ||
ca65c044 | 78 | void OnYearTextChange(wxCommandEvent& event) |
605dfd91 JS |
79 | { |
80 | m_cal->SetUserChangedYear(); | |
81 | m_cal->OnYearChange(event); | |
82 | } | |
882a8f40 VZ |
83 | void OnYearChange(wxSpinEvent& event) { m_cal->OnYearChange(event); } |
84 | ||
85 | private: | |
86 | wxCalendarCtrl *m_cal; | |
87 | ||
88 | DECLARE_EVENT_TABLE() | |
22f3361e | 89 | DECLARE_NO_COPY_CLASS(wxYearSpinCtrl) |
882a8f40 VZ |
90 | }; |
91 | ||
2ef31e80 VZ |
92 | // ---------------------------------------------------------------------------- |
93 | // wxWin macros | |
94 | // ---------------------------------------------------------------------------- | |
95 | ||
96 | BEGIN_EVENT_TABLE(wxCalendarCtrl, wxControl) | |
97 | EVT_PAINT(wxCalendarCtrl::OnPaint) | |
98 | ||
9d9b7755 VZ |
99 | EVT_CHAR(wxCalendarCtrl::OnChar) |
100 | ||
2ef31e80 | 101 | EVT_LEFT_DOWN(wxCalendarCtrl::OnClick) |
0185cd09 | 102 | EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick) |
882a8f40 VZ |
103 | END_EVENT_TABLE() |
104 | ||
105 | BEGIN_EVENT_TABLE(wxMonthComboBox, wxComboBox) | |
ca65c044 | 106 | EVT_COMBOBOX(wxID_ANY, wxMonthComboBox::OnMonthChange) |
882a8f40 | 107 | END_EVENT_TABLE() |
9d9b7755 | 108 | |
882a8f40 | 109 | BEGIN_EVENT_TABLE(wxYearSpinCtrl, wxSpinCtrl) |
ca65c044 WS |
110 | EVT_TEXT(wxID_ANY, wxYearSpinCtrl::OnYearTextChange) |
111 | EVT_SPINCTRL(wxID_ANY, wxYearSpinCtrl::OnYearChange) | |
2ef31e80 VZ |
112 | END_EVENT_TABLE() |
113 | ||
b0a877ec | 114 | #if wxUSE_EXTENDED_RTTI |
73c36334 JS |
115 | WX_DEFINE_FLAGS( wxCalendarCtrlStyle ) |
116 | ||
3ff066a4 | 117 | wxBEGIN_FLAGS( wxCalendarCtrlStyle ) |
73c36334 JS |
118 | // new style border flags, we put them first to |
119 | // use them for streaming out | |
3ff066a4 SC |
120 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
121 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
122 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
123 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
124 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
125 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
ca65c044 | 126 | |
73c36334 | 127 | // old style border flags |
3ff066a4 SC |
128 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
129 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
130 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
131 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
132 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
cb0afb26 | 133 | wxFLAGS_MEMBER(wxBORDER) |
73c36334 JS |
134 | |
135 | // standard window styles | |
3ff066a4 SC |
136 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
137 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
138 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
139 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 140 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
141 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
142 | wxFLAGS_MEMBER(wxVSCROLL) | |
143 | wxFLAGS_MEMBER(wxHSCROLL) | |
144 | ||
145 | wxFLAGS_MEMBER(wxCAL_SUNDAY_FIRST) | |
146 | wxFLAGS_MEMBER(wxCAL_MONDAY_FIRST) | |
147 | wxFLAGS_MEMBER(wxCAL_SHOW_HOLIDAYS) | |
148 | wxFLAGS_MEMBER(wxCAL_NO_YEAR_CHANGE) | |
149 | wxFLAGS_MEMBER(wxCAL_NO_MONTH_CHANGE) | |
150 | wxFLAGS_MEMBER(wxCAL_SEQUENTIAL_MONTH_SELECTION) | |
151 | wxFLAGS_MEMBER(wxCAL_SHOW_SURROUNDING_WEEKS) | |
152 | ||
153 | wxEND_FLAGS( wxCalendarCtrlStyle ) | |
73c36334 | 154 | |
b0a877ec SC |
155 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxCalendarCtrl, wxControl,"wx/calctrl.h") |
156 | ||
3ff066a4 SC |
157 | wxBEGIN_PROPERTIES_TABLE(wxCalendarCtrl) |
158 | wxEVENT_RANGE_PROPERTY( Updated , wxEVT_CALENDAR_SEL_CHANGED , wxEVT_CALENDAR_WEEKDAY_CLICKED , wxCalendarEvent ) | |
159 | wxHIDE_PROPERTY( Children ) | |
ca65c044 | 160 | wxPROPERTY( Date,wxDateTime, SetDate , GetDate, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
3ff066a4 SC |
161 | wxPROPERTY_FLAGS( WindowStyle , wxCalendarCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
162 | wxEND_PROPERTIES_TABLE() | |
b0a877ec | 163 | |
3ff066a4 SC |
164 | wxBEGIN_HANDLERS_TABLE(wxCalendarCtrl) |
165 | wxEND_HANDLERS_TABLE() | |
b0a877ec | 166 | |
ca65c044 | 167 | wxCONSTRUCTOR_6( wxCalendarCtrl , wxWindow* , Parent , wxWindowID , Id , wxDateTime , Date , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
b0a877ec | 168 | #else |
2ef31e80 | 169 | IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl, wxControl) |
b0a877ec | 170 | #endif |
feb72429 | 171 | IMPLEMENT_DYNAMIC_CLASS(wxCalendarEvent, wxDateEvent) |
2ef31e80 | 172 | |
2e4df4bf VZ |
173 | // ---------------------------------------------------------------------------- |
174 | // events | |
175 | // ---------------------------------------------------------------------------- | |
176 | ||
177 | DEFINE_EVENT_TYPE(wxEVT_CALENDAR_SEL_CHANGED) | |
178 | DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DAY_CHANGED) | |
179 | DEFINE_EVENT_TYPE(wxEVT_CALENDAR_MONTH_CHANGED) | |
180 | DEFINE_EVENT_TYPE(wxEVT_CALENDAR_YEAR_CHANGED) | |
181 | DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DOUBLECLICKED) | |
182 | DEFINE_EVENT_TYPE(wxEVT_CALENDAR_WEEKDAY_CLICKED) | |
183 | ||
2ef31e80 VZ |
184 | // ============================================================================ |
185 | // implementation | |
186 | // ============================================================================ | |
187 | ||
882a8f40 VZ |
188 | // ---------------------------------------------------------------------------- |
189 | // wxMonthComboBox and wxYearSpinCtrl | |
190 | // ---------------------------------------------------------------------------- | |
191 | ||
192 | wxMonthComboBox::wxMonthComboBox(wxCalendarCtrl *cal) | |
ca65c044 | 193 | : wxComboBox(cal->GetParent(), wxID_ANY, |
882a8f40 VZ |
194 | wxEmptyString, |
195 | wxDefaultPosition, | |
196 | wxDefaultSize, | |
197 | 0, NULL, | |
fdda2df6 | 198 | wxCB_READONLY | wxCLIP_SIBLINGS) |
882a8f40 VZ |
199 | { |
200 | m_cal = cal; | |
201 | ||
202 | wxDateTime::Month m; | |
203 | for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) ) | |
204 | { | |
205 | Append(wxDateTime::GetMonthName(m)); | |
206 | } | |
207 | ||
208 | SetSelection(m_cal->GetDate().GetMonth()); | |
422d0ff0 WS |
209 | SetSize(wxDefaultCoord, |
210 | wxDefaultCoord, | |
211 | wxDefaultCoord, | |
212 | wxDefaultCoord, | |
ca65c044 | 213 | wxSIZE_AUTO_WIDTH|wxSIZE_AUTO_HEIGHT); |
882a8f40 VZ |
214 | } |
215 | ||
216 | wxYearSpinCtrl::wxYearSpinCtrl(wxCalendarCtrl *cal) | |
ca65c044 | 217 | : wxSpinCtrl(cal->GetParent(), wxID_ANY, |
882a8f40 VZ |
218 | cal->GetDate().Format(_T("%Y")), |
219 | wxDefaultPosition, | |
220 | wxDefaultSize, | |
fdda2df6 | 221 | wxSP_ARROW_KEYS | wxCLIP_SIBLINGS, |
882a8f40 | 222 | -4300, 10000, cal->GetDate().GetYear()) |
95bcc40c | 223 | |
882a8f40 VZ |
224 | { |
225 | m_cal = cal; | |
226 | } | |
227 | ||
2ef31e80 VZ |
228 | // ---------------------------------------------------------------------------- |
229 | // wxCalendarCtrl | |
230 | // ---------------------------------------------------------------------------- | |
231 | ||
2b5f62a0 VZ |
232 | wxCalendarCtrl::wxCalendarCtrl(wxWindow *parent, |
233 | wxWindowID id, | |
234 | const wxDateTime& date, | |
235 | const wxPoint& pos, | |
236 | const wxSize& size, | |
237 | long style, | |
238 | const wxString& name) | |
239 | { | |
240 | Init(); | |
ca65c044 | 241 | |
2b5f62a0 VZ |
242 | (void)Create(parent, id, date, pos, size, style, name); |
243 | } | |
244 | ||
2ef31e80 VZ |
245 | void wxCalendarCtrl::Init() |
246 | { | |
247 | m_comboMonth = NULL; | |
248 | m_spinYear = NULL; | |
1882f416 JS |
249 | m_staticYear = NULL; |
250 | m_staticMonth = NULL; | |
2ef31e80 | 251 | |
ca65c044 | 252 | m_userChangedYear = false; |
f0d5e7a2 | 253 | |
2ef31e80 VZ |
254 | m_widthCol = |
255 | m_heightRow = 0; | |
9d9b7755 VZ |
256 | |
257 | wxDateTime::WeekDay wd; | |
258 | for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) ) | |
259 | { | |
260 | m_weekdays[wd] = wxDateTime::GetWeekDayName(wd, wxDateTime::Name_Abbr); | |
261 | } | |
3965571c | 262 | |
4f6aed9c VZ |
263 | for ( size_t n = 0; n < WXSIZEOF(m_attrs); n++ ) |
264 | { | |
265 | m_attrs[n] = NULL; | |
266 | } | |
267 | ||
7516ed26 VZ |
268 | m_colHighlightFg = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); |
269 | m_colHighlightBg = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); | |
4f6aed9c VZ |
270 | |
271 | m_colHolidayFg = *wxRED; | |
272 | // don't set m_colHolidayBg - by default, same as our bg colour | |
273 | ||
274 | m_colHeaderFg = *wxBLUE; | |
275 | m_colHeaderBg = *wxLIGHT_GREY; | |
2ef31e80 VZ |
276 | } |
277 | ||
4965c3d7 VZ |
278 | bool wxCalendarCtrl::Create(wxWindow *parent, |
279 | wxWindowID id, | |
2ef31e80 | 280 | const wxDateTime& date, |
4965c3d7 | 281 | const wxPoint& pos, |
2ef31e80 VZ |
282 | const wxSize& size, |
283 | long style, | |
4965c3d7 | 284 | const wxString& name) |
2ef31e80 | 285 | { |
4965c3d7 | 286 | if ( !wxControl::Create(parent, id, pos, size, |
fdda2df6 VZ |
287 | style | wxCLIP_CHILDREN | wxWANTS_CHARS, |
288 | wxDefaultValidator, name) ) | |
4965c3d7 | 289 | { |
ca65c044 | 290 | return false; |
4965c3d7 VZ |
291 | } |
292 | ||
bc385ba9 VZ |
293 | // needed to get the arrow keys normally used for the dialog navigation |
294 | SetWindowStyle(style | wxWANTS_CHARS); | |
2ef31e80 | 295 | |
882a8f40 | 296 | m_date = date.IsValid() ? date : wxDateTime::Today(); |
9d9b7755 | 297 | |
37df1f33 VZ |
298 | m_lowdate = wxDefaultDateTime; |
299 | m_highdate = wxDefaultDateTime; | |
bc385ba9 | 300 | |
bb61a983 | 301 | if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) ) |
37df1f33 VZ |
302 | { |
303 | m_spinYear = new wxYearSpinCtrl(this); | |
ca65c044 | 304 | m_staticYear = new wxStaticText(GetParent(), wxID_ANY, m_date.Format(_T("%Y")), |
37df1f33 VZ |
305 | wxDefaultPosition, wxDefaultSize, |
306 | wxALIGN_CENTRE); | |
307 | ||
308 | m_comboMonth = new wxMonthComboBox(this); | |
ca65c044 | 309 | m_staticMonth = new wxStaticText(GetParent(), wxID_ANY, m_date.Format(_T("%B")), |
37df1f33 VZ |
310 | wxDefaultPosition, wxDefaultSize, |
311 | wxALIGN_CENTRE); | |
312 | } | |
bc385ba9 VZ |
313 | |
314 | ShowCurrentControls(); | |
9d9b7755 | 315 | |
bb61a983 VZ |
316 | // we need to set the position as well because the main control position |
317 | // is not the same as the one specified in pos if we have the controls | |
318 | // above it | |
ab473127 RD |
319 | SetBestSize(size); |
320 | SetPosition(pos); | |
85fab316 RD |
321 | |
322 | // Since we don't paint the whole background make sure that the platform | |
323 | // will use the right one. | |
324 | SetBackgroundColour(GetBackgroundColour()); | |
2997ca30 | 325 | |
4f6aed9c VZ |
326 | SetHolidayAttrs(); |
327 | ||
ca65c044 | 328 | return true; |
2ef31e80 VZ |
329 | } |
330 | ||
882a8f40 VZ |
331 | wxCalendarCtrl::~wxCalendarCtrl() |
332 | { | |
4f6aed9c VZ |
333 | for ( size_t n = 0; n < WXSIZEOF(m_attrs); n++ ) |
334 | { | |
335 | delete m_attrs[n]; | |
336 | } | |
882a8f40 VZ |
337 | } |
338 | ||
339 | // ---------------------------------------------------------------------------- | |
340 | // forward wxWin functions to subcontrols | |
341 | // ---------------------------------------------------------------------------- | |
342 | ||
b70462f4 MB |
343 | bool wxCalendarCtrl::Destroy() |
344 | { | |
f0d5e7a2 VZ |
345 | if ( m_staticYear ) |
346 | m_staticYear->Destroy(); | |
347 | if ( m_spinYear ) | |
348 | m_spinYear->Destroy(); | |
349 | if ( m_comboMonth ) | |
350 | m_comboMonth->Destroy(); | |
351 | if ( m_staticMonth ) | |
352 | m_staticMonth->Destroy(); | |
353 | ||
354 | m_staticYear = NULL; | |
355 | m_spinYear = NULL; | |
356 | m_comboMonth = NULL; | |
357 | m_staticMonth = NULL; | |
b70462f4 MB |
358 | |
359 | return wxControl::Destroy(); | |
360 | } | |
361 | ||
882a8f40 VZ |
362 | bool wxCalendarCtrl::Show(bool show) |
363 | { | |
364 | if ( !wxControl::Show(show) ) | |
365 | { | |
ca65c044 | 366 | return false; |
882a8f40 VZ |
367 | } |
368 | ||
37df1f33 VZ |
369 | if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) ) |
370 | { | |
371 | if ( GetMonthControl() ) | |
372 | { | |
373 | GetMonthControl()->Show(show); | |
374 | GetYearControl()->Show(show); | |
375 | } | |
376 | } | |
882a8f40 | 377 | |
ca65c044 | 378 | return true; |
882a8f40 VZ |
379 | } |
380 | ||
381 | bool wxCalendarCtrl::Enable(bool enable) | |
382 | { | |
383 | if ( !wxControl::Enable(enable) ) | |
384 | { | |
ca65c044 | 385 | return false; |
882a8f40 VZ |
386 | } |
387 | ||
37df1f33 VZ |
388 | if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) ) |
389 | { | |
390 | GetMonthControl()->Enable(enable); | |
391 | GetYearControl()->Enable(enable); | |
392 | } | |
882a8f40 | 393 | |
ca65c044 | 394 | return true; |
882a8f40 VZ |
395 | } |
396 | ||
bc385ba9 VZ |
397 | // ---------------------------------------------------------------------------- |
398 | // enable/disable month/year controls | |
399 | // ---------------------------------------------------------------------------- | |
400 | ||
401 | void wxCalendarCtrl::ShowCurrentControls() | |
402 | { | |
bb61a983 | 403 | if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) ) |
bc385ba9 | 404 | { |
37df1f33 | 405 | if ( AllowMonthChange() ) |
bc385ba9 | 406 | { |
37df1f33 VZ |
407 | m_comboMonth->Show(); |
408 | m_staticMonth->Hide(); | |
409 | ||
410 | if ( AllowYearChange() ) | |
411 | { | |
412 | m_spinYear->Show(); | |
413 | m_staticYear->Hide(); | |
bc385ba9 | 414 | |
37df1f33 VZ |
415 | // skip the rest |
416 | return; | |
417 | } | |
418 | } | |
419 | else | |
420 | { | |
421 | m_comboMonth->Hide(); | |
422 | m_staticMonth->Show(); | |
bc385ba9 | 423 | } |
bc385ba9 | 424 | |
37df1f33 VZ |
425 | // year change not allowed here |
426 | m_spinYear->Hide(); | |
427 | m_staticYear->Show(); | |
428 | } | |
bc385ba9 VZ |
429 | } |
430 | ||
431 | wxControl *wxCalendarCtrl::GetMonthControl() const | |
432 | { | |
380d9d62 | 433 | return AllowMonthChange() ? (wxControl *)m_comboMonth : (wxControl *)m_staticMonth; |
bc385ba9 VZ |
434 | } |
435 | ||
436 | wxControl *wxCalendarCtrl::GetYearControl() const | |
437 | { | |
380d9d62 | 438 | return AllowYearChange() ? (wxControl *)m_spinYear : (wxControl *)m_staticYear; |
bc385ba9 VZ |
439 | } |
440 | ||
441 | void wxCalendarCtrl::EnableYearChange(bool enable) | |
442 | { | |
443 | if ( enable != AllowYearChange() ) | |
444 | { | |
445 | long style = GetWindowStyle(); | |
446 | if ( enable ) | |
447 | style &= ~wxCAL_NO_YEAR_CHANGE; | |
448 | else | |
449 | style |= wxCAL_NO_YEAR_CHANGE; | |
450 | SetWindowStyle(style); | |
451 | ||
452 | ShowCurrentControls(); | |
37df1f33 VZ |
453 | if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION ) |
454 | { | |
455 | Refresh(); | |
456 | } | |
bc385ba9 VZ |
457 | } |
458 | } | |
459 | ||
460 | void wxCalendarCtrl::EnableMonthChange(bool enable) | |
461 | { | |
462 | if ( enable != AllowMonthChange() ) | |
463 | { | |
464 | long style = GetWindowStyle(); | |
465 | if ( enable ) | |
466 | style &= ~wxCAL_NO_MONTH_CHANGE; | |
467 | else | |
468 | style |= wxCAL_NO_MONTH_CHANGE; | |
469 | SetWindowStyle(style); | |
1bcf0c7d | 470 | |
bc385ba9 | 471 | ShowCurrentControls(); |
37df1f33 VZ |
472 | if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION ) |
473 | { | |
474 | Refresh(); | |
475 | } | |
bc385ba9 VZ |
476 | } |
477 | } | |
478 | ||
9d9b7755 VZ |
479 | // ---------------------------------------------------------------------------- |
480 | // changing date | |
481 | // ---------------------------------------------------------------------------- | |
482 | ||
37df1f33 | 483 | bool wxCalendarCtrl::SetDate(const wxDateTime& date) |
9d9b7755 | 484 | { |
ca65c044 | 485 | bool retval = true; |
37df1f33 | 486 | |
bc385ba9 VZ |
487 | bool sameMonth = m_date.GetMonth() == date.GetMonth(), |
488 | sameYear = m_date.GetYear() == date.GetYear(); | |
489 | ||
37df1f33 | 490 | if ( IsDateInRange(date) ) |
9d9b7755 | 491 | { |
37df1f33 | 492 | if ( sameMonth && sameYear ) |
bc385ba9 | 493 | { |
37df1f33 | 494 | // just change the day |
37df1f33 | 495 | ChangeDay(date); |
bc385ba9 | 496 | } |
37df1f33 VZ |
497 | else |
498 | { | |
f0d5e7a2 | 499 | if ( AllowMonthChange() && (AllowYearChange() || sameYear) ) |
37df1f33 | 500 | { |
f0d5e7a2 VZ |
501 | // change everything |
502 | m_date = date; | |
bc385ba9 | 503 | |
f0d5e7a2 | 504 | if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) ) |
37df1f33 | 505 | { |
f0d5e7a2 VZ |
506 | // update the controls |
507 | m_comboMonth->SetSelection(m_date.GetMonth()); | |
508 | ||
509 | if ( AllowYearChange() ) | |
510 | { | |
511 | if ( !m_userChangedYear ) | |
512 | m_spinYear->SetValue(m_date.Format(_T("%Y"))); | |
f0d5e7a2 | 513 | } |
37df1f33 | 514 | } |
9d9b7755 | 515 | |
f0d5e7a2 VZ |
516 | // as the month changed, holidays did too |
517 | SetHolidayAttrs(); | |
0de868d9 | 518 | |
f0d5e7a2 VZ |
519 | // update the calendar |
520 | Refresh(); | |
521 | } | |
522 | else | |
523 | { | |
524 | // forbidden | |
ca65c044 | 525 | retval = false; |
f0d5e7a2 | 526 | } |
37df1f33 | 527 | } |
9d9b7755 | 528 | } |
37df1f33 | 529 | |
ca65c044 WS |
530 | m_userChangedYear = false; |
531 | ||
37df1f33 | 532 | return retval; |
9d9b7755 VZ |
533 | } |
534 | ||
535 | void wxCalendarCtrl::ChangeDay(const wxDateTime& date) | |
536 | { | |
537 | if ( m_date != date ) | |
538 | { | |
539 | // we need to refresh the row containing the old date and the one | |
540 | // containing the new one | |
541 | wxDateTime dateOld = m_date; | |
542 | m_date = date; | |
543 | ||
544 | RefreshDate(dateOld); | |
545 | ||
546 | // if the date is in the same row, it was already drawn correctly | |
547 | if ( GetWeek(m_date) != GetWeek(dateOld) ) | |
548 | { | |
549 | RefreshDate(m_date); | |
550 | } | |
551 | } | |
552 | } | |
553 | ||
554 | void wxCalendarCtrl::SetDateAndNotify(const wxDateTime& date) | |
555 | { | |
556 | wxDateTime::Tm tm1 = m_date.GetTm(), | |
557 | tm2 = date.GetTm(); | |
558 | ||
559 | wxEventType type; | |
560 | if ( tm1.year != tm2.year ) | |
561 | type = wxEVT_CALENDAR_YEAR_CHANGED; | |
562 | else if ( tm1.mon != tm2.mon ) | |
563 | type = wxEVT_CALENDAR_MONTH_CHANGED; | |
564 | else if ( tm1.mday != tm2.mday ) | |
565 | type = wxEVT_CALENDAR_DAY_CHANGED; | |
566 | else | |
567 | return; | |
568 | ||
37df1f33 VZ |
569 | if ( SetDate(date) ) |
570 | { | |
571 | GenerateEvents(type, wxEVT_CALENDAR_SEL_CHANGED); | |
572 | } | |
573 | } | |
574 | ||
575 | // ---------------------------------------------------------------------------- | |
576 | // date range | |
577 | // ---------------------------------------------------------------------------- | |
578 | ||
579 | bool wxCalendarCtrl::SetLowerDateLimit(const wxDateTime& date /* = wxDefaultDateTime */) | |
580 | { | |
ca65c044 | 581 | bool retval = true; |
37df1f33 | 582 | |
ca65c044 | 583 | if ( !(date.IsValid()) || ( ( m_highdate.IsValid() ) ? ( date <= m_highdate ) : true ) ) |
37df1f33 VZ |
584 | { |
585 | m_lowdate = date; | |
586 | } | |
587 | else | |
588 | { | |
ca65c044 | 589 | retval = false; |
37df1f33 | 590 | } |
9d9b7755 | 591 | |
37df1f33 VZ |
592 | return retval; |
593 | } | |
594 | ||
595 | bool wxCalendarCtrl::SetUpperDateLimit(const wxDateTime& date /* = wxDefaultDateTime */) | |
596 | { | |
ca65c044 | 597 | bool retval = true; |
37df1f33 | 598 | |
ca65c044 | 599 | if ( !(date.IsValid()) || ( ( m_lowdate.IsValid() ) ? ( date >= m_lowdate ) : true ) ) |
37df1f33 VZ |
600 | { |
601 | m_highdate = date; | |
602 | } | |
603 | else | |
604 | { | |
ca65c044 | 605 | retval = false; |
37df1f33 VZ |
606 | } |
607 | ||
608 | return retval; | |
609 | } | |
610 | ||
611 | bool wxCalendarCtrl::SetDateRange(const wxDateTime& lowerdate /* = wxDefaultDateTime */, const wxDateTime& upperdate /* = wxDefaultDateTime */) | |
612 | { | |
ca65c044 | 613 | bool retval = true; |
37df1f33 VZ |
614 | |
615 | if ( | |
ca65c044 WS |
616 | ( !( lowerdate.IsValid() ) || ( ( upperdate.IsValid() ) ? ( lowerdate <= upperdate ) : true ) ) && |
617 | ( !( upperdate.IsValid() ) || ( ( lowerdate.IsValid() ) ? ( upperdate >= lowerdate ) : true ) ) ) | |
37df1f33 VZ |
618 | { |
619 | m_lowdate = lowerdate; | |
620 | m_highdate = upperdate; | |
621 | } | |
622 | else | |
623 | { | |
ca65c044 | 624 | retval = false; |
37df1f33 | 625 | } |
1bcf0c7d | 626 | |
37df1f33 | 627 | return retval; |
9d9b7755 VZ |
628 | } |
629 | ||
2ef31e80 VZ |
630 | // ---------------------------------------------------------------------------- |
631 | // date helpers | |
632 | // ---------------------------------------------------------------------------- | |
633 | ||
634 | wxDateTime wxCalendarCtrl::GetStartDate() const | |
635 | { | |
636 | wxDateTime::Tm tm = m_date.GetTm(); | |
637 | ||
638 | wxDateTime date = wxDateTime(1, tm.mon, tm.year); | |
9d9b7755 | 639 | |
1a8557b1 VZ |
640 | // rewind back |
641 | date.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST | |
642 | ? wxDateTime::Mon : wxDateTime::Sun); | |
643 | ||
37df1f33 VZ |
644 | if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS ) |
645 | { | |
646 | // We want to offset the calendar if we start on the first.. | |
647 | if ( date.GetDay() == 1 ) | |
648 | { | |
649 | date -= wxDateSpan::Week(); | |
650 | } | |
651 | } | |
652 | ||
2ef31e80 VZ |
653 | return date; |
654 | } | |
655 | ||
656 | bool wxCalendarCtrl::IsDateShown(const wxDateTime& date) const | |
657 | { | |
37df1f33 VZ |
658 | if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS) ) |
659 | { | |
660 | return date.GetMonth() == m_date.GetMonth(); | |
661 | } | |
662 | else | |
663 | { | |
ca65c044 | 664 | return true; |
37df1f33 VZ |
665 | } |
666 | } | |
667 | ||
668 | bool wxCalendarCtrl::IsDateInRange(const wxDateTime& date) const | |
669 | { | |
37df1f33 | 670 | // Check if the given date is in the range specified |
ca65c044 WS |
671 | return ( ( ( m_lowdate.IsValid() ) ? ( date >= m_lowdate ) : true ) |
672 | && ( ( m_highdate.IsValid() ) ? ( date <= m_highdate ) : true ) ); | |
37df1f33 VZ |
673 | } |
674 | ||
675 | bool wxCalendarCtrl::ChangeYear(wxDateTime* target) const | |
676 | { | |
ca65c044 | 677 | bool retval = false; |
37df1f33 VZ |
678 | |
679 | if ( !(IsDateInRange(*target)) ) | |
680 | { | |
681 | if ( target->GetYear() < m_date.GetYear() ) | |
682 | { | |
683 | if ( target->GetYear() >= GetLowerDateLimit().GetYear() ) | |
684 | { | |
685 | *target = GetLowerDateLimit(); | |
ca65c044 | 686 | retval = true; |
37df1f33 VZ |
687 | } |
688 | else | |
689 | { | |
690 | *target = m_date; | |
691 | } | |
692 | } | |
693 | else | |
694 | { | |
695 | if ( target->GetYear() <= GetUpperDateLimit().GetYear() ) | |
696 | { | |
697 | *target = GetUpperDateLimit(); | |
ca65c044 | 698 | retval = true; |
37df1f33 VZ |
699 | } |
700 | else | |
701 | { | |
702 | *target = m_date; | |
703 | } | |
704 | } | |
705 | } | |
706 | else | |
707 | { | |
ca65c044 | 708 | retval = true; |
37df1f33 VZ |
709 | } |
710 | ||
711 | return retval; | |
712 | } | |
713 | ||
714 | bool wxCalendarCtrl::ChangeMonth(wxDateTime* target) const | |
715 | { | |
ca65c044 | 716 | bool retval = true; |
37df1f33 VZ |
717 | |
718 | if ( !(IsDateInRange(*target)) ) | |
719 | { | |
ca65c044 | 720 | retval = false; |
37df1f33 VZ |
721 | |
722 | if ( target->GetMonth() < m_date.GetMonth() ) | |
723 | { | |
724 | *target = GetLowerDateLimit(); | |
725 | } | |
726 | else | |
727 | { | |
728 | *target = GetUpperDateLimit(); | |
729 | } | |
730 | } | |
731 | ||
732 | return retval; | |
2ef31e80 VZ |
733 | } |
734 | ||
9d9b7755 VZ |
735 | size_t wxCalendarCtrl::GetWeek(const wxDateTime& date) const |
736 | { | |
37df1f33 VZ |
737 | size_t retval = date.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST |
738 | ? wxDateTime::Monday_First | |
739 | : wxDateTime::Sunday_First); | |
740 | ||
741 | if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS) ) | |
742 | { | |
743 | // we need to offset an extra week if we "start" on the 1st of the month | |
744 | wxDateTime::Tm tm = date.GetTm(); | |
745 | ||
746 | wxDateTime datetest = wxDateTime(1, tm.mon, tm.year); | |
747 | ||
748 | // rewind back | |
749 | datetest.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST | |
750 | ? wxDateTime::Mon : wxDateTime::Sun); | |
1bcf0c7d | 751 | |
37df1f33 VZ |
752 | if ( datetest.GetDay() == 1 ) |
753 | { | |
754 | retval += 1; | |
755 | } | |
756 | } | |
1bcf0c7d | 757 | |
37df1f33 | 758 | return retval; |
9d9b7755 VZ |
759 | } |
760 | ||
2ef31e80 VZ |
761 | // ---------------------------------------------------------------------------- |
762 | // size management | |
763 | // ---------------------------------------------------------------------------- | |
764 | ||
9d9b7755 VZ |
765 | // this is a composite control and it must arrange its parts each time its |
766 | // size or position changes: the combobox and spinctrl are along the top of | |
767 | // the available area and the calendar takes up therest of the space | |
768 | ||
bc385ba9 VZ |
769 | // the static controls are supposed to be always smaller than combo/spin so we |
770 | // always use the latter for size calculations and position the static to take | |
771 | // the same space | |
772 | ||
9d9b7755 VZ |
773 | // the constants used for the layout |
774 | #define VERT_MARGIN 5 // distance between combo and calendar | |
81b6ccf1 SC |
775 | #ifdef __WXMAC__ |
776 | #define HORZ_MARGIN 5 // spin | |
777 | #else | |
9d9b7755 | 778 | #define HORZ_MARGIN 15 // spin |
81b6ccf1 | 779 | #endif |
2ef31e80 VZ |
780 | wxSize wxCalendarCtrl::DoGetBestSize() const |
781 | { | |
9d9b7755 VZ |
782 | // calc the size of the calendar |
783 | ((wxCalendarCtrl *)this)->RecalcGeometry(); // const_cast | |
784 | ||
785 | wxCoord width = 7*m_widthCol, | |
bb61a983 | 786 | height = 7*m_heightRow + m_rowOffset + VERT_MARGIN; |
9d9b7755 | 787 | |
bb61a983 | 788 | if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) ) |
37df1f33 | 789 | { |
bb61a983 VZ |
790 | // the combobox doesn't report its height correctly (it returns the |
791 | // height including the drop down list) so don't use it | |
792 | height += m_spinYear->GetBestSize().y; | |
37df1f33 VZ |
793 | } |
794 | ||
562e3578 JS |
795 | wxCoord w2= m_comboMonth->GetBestSize().x + HORZ_MARGIN + GetCharWidth()*6; |
796 | if (width < w2) | |
797 | width=w2; | |
798 | ||
bb61a983 | 799 | if ( !HasFlag(wxBORDER_NONE) ) |
bc385ba9 VZ |
800 | { |
801 | // the border would clip the last line otherwise | |
f41cb81e | 802 | height += 6; |
37df1f33 | 803 | width += 4; |
bc385ba9 VZ |
804 | } |
805 | ||
ab473127 RD |
806 | wxSize best(width, height); |
807 | CacheBestSize(best); | |
2997ca30 | 808 | return best; |
2ef31e80 VZ |
809 | } |
810 | ||
811 | void wxCalendarCtrl::DoSetSize(int x, int y, | |
812 | int width, int height, | |
813 | int sizeFlags) | |
814 | { | |
815 | wxControl::DoSetSize(x, y, width, height, sizeFlags); | |
816 | } | |
817 | ||
818 | void wxCalendarCtrl::DoMoveWindow(int x, int y, int width, int height) | |
819 | { | |
bb61a983 | 820 | int yDiff; |
2ef31e80 | 821 | |
bb61a983 | 822 | if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) ) |
37df1f33 VZ |
823 | { |
824 | wxSize sizeCombo = m_comboMonth->GetSize(); | |
825 | wxSize sizeStatic = m_staticMonth->GetSize(); | |
95bcc40c | 826 | wxSize sizeSpin = m_spinYear->GetSize(); |
37df1f33 | 827 | int dy = (sizeCombo.y - sizeStatic.y) / 2; |
e910c000 JS |
828 | m_comboMonth->Move(x, y); |
829 | m_staticMonth->SetSize(x, y + dy, sizeCombo.x, sizeStatic.y); | |
37df1f33 | 830 | |
e910c000 JS |
831 | int xDiff = sizeCombo.x + HORZ_MARGIN; |
832 | ||
833 | m_spinYear->SetSize(x + xDiff, y, width - xDiff, sizeCombo.y); | |
834 | m_staticYear->SetSize(x + xDiff, y + dy, width - xDiff, sizeStatic.y); | |
50807f90 | 835 | |
37df1f33 VZ |
836 | yDiff = wxMax(sizeSpin.y, sizeCombo.y) + VERT_MARGIN; |
837 | } | |
bb61a983 VZ |
838 | else // no controls on the top |
839 | { | |
840 | yDiff = 0; | |
841 | } | |
9d9b7755 VZ |
842 | |
843 | wxControl::DoMoveWindow(x, y + yDiff, width, height - yDiff); | |
844 | } | |
845 | ||
882a8f40 VZ |
846 | void wxCalendarCtrl::DoGetPosition(int *x, int *y) const |
847 | { | |
848 | wxControl::DoGetPosition(x, y); | |
849 | ||
37df1f33 | 850 | if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) ) |
882a8f40 | 851 | { |
37df1f33 VZ |
852 | // our real top corner is not in this position |
853 | if ( y ) | |
854 | { | |
855 | *y -= GetMonthControl()->GetSize().y + VERT_MARGIN; | |
856 | } | |
882a8f40 VZ |
857 | } |
858 | } | |
859 | ||
860 | void wxCalendarCtrl::DoGetSize(int *width, int *height) const | |
861 | { | |
862 | wxControl::DoGetSize(width, height); | |
863 | ||
37df1f33 | 864 | if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) ) |
882a8f40 | 865 | { |
37df1f33 | 866 | // our real height is bigger |
5b3610da | 867 | if ( height && GetMonthControl()) |
37df1f33 VZ |
868 | { |
869 | *height += GetMonthControl()->GetSize().y + VERT_MARGIN; | |
870 | } | |
882a8f40 VZ |
871 | } |
872 | } | |
873 | ||
9d9b7755 | 874 | void wxCalendarCtrl::RecalcGeometry() |
2ef31e80 | 875 | { |
9d9b7755 | 876 | wxClientDC dc(this); |
3965571c | 877 | |
ab473127 | 878 | dc.SetFont(GetFont()); |
3965571c | 879 | |
2ef31e80 VZ |
880 | // determine the column width (we assume that the weekday names are always |
881 | // wider (in any language) than the numbers) | |
882 | m_widthCol = 0; | |
9d9b7755 | 883 | wxDateTime::WeekDay wd; |
2ef31e80 VZ |
884 | for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) ) |
885 | { | |
2ef31e80 | 886 | wxCoord width; |
9d9b7755 | 887 | dc.GetTextExtent(m_weekdays[wd], &width, &m_heightRow); |
2ef31e80 VZ |
888 | if ( width > m_widthCol ) |
889 | { | |
890 | m_widthCol = width; | |
891 | } | |
892 | } | |
3965571c | 893 | |
2ef31e80 VZ |
894 | // leave some margins |
895 | m_widthCol += 2; | |
896 | m_heightRow += 2; | |
37df1f33 VZ |
897 | |
898 | m_rowOffset = (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) ? m_heightRow : 0; // conditional in relation to style | |
9d9b7755 VZ |
899 | } |
900 | ||
901 | // ---------------------------------------------------------------------------- | |
902 | // drawing | |
903 | // ---------------------------------------------------------------------------- | |
904 | ||
13111b2a | 905 | void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) |
9d9b7755 | 906 | { |
07e87221 | 907 | wxPaintDC dc(this); |
3965571c | 908 | |
ab473127 | 909 | dc.SetFont(GetFont()); |
9d9b7755 | 910 | |
3965571c | 911 | RecalcGeometry(); |
9d9b7755 | 912 | |
882a8f40 | 913 | #if DEBUG_PAINT |
f6bcfd97 | 914 | wxLogDebug("--- starting to paint, selection: %s, week %u\n", |
9d9b7755 VZ |
915 | m_date.Format("%a %d-%m-%Y %H:%M:%S").c_str(), |
916 | GetWeek(m_date)); | |
882a8f40 | 917 | #endif |
2ef31e80 | 918 | |
37df1f33 | 919 | wxCoord y = 0; |
562e3578 JS |
920 | wxCoord x0 = (GetSize().x - m_widthCol*7) /2; |
921 | if (x0 < 0) x0 = 0; | |
37df1f33 | 922 | |
bb61a983 | 923 | if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) ) |
37df1f33 VZ |
924 | { |
925 | // draw the sequential month-selector | |
926 | ||
927 | dc.SetBackgroundMode(wxTRANSPARENT); | |
928 | dc.SetTextForeground(*wxBLACK); | |
929 | dc.SetBrush(wxBrush(m_colHeaderBg, wxSOLID)); | |
930 | dc.SetPen(wxPen(m_colHeaderBg, 1, wxSOLID)); | |
562e3578 | 931 | dc.DrawRectangle(x0, y, GetClientSize().x, m_heightRow); |
37df1f33 VZ |
932 | |
933 | // Get extent of month-name + year | |
934 | wxCoord monthw, monthh; | |
50d0b061 | 935 | wxString headertext = m_date.Format(wxT("%B %Y")); |
37df1f33 VZ |
936 | dc.GetTextExtent(headertext, &monthw, &monthh); |
937 | ||
938 | // draw month-name centered above weekdays | |
562e3578 | 939 | wxCoord monthx = ((m_widthCol * 7) - monthw) / 2 + x0; |
37df1f33 | 940 | wxCoord monthy = ((m_heightRow - monthh) / 2) + y; |
1bcf0c7d | 941 | dc.DrawText(headertext, monthx, monthy); |
37df1f33 VZ |
942 | |
943 | // calculate the "month-arrows" | |
944 | wxPoint leftarrow[3]; | |
945 | wxPoint rightarrow[3]; | |
946 | ||
947 | int arrowheight = monthh / 2; | |
1bcf0c7d | 948 | |
37df1f33 VZ |
949 | leftarrow[0] = wxPoint(0, arrowheight / 2); |
950 | leftarrow[1] = wxPoint(arrowheight / 2, 0); | |
951 | leftarrow[2] = wxPoint(arrowheight / 2, arrowheight - 1); | |
952 | ||
c47addef | 953 | rightarrow[0] = wxPoint(0,0); |
37df1f33 VZ |
954 | rightarrow[1] = wxPoint(arrowheight / 2, arrowheight / 2); |
955 | rightarrow[2] = wxPoint(0, arrowheight - 1); | |
956 | ||
957 | // draw the "month-arrows" | |
958 | ||
959 | wxCoord arrowy = (m_heightRow - arrowheight) / 2; | |
562e3578 JS |
960 | wxCoord larrowx = (m_widthCol - (arrowheight / 2)) / 2 + x0; |
961 | wxCoord rarrowx = ((m_widthCol - (arrowheight / 2)) / 2) + m_widthCol*6 + x0; | |
c47addef | 962 | m_leftArrowRect = m_rightArrowRect = wxRect(0,0,0,0); |
37df1f33 VZ |
963 | |
964 | if ( AllowMonthChange() ) | |
965 | { | |
966 | wxDateTime ldpm = wxDateTime(1,m_date.GetMonth(), m_date.GetYear()) - wxDateSpan::Day(); // last day prev month | |
967 | // Check if range permits change | |
ca65c044 | 968 | if ( IsDateInRange(ldpm) && ( ( ldpm.GetYear() == m_date.GetYear() ) ? true : AllowYearChange() ) ) |
37df1f33 VZ |
969 | { |
970 | m_leftArrowRect = wxRect(larrowx - 3, arrowy - 3, (arrowheight / 2) + 8, (arrowheight + 6)); | |
971 | dc.SetBrush(wxBrush(*wxBLACK, wxSOLID)); | |
972 | dc.SetPen(wxPen(*wxBLACK, 1, wxSOLID)); | |
973 | dc.DrawPolygon(3, leftarrow, larrowx , arrowy, wxWINDING_RULE); | |
974 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
975 | dc.DrawRectangle(m_leftArrowRect); | |
976 | } | |
977 | wxDateTime fdnm = wxDateTime(1,m_date.GetMonth(), m_date.GetYear()) + wxDateSpan::Month(); // first day next month | |
ca65c044 | 978 | if ( IsDateInRange(fdnm) && ( ( fdnm.GetYear() == m_date.GetYear() ) ? true : AllowYearChange() ) ) |
37df1f33 VZ |
979 | { |
980 | m_rightArrowRect = wxRect(rarrowx - 4, arrowy - 3, (arrowheight / 2) + 8, (arrowheight + 6)); | |
981 | dc.SetBrush(wxBrush(*wxBLACK, wxSOLID)); | |
982 | dc.SetPen(wxPen(*wxBLACK, 1, wxSOLID)); | |
983 | dc.DrawPolygon(3, rightarrow, rarrowx , arrowy, wxWINDING_RULE); | |
984 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
985 | dc.DrawRectangle(m_rightArrowRect); | |
986 | } | |
987 | } | |
988 | ||
989 | y += m_heightRow; | |
990 | } | |
991 | ||
2ef31e80 | 992 | // first draw the week days |
562e3578 | 993 | if ( IsExposed(x0, y, 7*m_widthCol, m_heightRow) ) |
2ef31e80 | 994 | { |
882a8f40 | 995 | #if DEBUG_PAINT |
f6bcfd97 | 996 | wxLogDebug("painting the header"); |
882a8f40 | 997 | #endif |
9d9b7755 | 998 | |
9d9b7755 | 999 | dc.SetBackgroundMode(wxTRANSPARENT); |
4f6aed9c VZ |
1000 | dc.SetTextForeground(m_colHeaderFg); |
1001 | dc.SetBrush(wxBrush(m_colHeaderBg, wxSOLID)); | |
1002 | dc.SetPen(wxPen(m_colHeaderBg, 1, wxSOLID)); | |
bb61a983 | 1003 | dc.DrawRectangle(0, y, GetClientSize().x, m_heightRow); |
1a8557b1 VZ |
1004 | |
1005 | bool startOnMonday = (GetWindowStyle() & wxCAL_MONDAY_FIRST) != 0; | |
1006 | for ( size_t wd = 0; wd < 7; wd++ ) | |
9d9b7755 | 1007 | { |
1a8557b1 VZ |
1008 | size_t n; |
1009 | if ( startOnMonday ) | |
1010 | n = wd == 6 ? 0 : wd + 1; | |
1011 | else | |
1012 | n = wd; | |
37df1f33 VZ |
1013 | wxCoord dayw, dayh; |
1014 | dc.GetTextExtent(m_weekdays[n], &dayw, &dayh); | |
562e3578 | 1015 | dc.DrawText(m_weekdays[n], x0 + (wd*m_widthCol) + ((m_widthCol- dayw) / 2), y); // center the day-name |
9d9b7755 | 1016 | } |
2ef31e80 VZ |
1017 | } |
1018 | ||
1019 | // then the calendar itself | |
1020 | dc.SetTextForeground(*wxBLACK); | |
1021 | //dc.SetFont(*wxNORMAL_FONT); | |
1022 | ||
37df1f33 | 1023 | y += m_heightRow; |
2ef31e80 | 1024 | wxDateTime date = GetStartDate(); |
37df1f33 | 1025 | |
882a8f40 | 1026 | #if DEBUG_PAINT |
f6bcfd97 | 1027 | wxLogDebug("starting calendar from %s\n", |
9d9b7755 | 1028 | date.Format("%a %d-%m-%Y %H:%M:%S").c_str()); |
882a8f40 | 1029 | #endif |
9d9b7755 | 1030 | |
2ef31e80 | 1031 | dc.SetBackgroundMode(wxSOLID); |
9d9b7755 | 1032 | for ( size_t nWeek = 1; nWeek <= 6; nWeek++, y += m_heightRow ) |
2ef31e80 | 1033 | { |
9d9b7755 | 1034 | // if the update region doesn't intersect this row, don't paint it |
15807266 | 1035 | if ( !IsExposed(0, y, 7*m_widthCol, m_heightRow - 1) ) |
9d9b7755 VZ |
1036 | { |
1037 | date += wxDateSpan::Week(); | |
1038 | ||
1039 | continue; | |
1040 | } | |
882a8f40 | 1041 | |
1a8557b1 | 1042 | #if DEBUG_PAINT |
f6bcfd97 | 1043 | wxLogDebug("painting week %d at y = %d\n", nWeek, y); |
882a8f40 | 1044 | #endif |
9d9b7755 | 1045 | |
1a8557b1 | 1046 | for ( size_t wd = 0; wd < 7; wd++ ) |
2ef31e80 VZ |
1047 | { |
1048 | if ( IsDateShown(date) ) | |
1049 | { | |
882a8f40 | 1050 | // don't use wxDate::Format() which prepends 0s |
4f6aed9c VZ |
1051 | unsigned int day = date.GetDay(); |
1052 | wxString dayStr = wxString::Format(_T("%u"), day); | |
3965571c | 1053 | wxCoord width; |
4f6aed9c VZ |
1054 | dc.GetTextExtent(dayStr, &width, (wxCoord *)NULL); |
1055 | ||
ca65c044 WS |
1056 | bool changedColours = false, |
1057 | changedFont = false; | |
4f6aed9c | 1058 | |
ca65c044 | 1059 | bool isSel = false; |
37df1f33 | 1060 | wxCalendarDateAttr *attr = NULL; |
2ef31e80 | 1061 | |
37df1f33 | 1062 | if ( date.GetMonth() != m_date.GetMonth() || !IsDateInRange(date) ) |
2ef31e80 | 1063 | { |
37df1f33 VZ |
1064 | // surrounding week or out-of-range |
1065 | // draw "disabled" | |
1066 | dc.SetTextForeground(*wxLIGHT_GREY); | |
ca65c044 | 1067 | changedColours = true; |
4f6aed9c | 1068 | } |
37df1f33 | 1069 | else |
4f6aed9c | 1070 | { |
37df1f33 VZ |
1071 | isSel = date.IsSameDate(m_date); |
1072 | attr = m_attrs[day - 1]; | |
4f6aed9c | 1073 | |
37df1f33 | 1074 | if ( isSel ) |
4f6aed9c | 1075 | { |
37df1f33 VZ |
1076 | dc.SetTextForeground(m_colHighlightFg); |
1077 | dc.SetTextBackground(m_colHighlightBg); | |
4f6aed9c | 1078 | |
ca65c044 | 1079 | changedColours = true; |
4f6aed9c | 1080 | } |
37df1f33 | 1081 | else if ( attr ) |
4f6aed9c | 1082 | { |
37df1f33 VZ |
1083 | wxColour colFg, colBg; |
1084 | ||
1085 | if ( attr->IsHoliday() ) | |
1086 | { | |
1087 | colFg = m_colHolidayFg; | |
1088 | colBg = m_colHolidayBg; | |
1089 | } | |
1090 | else | |
1091 | { | |
1092 | colFg = attr->GetTextColour(); | |
1093 | colBg = attr->GetBackgroundColour(); | |
1094 | } | |
1095 | ||
1096 | if ( colFg.Ok() ) | |
1097 | { | |
1098 | dc.SetTextForeground(colFg); | |
ca65c044 | 1099 | changedColours = true; |
37df1f33 VZ |
1100 | } |
1101 | ||
1102 | if ( colBg.Ok() ) | |
1103 | { | |
1104 | dc.SetTextBackground(colBg); | |
ca65c044 | 1105 | changedColours = true; |
37df1f33 VZ |
1106 | } |
1107 | ||
1108 | if ( attr->HasFont() ) | |
1109 | { | |
1110 | dc.SetFont(attr->GetFont()); | |
ca65c044 | 1111 | changedFont = true; |
37df1f33 | 1112 | } |
4f6aed9c | 1113 | } |
2ef31e80 VZ |
1114 | } |
1115 | ||
562e3578 | 1116 | wxCoord x = wd*m_widthCol + (m_widthCol - width) / 2 + x0; |
4f6aed9c | 1117 | dc.DrawText(dayStr, x, y + 1); |
2ef31e80 | 1118 | |
4f6aed9c VZ |
1119 | if ( !isSel && attr && attr->HasBorder() ) |
1120 | { | |
1121 | wxColour colBorder; | |
1122 | if ( attr->HasBorderColour() ) | |
1123 | { | |
1124 | colBorder = attr->GetBorderColour(); | |
1125 | } | |
1126 | else | |
1127 | { | |
ab473127 | 1128 | colBorder = GetForegroundColour(); |
4f6aed9c VZ |
1129 | } |
1130 | ||
1131 | wxPen pen(colBorder, 1, wxSOLID); | |
1132 | dc.SetPen(pen); | |
1133 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
1134 | ||
1135 | switch ( attr->GetBorder() ) | |
1136 | { | |
1137 | case wxCAL_BORDER_SQUARE: | |
1138 | dc.DrawRectangle(x - 2, y, | |
1139 | width + 4, m_heightRow); | |
1140 | break; | |
1141 | ||
1142 | case wxCAL_BORDER_ROUND: | |
1143 | dc.DrawEllipse(x - 2, y, | |
1144 | width + 4, m_heightRow); | |
1145 | break; | |
1146 | ||
1147 | default: | |
1148 | wxFAIL_MSG(_T("unknown border type")); | |
1149 | } | |
1150 | } | |
1151 | ||
1152 | if ( changedColours ) | |
2ef31e80 | 1153 | { |
ab473127 RD |
1154 | dc.SetTextForeground(GetForegroundColour()); |
1155 | dc.SetTextBackground(GetBackgroundColour()); | |
2ef31e80 | 1156 | } |
4f6aed9c VZ |
1157 | |
1158 | if ( changedFont ) | |
1159 | { | |
ab473127 | 1160 | dc.SetFont(GetFont()); |
4f6aed9c | 1161 | } |
2ef31e80 VZ |
1162 | } |
1163 | //else: just don't draw it | |
1164 | ||
1165 | date += wxDateSpan::Day(); | |
1166 | } | |
2ef31e80 | 1167 | } |
37df1f33 VZ |
1168 | |
1169 | // Greying out out-of-range background | |
1170 | bool showSurrounding = (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS) != 0; | |
1171 | ||
1172 | date = ( showSurrounding ) ? GetStartDate() : wxDateTime(1, m_date.GetMonth(), m_date.GetYear()); | |
1173 | if ( !IsDateInRange(date) ) | |
1174 | { | |
1175 | wxDateTime firstOOR = GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range | |
1176 | ||
1177 | wxBrush oorbrush = *wxLIGHT_GREY_BRUSH; | |
1178 | oorbrush.SetStyle(wxFDIAGONAL_HATCH); | |
1179 | ||
1180 | HighlightRange(&dc, date, firstOOR, wxTRANSPARENT_PEN, &oorbrush); | |
1181 | } | |
1182 | ||
1183 | date = ( showSurrounding ) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date.GetMonth(), m_date.GetYear()); | |
1184 | if ( !IsDateInRange(date) ) | |
1185 | { | |
1186 | wxDateTime firstOOR = GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range | |
1bcf0c7d | 1187 | |
37df1f33 VZ |
1188 | wxBrush oorbrush = *wxLIGHT_GREY_BRUSH; |
1189 | oorbrush.SetStyle(wxFDIAGONAL_HATCH); | |
1190 | ||
1191 | HighlightRange(&dc, firstOOR, date, wxTRANSPARENT_PEN, &oorbrush); | |
1192 | } | |
1193 | ||
882a8f40 | 1194 | #if DEBUG_PAINT |
f6bcfd97 | 1195 | wxLogDebug("+++ finished painting"); |
882a8f40 | 1196 | #endif |
9d9b7755 VZ |
1197 | } |
1198 | ||
1199 | void wxCalendarCtrl::RefreshDate(const wxDateTime& date) | |
1200 | { | |
1201 | RecalcGeometry(); | |
1202 | ||
1203 | wxRect rect; | |
1204 | ||
1205 | // always refresh the whole row at once because our OnPaint() will draw | |
1206 | // the whole row anyhow - and this allows the small optimisation in | |
1207 | // OnClick() below to work | |
1208 | rect.x = 0; | |
37df1f33 VZ |
1209 | |
1210 | rect.y = (m_heightRow * GetWeek(date)) + m_rowOffset; | |
1211 | ||
9d9b7755 VZ |
1212 | rect.width = 7*m_widthCol; |
1213 | rect.height = m_heightRow; | |
1214 | ||
f6bcfd97 BP |
1215 | #ifdef __WXMSW__ |
1216 | // VZ: for some reason, the selected date seems to occupy more space under | |
1217 | // MSW - this is probably some bug in the font size calculations, but I | |
1218 | // don't know where exactly. This fix is ugly and leads to more | |
1219 | // refreshes than really needed, but without it the selected days | |
1220 | // leaves even more ugly underscores on screen. | |
1221 | rect.Inflate(0, 1); | |
1222 | #endif // MSW | |
1223 | ||
882a8f40 | 1224 | #if DEBUG_PAINT |
f6bcfd97 | 1225 | wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n", |
9d9b7755 VZ |
1226 | GetWeek(date), |
1227 | rect.x, rect.y, | |
1228 | rect.x + rect.width, rect.y + rect.height); | |
882a8f40 | 1229 | #endif |
9d9b7755 | 1230 | |
ca65c044 | 1231 | Refresh(true, &rect); |
2ef31e80 VZ |
1232 | } |
1233 | ||
37df1f33 VZ |
1234 | void wxCalendarCtrl::HighlightRange(wxPaintDC* pDC, const wxDateTime& fromdate, const wxDateTime& todate, wxPen* pPen, wxBrush* pBrush) |
1235 | { | |
1236 | // Highlights the given range using pen and brush | |
1237 | // Does nothing if todate < fromdate | |
1238 | ||
1239 | ||
1240 | #if DEBUG_PAINT | |
1241 | wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate.Format("%d %m %Y"), todate.Format("%d %m %Y")); | |
1242 | #endif | |
1243 | ||
1244 | if ( todate >= fromdate ) | |
1245 | { | |
1246 | // do stuff | |
1247 | // date-coordinates | |
1bcf0c7d | 1248 | int fd, fw; |
37df1f33 VZ |
1249 | int td, tw; |
1250 | ||
1251 | // implicit: both dates must be currently shown - checked by GetDateCoord | |
1252 | if ( GetDateCoord(fromdate, &fd, &fw) && GetDateCoord(todate, &td, &tw) ) | |
1253 | { | |
1254 | #if DEBUG_PAINT | |
1255 | wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd, fw, td, tw); | |
1256 | #endif | |
1257 | if ( ( (tw - fw) == 1 ) && ( td < fd ) ) | |
1258 | { | |
1259 | // special case: interval 7 days or less not in same week | |
1260 | // split in two seperate intervals | |
1261 | wxDateTime tfd = fromdate + wxDateSpan::Days(7-fd); | |
1262 | wxDateTime ftd = tfd + wxDateSpan::Day(); | |
1263 | #if DEBUG_PAINT | |
1264 | wxLogDebug("Highlight: Seperate segments"); | |
1265 | #endif | |
1266 | // draw seperately | |
1267 | HighlightRange(pDC, fromdate, tfd, pPen, pBrush); | |
1268 | HighlightRange(pDC, ftd, todate, pPen, pBrush); | |
1269 | } | |
1270 | else | |
1271 | { | |
1272 | int numpoints; | |
1273 | wxPoint corners[8]; // potentially 8 corners in polygon | |
1274 | ||
1275 | if ( fw == tw ) | |
1276 | { | |
1277 | // simple case: same week | |
1278 | numpoints = 4; | |
1279 | corners[0] = wxPoint((fd - 1) * m_widthCol, (fw * m_heightRow) + m_rowOffset); | |
1280 | corners[1] = wxPoint((fd - 1) * m_widthCol, ((fw + 1 ) * m_heightRow) + m_rowOffset); | |
1281 | corners[2] = wxPoint(td * m_widthCol, ((tw + 1) * m_heightRow) + m_rowOffset); | |
1282 | corners[3] = wxPoint(td * m_widthCol, (tw * m_heightRow) + m_rowOffset); | |
1283 | } | |
1284 | else | |
1285 | { | |
1286 | int cidx = 0; | |
1287 | // "complex" polygon | |
1288 | corners[cidx] = wxPoint((fd - 1) * m_widthCol, (fw * m_heightRow) + m_rowOffset); cidx++; | |
1bcf0c7d | 1289 | |
37df1f33 VZ |
1290 | if ( fd > 1 ) |
1291 | { | |
1292 | corners[cidx] = wxPoint((fd - 1) * m_widthCol, ((fw + 1) * m_heightRow) + m_rowOffset); cidx++; | |
1293 | corners[cidx] = wxPoint(0, ((fw + 1) * m_heightRow) + m_rowOffset); cidx++; | |
1294 | } | |
1295 | ||
1296 | corners[cidx] = wxPoint(0, ((tw + 1) * m_heightRow) + m_rowOffset); cidx++; | |
1297 | corners[cidx] = wxPoint(td * m_widthCol, ((tw + 1) * m_heightRow) + m_rowOffset); cidx++; | |
1298 | ||
1299 | if ( td < 7 ) | |
1300 | { | |
1301 | corners[cidx] = wxPoint(td * m_widthCol, (tw * m_heightRow) + m_rowOffset); cidx++; | |
1302 | corners[cidx] = wxPoint(7 * m_widthCol, (tw * m_heightRow) + m_rowOffset); cidx++; | |
1303 | } | |
1304 | ||
1305 | corners[cidx] = wxPoint(7 * m_widthCol, (fw * m_heightRow) + m_rowOffset); cidx++; | |
1bcf0c7d | 1306 | |
37df1f33 VZ |
1307 | numpoints = cidx; |
1308 | } | |
1309 | ||
1310 | // draw the polygon | |
1311 | pDC->SetBrush(*pBrush); | |
1312 | pDC->SetPen(*pPen); | |
1313 | pDC->DrawPolygon(numpoints, corners); | |
1314 | } | |
1315 | } | |
1316 | } | |
1317 | // else do nothing | |
1318 | #if DEBUG_PAINT | |
1319 | wxLogDebug("--- HighlightRange ---"); | |
1320 | #endif | |
1321 | } | |
1322 | ||
1323 | bool wxCalendarCtrl::GetDateCoord(const wxDateTime& date, int *day, int *week) const | |
1324 | { | |
ca65c044 | 1325 | bool retval = true; |
37df1f33 VZ |
1326 | |
1327 | #if DEBUG_PAINT | |
1328 | wxLogDebug("+++ GetDateCoord: (%s) +++", date.Format("%d %m %Y")); | |
1329 | #endif | |
1330 | ||
1331 | if ( IsDateShown(date) ) | |
1332 | { | |
1333 | bool startOnMonday = ( GetWindowStyle() & wxCAL_MONDAY_FIRST ) != 0; | |
1bcf0c7d | 1334 | |
37df1f33 VZ |
1335 | // Find day |
1336 | *day = date.GetWeekDay(); | |
1bcf0c7d | 1337 | |
37df1f33 VZ |
1338 | if ( *day == 0 ) // sunday |
1339 | { | |
1340 | *day = ( startOnMonday ) ? 7 : 1; | |
1341 | } | |
1342 | else | |
1343 | { | |
999836aa | 1344 | *day += ( startOnMonday ) ? 0 : 1; |
37df1f33 VZ |
1345 | } |
1346 | ||
1347 | int targetmonth = date.GetMonth() + (12 * date.GetYear()); | |
1348 | int thismonth = m_date.GetMonth() + (12 * m_date.GetYear()); | |
1349 | ||
1350 | // Find week | |
1351 | if ( targetmonth == thismonth ) | |
1352 | { | |
1353 | *week = GetWeek(date); | |
1354 | } | |
1355 | else | |
1356 | { | |
1357 | if ( targetmonth < thismonth ) | |
1358 | { | |
1359 | *week = 1; // trivial | |
1360 | } | |
1361 | else // targetmonth > thismonth | |
1362 | { | |
1363 | wxDateTime ldcm; | |
1364 | int lastweek; | |
1365 | int lastday; | |
1366 | ||
1367 | // get the datecoord of the last day in the month currently shown | |
1368 | #if DEBUG_PAINT | |
1369 | wxLogDebug(" +++ LDOM +++"); | |
1370 | #endif | |
1371 | GetDateCoord(ldcm.SetToLastMonthDay(m_date.GetMonth(), m_date.GetYear()), &lastday, &lastweek); | |
1372 | #if DEBUG_PAINT | |
1373 | wxLogDebug(" --- LDOM ---"); | |
1374 | #endif | |
1375 | ||
1376 | wxTimeSpan span = date - ldcm; | |
1377 | ||
1378 | int daysfromlast = span.GetDays(); | |
1379 | #if DEBUG_PAINT | |
1380 | wxLogDebug("daysfromlast: %i", daysfromlast); | |
1381 | #endif | |
1382 | if ( daysfromlast + lastday > 7 ) // past week boundary | |
1383 | { | |
1384 | int wholeweeks = (daysfromlast / 7); | |
1385 | *week = wholeweeks + lastweek; | |
1386 | if ( (daysfromlast - (7 * wholeweeks) + lastday) > 7 ) | |
1387 | { | |
1388 | *week += 1; | |
1389 | } | |
1390 | } | |
1391 | else | |
1392 | { | |
1393 | *week = lastweek; | |
1394 | } | |
1395 | } | |
1396 | } | |
1397 | } | |
1398 | else | |
1399 | { | |
1400 | *day = -1; | |
1401 | *week = -1; | |
ca65c044 | 1402 | retval = false; |
37df1f33 VZ |
1403 | } |
1404 | ||
1405 | #if DEBUG_PAINT | |
1406 | wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date.Format("%d %m %Y"), *day, *week); | |
1407 | #endif | |
1408 | ||
1409 | return retval; | |
1410 | } | |
1411 | ||
2ef31e80 VZ |
1412 | // ---------------------------------------------------------------------------- |
1413 | // mouse handling | |
1414 | // ---------------------------------------------------------------------------- | |
1415 | ||
0185cd09 | 1416 | void wxCalendarCtrl::OnDClick(wxMouseEvent& event) |
2ef31e80 | 1417 | { |
0185cd09 | 1418 | if ( HitTest(event.GetPosition()) != wxCAL_HITTEST_DAY ) |
2ef31e80 VZ |
1419 | { |
1420 | event.Skip(); | |
1421 | } | |
1422 | else | |
1423 | { | |
4f6aed9c | 1424 | GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED); |
0185cd09 VZ |
1425 | } |
1426 | } | |
1427 | ||
1428 | void wxCalendarCtrl::OnClick(wxMouseEvent& event) | |
1429 | { | |
1430 | wxDateTime date; | |
1431 | wxDateTime::WeekDay wday; | |
1432 | switch ( HitTest(event.GetPosition(), &date, &wday) ) | |
1433 | { | |
1434 | case wxCAL_HITTEST_DAY: | |
37df1f33 VZ |
1435 | if ( IsDateInRange(date) ) |
1436 | { | |
1437 | ChangeDay(date); | |
2ef31e80 | 1438 | |
37df1f33 VZ |
1439 | GenerateEvents(wxEVT_CALENDAR_DAY_CHANGED, |
1440 | wxEVT_CALENDAR_SEL_CHANGED); | |
1441 | } | |
0185cd09 VZ |
1442 | break; |
1443 | ||
1444 | case wxCAL_HITTEST_HEADER: | |
1445 | { | |
1446 | wxCalendarEvent event(this, wxEVT_CALENDAR_WEEKDAY_CLICKED); | |
1447 | event.m_wday = wday; | |
1448 | (void)GetEventHandler()->ProcessEvent(event); | |
1449 | } | |
1450 | break; | |
1451 | ||
37df1f33 VZ |
1452 | case wxCAL_HITTEST_DECMONTH: |
1453 | case wxCAL_HITTEST_INCMONTH: | |
1454 | case wxCAL_HITTEST_SURROUNDING_WEEK: | |
1455 | SetDateAndNotify(date); // we probably only want to refresh the control. No notification.. (maybe as an option?) | |
1456 | break; | |
1457 | ||
0185cd09 VZ |
1458 | default: |
1459 | wxFAIL_MSG(_T("unknown hittest code")); | |
1460 | // fall through | |
1461 | ||
1462 | case wxCAL_HITTEST_NOWHERE: | |
1463 | event.Skip(); | |
1464 | break; | |
2ef31e80 VZ |
1465 | } |
1466 | } | |
1467 | ||
0185cd09 VZ |
1468 | wxCalendarHitTestResult wxCalendarCtrl::HitTest(const wxPoint& pos, |
1469 | wxDateTime *date, | |
1470 | wxDateTime::WeekDay *wd) | |
2ef31e80 | 1471 | { |
9d9b7755 VZ |
1472 | RecalcGeometry(); |
1473 | ||
2ef31e80 | 1474 | wxCoord y = pos.y; |
37df1f33 VZ |
1475 | |
1476 | /////////////////////////////////////////////////////////////////////////////////////////////////////// | |
1477 | if ( (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) ) | |
0185cd09 | 1478 | { |
37df1f33 | 1479 | // Header: month |
1bcf0c7d | 1480 | |
37df1f33 VZ |
1481 | // we need to find out if the hit is on left arrow, on month or on right arrow |
1482 | // left arrow? | |
1483 | if ( wxRegion(m_leftArrowRect).Contains(pos) == wxInRegion ) | |
0185cd09 | 1484 | { |
37df1f33 | 1485 | if ( date ) |
0185cd09 | 1486 | { |
37df1f33 VZ |
1487 | if ( IsDateInRange(m_date - wxDateSpan::Month()) ) |
1488 | { | |
1489 | *date = m_date - wxDateSpan::Month(); | |
1490 | } | |
1491 | else | |
1492 | { | |
1493 | *date = GetLowerDateLimit(); | |
1494 | } | |
1495 | } | |
1496 | ||
1497 | return wxCAL_HITTEST_DECMONTH; | |
1498 | } | |
1499 | ||
1500 | if ( wxRegion(m_rightArrowRect).Contains(pos) == wxInRegion ) | |
1501 | { | |
1502 | if ( date ) | |
1503 | { | |
1504 | if ( IsDateInRange(m_date + wxDateSpan::Month()) ) | |
1505 | { | |
1506 | *date = m_date + wxDateSpan::Month(); | |
1507 | } | |
1508 | else | |
1509 | { | |
1510 | *date = GetUpperDateLimit(); | |
1511 | } | |
0185cd09 VZ |
1512 | } |
1513 | ||
37df1f33 | 1514 | return wxCAL_HITTEST_INCMONTH; |
0185cd09 | 1515 | } |
2ef31e80 | 1516 | |
0185cd09 | 1517 | } |
2ef31e80 | 1518 | |
37df1f33 VZ |
1519 | /////////////////////////////////////////////////////////////////////////////////////////////////////// |
1520 | // Header: Days | |
1521 | int wday = pos.x / m_widthCol; | |
1522 | // if ( y < m_heightRow ) | |
1523 | if ( y < (m_heightRow + m_rowOffset) ) | |
1524 | { | |
1525 | if ( y > m_rowOffset ) | |
1526 | { | |
1527 | if ( wd ) | |
1528 | { | |
1529 | if ( GetWindowStyle() & wxCAL_MONDAY_FIRST ) | |
1530 | { | |
1531 | wday = wday == 6 ? 0 : wday + 1; | |
1532 | } | |
1533 | ||
1534 | *wd = (wxDateTime::WeekDay)wday; | |
1535 | } | |
1536 | ||
1537 | return wxCAL_HITTEST_HEADER; | |
1538 | } | |
1539 | else | |
1540 | { | |
1541 | return wxCAL_HITTEST_NOWHERE; | |
1542 | } | |
1543 | } | |
1544 | ||
1545 | // int week = (y - m_heightRow) / m_heightRow; | |
1546 | int week = (y - (m_heightRow + m_rowOffset)) / m_heightRow; | |
2ef31e80 | 1547 | if ( week >= 6 || wday >= 7 ) |
0185cd09 VZ |
1548 | { |
1549 | return wxCAL_HITTEST_NOWHERE; | |
1550 | } | |
2ef31e80 | 1551 | |
0185cd09 | 1552 | wxDateTime dt = GetStartDate() + wxDateSpan::Days(7*week + wday); |
2ef31e80 | 1553 | |
0185cd09 VZ |
1554 | if ( IsDateShown(dt) ) |
1555 | { | |
1556 | if ( date ) | |
1557 | *date = dt; | |
9d9b7755 | 1558 | |
37df1f33 VZ |
1559 | if ( dt.GetMonth() == m_date.GetMonth() ) |
1560 | { | |
1561 | ||
1562 | return wxCAL_HITTEST_DAY; | |
1563 | } | |
1564 | else | |
1565 | { | |
1566 | return wxCAL_HITTEST_SURROUNDING_WEEK; | |
1567 | } | |
0185cd09 VZ |
1568 | } |
1569 | else | |
1570 | { | |
1571 | return wxCAL_HITTEST_NOWHERE; | |
1572 | } | |
2ef31e80 | 1573 | } |
9d9b7755 VZ |
1574 | |
1575 | // ---------------------------------------------------------------------------- | |
1576 | // subcontrols events handling | |
1577 | // ---------------------------------------------------------------------------- | |
1578 | ||
1579 | void wxCalendarCtrl::OnMonthChange(wxCommandEvent& event) | |
1580 | { | |
1581 | wxDateTime::Tm tm = m_date.GetTm(); | |
1582 | ||
1583 | wxDateTime::Month mon = (wxDateTime::Month)event.GetInt(); | |
1584 | if ( tm.mday > wxDateTime::GetNumberOfDays(mon, tm.year) ) | |
1585 | { | |
1586 | tm.mday = wxDateTime::GetNumberOfDays(mon, tm.year); | |
1587 | } | |
1588 | ||
37df1f33 VZ |
1589 | wxDateTime target = wxDateTime(tm.mday, mon, tm.year); |
1590 | ||
1591 | ChangeMonth(&target); | |
1592 | SetDateAndNotify(target); | |
9d9b7755 VZ |
1593 | } |
1594 | ||
f0d5e7a2 | 1595 | void wxCalendarCtrl::OnYearChange(wxCommandEvent& event) |
9d9b7755 | 1596 | { |
f0d5e7a2 VZ |
1597 | int year = (int)event.GetInt(); |
1598 | if ( year == INT_MIN ) | |
1599 | { | |
1600 | // invalid year in the spin control, ignore it | |
1601 | return; | |
1602 | } | |
1603 | ||
9d9b7755 VZ |
1604 | wxDateTime::Tm tm = m_date.GetTm(); |
1605 | ||
9d9b7755 VZ |
1606 | if ( tm.mday > wxDateTime::GetNumberOfDays(tm.mon, year) ) |
1607 | { | |
1608 | tm.mday = wxDateTime::GetNumberOfDays(tm.mon, year); | |
1609 | } | |
1bcf0c7d | 1610 | |
37df1f33 VZ |
1611 | wxDateTime target = wxDateTime(tm.mday, tm.mon, year); |
1612 | ||
1613 | if ( ChangeYear(&target) ) | |
1614 | { | |
1615 | SetDateAndNotify(target); | |
1616 | } | |
1617 | else | |
1618 | { | |
1619 | // In this case we don't want to change the date. That would put us | |
1620 | // inside the same year but a strange number of months forward/back.. | |
1621 | m_spinYear->SetValue(target.GetYear()); | |
1622 | } | |
9d9b7755 VZ |
1623 | } |
1624 | ||
1625 | // ---------------------------------------------------------------------------- | |
1626 | // keyboard interface | |
1627 | // ---------------------------------------------------------------------------- | |
1628 | ||
1629 | void wxCalendarCtrl::OnChar(wxKeyEvent& event) | |
1630 | { | |
37df1f33 | 1631 | wxDateTime target; |
12a3f227 | 1632 | switch ( event.GetKeyCode() ) |
9d9b7755 VZ |
1633 | { |
1634 | case _T('+'): | |
1635 | case WXK_ADD: | |
37df1f33 VZ |
1636 | target = m_date + wxDateSpan::Year(); |
1637 | if ( ChangeYear(&target) ) | |
1638 | { | |
1639 | SetDateAndNotify(target); | |
1640 | } | |
9d9b7755 VZ |
1641 | break; |
1642 | ||
1643 | case _T('-'): | |
1644 | case WXK_SUBTRACT: | |
37df1f33 VZ |
1645 | target = m_date - wxDateSpan::Year(); |
1646 | if ( ChangeYear(&target) ) | |
1647 | { | |
1648 | SetDateAndNotify(target); | |
1649 | } | |
9d9b7755 VZ |
1650 | break; |
1651 | ||
882a8f40 | 1652 | case WXK_PRIOR: |
37df1f33 VZ |
1653 | target = m_date - wxDateSpan::Month(); |
1654 | ChangeMonth(&target); | |
1655 | SetDateAndNotify(target); // always | |
9d9b7755 VZ |
1656 | break; |
1657 | ||
882a8f40 | 1658 | case WXK_NEXT: |
37df1f33 VZ |
1659 | target = m_date + wxDateSpan::Month(); |
1660 | ChangeMonth(&target); | |
1661 | SetDateAndNotify(target); // always | |
9d9b7755 VZ |
1662 | break; |
1663 | ||
1664 | case WXK_RIGHT: | |
1a8557b1 | 1665 | if ( event.ControlDown() ) |
37df1f33 VZ |
1666 | { |
1667 | target = wxDateTime(m_date).SetToNextWeekDay( | |
1a8557b1 | 1668 | GetWindowStyle() & wxCAL_MONDAY_FIRST |
37df1f33 VZ |
1669 | ? wxDateTime::Sun : wxDateTime::Sat); |
1670 | if ( !IsDateInRange(target) ) | |
1671 | { | |
1672 | target = GetUpperDateLimit(); | |
1673 | } | |
1674 | SetDateAndNotify(target); | |
1675 | } | |
1a8557b1 VZ |
1676 | else |
1677 | SetDateAndNotify(m_date + wxDateSpan::Day()); | |
9d9b7755 VZ |
1678 | break; |
1679 | ||
1680 | case WXK_LEFT: | |
1a8557b1 | 1681 | if ( event.ControlDown() ) |
37df1f33 VZ |
1682 | { |
1683 | target = wxDateTime(m_date).SetToPrevWeekDay( | |
1a8557b1 | 1684 | GetWindowStyle() & wxCAL_MONDAY_FIRST |
37df1f33 VZ |
1685 | ? wxDateTime::Mon : wxDateTime::Sun); |
1686 | if ( !IsDateInRange(target) ) | |
1687 | { | |
1688 | target = GetLowerDateLimit(); | |
1689 | } | |
1690 | SetDateAndNotify(target); | |
1691 | } | |
1a8557b1 VZ |
1692 | else |
1693 | SetDateAndNotify(m_date - wxDateSpan::Day()); | |
9d9b7755 VZ |
1694 | break; |
1695 | ||
1696 | case WXK_UP: | |
1697 | SetDateAndNotify(m_date - wxDateSpan::Week()); | |
1698 | break; | |
1699 | ||
1700 | case WXK_DOWN: | |
1701 | SetDateAndNotify(m_date + wxDateSpan::Week()); | |
1702 | break; | |
1703 | ||
1704 | case WXK_HOME: | |
1a8557b1 VZ |
1705 | if ( event.ControlDown() ) |
1706 | SetDateAndNotify(wxDateTime::Today()); | |
1707 | else | |
1708 | SetDateAndNotify(wxDateTime(1, m_date.GetMonth(), m_date.GetYear())); | |
1709 | break; | |
1710 | ||
1711 | case WXK_END: | |
1712 | SetDateAndNotify(wxDateTime(m_date).SetToLastMonthDay()); | |
9d9b7755 VZ |
1713 | break; |
1714 | ||
4f6aed9c VZ |
1715 | case WXK_RETURN: |
1716 | GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED); | |
1717 | break; | |
1718 | ||
9d9b7755 VZ |
1719 | default: |
1720 | event.Skip(); | |
1721 | } | |
1722 | } | |
1723 | ||
1724 | // ---------------------------------------------------------------------------- | |
4f6aed9c | 1725 | // holidays handling |
9d9b7755 VZ |
1726 | // ---------------------------------------------------------------------------- |
1727 | ||
4f6aed9c | 1728 | void wxCalendarCtrl::EnableHolidayDisplay(bool display) |
9d9b7755 | 1729 | { |
4f6aed9c VZ |
1730 | long style = GetWindowStyle(); |
1731 | if ( display ) | |
1732 | style |= wxCAL_SHOW_HOLIDAYS; | |
1733 | else | |
1734 | style &= ~wxCAL_SHOW_HOLIDAYS; | |
1735 | ||
1736 | SetWindowStyle(style); | |
1737 | ||
1738 | if ( display ) | |
1739 | SetHolidayAttrs(); | |
1740 | else | |
1741 | ResetHolidayAttrs(); | |
1742 | ||
1743 | Refresh(); | |
1744 | } | |
1745 | ||
1746 | void wxCalendarCtrl::SetHolidayAttrs() | |
1747 | { | |
1748 | if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS ) | |
1749 | { | |
1750 | ResetHolidayAttrs(); | |
1751 | ||
1752 | wxDateTime::Tm tm = m_date.GetTm(); | |
1753 | wxDateTime dtStart(1, tm.mon, tm.year), | |
1754 | dtEnd = dtStart.GetLastMonthDay(); | |
1755 | ||
1756 | wxDateTimeArray hol; | |
1757 | wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol); | |
1758 | ||
1759 | size_t count = hol.GetCount(); | |
1760 | for ( size_t n = 0; n < count; n++ ) | |
1761 | { | |
1762 | SetHoliday(hol[n].GetDay()); | |
1763 | } | |
1764 | } | |
1765 | } | |
1766 | ||
1767 | void wxCalendarCtrl::SetHoliday(size_t day) | |
1768 | { | |
1769 | wxCHECK_RET( day > 0 && day < 32, _T("invalid day in SetHoliday") ); | |
0185cd09 | 1770 | |
4f6aed9c VZ |
1771 | wxCalendarDateAttr *attr = GetAttr(day); |
1772 | if ( !attr ) | |
0185cd09 | 1773 | { |
4f6aed9c VZ |
1774 | attr = new wxCalendarDateAttr; |
1775 | } | |
0185cd09 | 1776 | |
ca65c044 | 1777 | attr->SetHoliday(true); |
4f6aed9c VZ |
1778 | |
1779 | // can't use SetAttr() because it would delete this pointer | |
1780 | m_attrs[day - 1] = attr; | |
1781 | } | |
1782 | ||
1783 | void wxCalendarCtrl::ResetHolidayAttrs() | |
1784 | { | |
1785 | for ( size_t day = 0; day < 31; day++ ) | |
1786 | { | |
1787 | if ( m_attrs[day] ) | |
1788 | { | |
ca65c044 | 1789 | m_attrs[day]->SetHoliday(false); |
4f6aed9c | 1790 | } |
0185cd09 VZ |
1791 | } |
1792 | } | |
1793 | ||
ab473127 RD |
1794 | |
1795 | //static | |
1796 | wxVisualAttributes | |
1797 | wxCalendarCtrl::GetClassDefaultAttributes(wxWindowVariant variant) | |
1798 | { | |
1799 | // Use the same color scheme as wxListBox | |
1800 | return wxListBox::GetClassDefaultAttributes(variant); | |
1801 | } | |
1802 | ||
1e6feb95 | 1803 | #endif // wxUSE_CALENDARCTRL |
2fa7c206 | 1804 |