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