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