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