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