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