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