]> git.saurik.com Git - wxWidgets.git/blame - src/generic/calctrlg.cpp
Patches-1940222 ] wxTextCtrl and keydown on fully selected text (wxMac)
[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);
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,
c3a899dd 792 height = 7*m_heightRow + m_rowOffset;
9d9b7755 793
bb61a983 794 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
37df1f33 795 {
c3a899dd 796 const wxSize bestSizeCombo = m_comboMonth->GetBestSize();
311b8d3d 797
c3a899dd
VZ
798 height += wxMax(bestSizeCombo.y, m_spinYear->GetBestSize().y)
799 + VERT_MARGIN;
37df1f33 800
c3a899dd
VZ
801 wxCoord w2 = bestSizeCombo.x + HORZ_MARGIN + GetCharWidth()*6;
802 if ( width < w2 )
3a39df31 803 width = w2;
311b8d3d 804 }
562e3578 805
c3a899dd 806 wxSize best(width, height);
bb61a983 807 if ( !HasFlag(wxBORDER_NONE) )
bc385ba9 808 {
c3a899dd 809 best += GetWindowBorderSize();
bc385ba9
VZ
810 }
811
ab473127 812 CacheBestSize(best);
2997ca30 813 return best;
2ef31e80
VZ
814}
815
628e155d 816void wxGenericCalendarCtrl::DoMoveWindow(int x, int y, int width, int height)
2ef31e80 817{
bb61a983 818 int yDiff;
2ef31e80 819
0bb4ad85 820 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) && m_staticMonth )
37df1f33 821 {
170acdc9 822 wxSize sizeCombo = m_comboMonth->GetEffectiveMinSize();
37df1f33 823 wxSize sizeStatic = m_staticMonth->GetSize();
95bcc40c 824 wxSize sizeSpin = m_spinYear->GetSize();
028285e4 825
c3a899dd 826 int maxHeight = wxMax(sizeSpin.y, sizeCombo.y);
028285e4 827 int dy = (maxHeight - sizeStatic.y) / 2;
e910c000 828 m_comboMonth->Move(x, y);
028285e4 829 m_staticMonth->SetSize(x, y + dy, sizeCombo.x, -1, sizeStatic.y);
37df1f33 830
e910c000
JS
831 int xDiff = sizeCombo.x + HORZ_MARGIN;
832
c3a899dd 833 m_spinYear->SetSize(x + xDiff, y, width - xDiff, maxHeight);
e910c000 834 m_staticYear->SetSize(x + xDiff, y + dy, width - xDiff, sizeStatic.y);
50807f90 835
c3a899dd 836 yDiff = maxHeight + VERT_MARGIN;
37df1f33 837 }
bb61a983
VZ
838 else // no controls on the top
839 {
840 yDiff = 0;
841 }
9d9b7755
VZ
842
843 wxControl::DoMoveWindow(x, y + yDiff, width, height - yDiff);
844}
845
628e155d 846void wxGenericCalendarCtrl::RecalcGeometry()
2ef31e80 847{
9d9b7755 848 wxClientDC dc(this);
3965571c 849
ab473127 850 dc.SetFont(GetFont());
3965571c 851
f6f735d3
JS
852 // determine the column width (weekday names are not necessarily wider
853 // than the numbers (in some languages), so let's not assume that they are)
2ef31e80 854 m_widthCol = 0;
3a39df31 855 for ( int day = 10; day <= 31; day++)
f6f735d3
JS
856 {
857 wxCoord width;
858 dc.GetTextExtent(wxString::Format(wxT("%d"), day), &width, &m_heightRow);
859 if ( width > m_widthCol )
860 {
861 // 1.5 times the width gives nice margins even if the weekday
862 // names are short
faa94f3e 863 m_widthCol = width+width/2;
f6f735d3
JS
864 }
865 }
9d9b7755 866 wxDateTime::WeekDay wd;
2ef31e80
VZ
867 for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
868 {
2ef31e80 869 wxCoord width;
9d9b7755 870 dc.GetTextExtent(m_weekdays[wd], &width, &m_heightRow);
2ef31e80
VZ
871 if ( width > m_widthCol )
872 {
873 m_widthCol = width;
874 }
875 }
3965571c 876
2ef31e80
VZ
877 // leave some margins
878 m_widthCol += 2;
879 m_heightRow += 2;
37df1f33 880
63403da7 881 m_rowOffset = HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) ? m_heightRow : 0; // conditional in relation to style
9d9b7755
VZ
882}
883
884// ----------------------------------------------------------------------------
885// drawing
886// ----------------------------------------------------------------------------
887
628e155d 888void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
9d9b7755 889{
07e87221 890 wxPaintDC dc(this);
3965571c 891
ab473127 892 dc.SetFont(GetFont());
9d9b7755 893
3965571c 894 RecalcGeometry();
9d9b7755 895
882a8f40 896#if DEBUG_PAINT
f6bcfd97 897 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
9d9b7755
VZ
898 m_date.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
899 GetWeek(m_date));
882a8f40 900#endif
2ef31e80 901
37df1f33 902 wxCoord y = 0;
d9170b47 903 wxCoord x0 = wxMax( (GetSize().x - m_widthCol*7) /2 , 0 );
37df1f33 904
bb61a983 905 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
37df1f33
VZ
906 {
907 // draw the sequential month-selector
908
04ee05f9 909 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
37df1f33 910 dc.SetTextForeground(*wxBLACK);
04ee05f9
PC
911 dc.SetBrush(wxBrush(m_colHeaderBg, wxBRUSHSTYLE_SOLID));
912 dc.SetPen(wxPen(m_colHeaderBg, 1, wxPENSTYLE_SOLID));
311b8d3d 913 dc.DrawRectangle(0, y, GetClientSize().x, m_heightRow);
37df1f33
VZ
914
915 // Get extent of month-name + year
916 wxCoord monthw, monthh;
50d0b061 917 wxString headertext = m_date.Format(wxT("%B %Y"));
37df1f33
VZ
918 dc.GetTextExtent(headertext, &monthw, &monthh);
919
920 // draw month-name centered above weekdays
562e3578 921 wxCoord monthx = ((m_widthCol * 7) - monthw) / 2 + x0;
37df1f33 922 wxCoord monthy = ((m_heightRow - monthh) / 2) + y;
1bcf0c7d 923 dc.DrawText(headertext, monthx, monthy);
37df1f33
VZ
924
925 // calculate the "month-arrows"
926 wxPoint leftarrow[3];
927 wxPoint rightarrow[3];
928
929 int arrowheight = monthh / 2;
1bcf0c7d 930
37df1f33
VZ
931 leftarrow[0] = wxPoint(0, arrowheight / 2);
932 leftarrow[1] = wxPoint(arrowheight / 2, 0);
933 leftarrow[2] = wxPoint(arrowheight / 2, arrowheight - 1);
934
c47addef 935 rightarrow[0] = wxPoint(0,0);
37df1f33
VZ
936 rightarrow[1] = wxPoint(arrowheight / 2, arrowheight / 2);
937 rightarrow[2] = wxPoint(0, arrowheight - 1);
938
939 // draw the "month-arrows"
940
941 wxCoord arrowy = (m_heightRow - arrowheight) / 2;
562e3578
JS
942 wxCoord larrowx = (m_widthCol - (arrowheight / 2)) / 2 + x0;
943 wxCoord rarrowx = ((m_widthCol - (arrowheight / 2)) / 2) + m_widthCol*6 + x0;
c47addef 944 m_leftArrowRect = m_rightArrowRect = wxRect(0,0,0,0);
37df1f33
VZ
945
946 if ( AllowMonthChange() )
947 {
948 wxDateTime ldpm = wxDateTime(1,m_date.GetMonth(), m_date.GetYear()) - wxDateSpan::Day(); // last day prev month
949 // Check if range permits change
ca65c044 950 if ( IsDateInRange(ldpm) && ( ( ldpm.GetYear() == m_date.GetYear() ) ? true : AllowYearChange() ) )
37df1f33
VZ
951 {
952 m_leftArrowRect = wxRect(larrowx - 3, arrowy - 3, (arrowheight / 2) + 8, (arrowheight + 6));
d9170b47
WS
953 dc.SetBrush(*wxBLACK_BRUSH);
954 dc.SetPen(*wxBLACK_PEN);
37df1f33
VZ
955 dc.DrawPolygon(3, leftarrow, larrowx , arrowy, wxWINDING_RULE);
956 dc.SetBrush(*wxTRANSPARENT_BRUSH);
957 dc.DrawRectangle(m_leftArrowRect);
958 }
959 wxDateTime fdnm = wxDateTime(1,m_date.GetMonth(), m_date.GetYear()) + wxDateSpan::Month(); // first day next month
ca65c044 960 if ( IsDateInRange(fdnm) && ( ( fdnm.GetYear() == m_date.GetYear() ) ? true : AllowYearChange() ) )
37df1f33
VZ
961 {
962 m_rightArrowRect = wxRect(rarrowx - 4, arrowy - 3, (arrowheight / 2) + 8, (arrowheight + 6));
d9170b47
WS
963 dc.SetBrush(*wxBLACK_BRUSH);
964 dc.SetPen(*wxBLACK_PEN);
37df1f33
VZ
965 dc.DrawPolygon(3, rightarrow, rarrowx , arrowy, wxWINDING_RULE);
966 dc.SetBrush(*wxTRANSPARENT_BRUSH);
967 dc.DrawRectangle(m_rightArrowRect);
968 }
969 }
970
971 y += m_heightRow;
972 }
973
2ef31e80 974 // first draw the week days
d9170b47 975 if ( IsExposed(x0, y, x0 + 7*m_widthCol, m_heightRow) )
2ef31e80 976 {
882a8f40 977#if DEBUG_PAINT
f6bcfd97 978 wxLogDebug("painting the header");
882a8f40 979#endif
9d9b7755 980
04ee05f9 981 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
4f6aed9c 982 dc.SetTextForeground(m_colHeaderFg);
04ee05f9
PC
983 dc.SetBrush(wxBrush(m_colHeaderBg, wxBRUSHSTYLE_SOLID));
984 dc.SetPen(wxPen(m_colHeaderBg, 1, wxPENSTYLE_SOLID));
bb61a983 985 dc.DrawRectangle(0, y, GetClientSize().x, m_heightRow);
1a8557b1
VZ
986
987 bool startOnMonday = (GetWindowStyle() & wxCAL_MONDAY_FIRST) != 0;
4a10ea8b 988 for ( int wd = 0; wd < 7; wd++ )
9d9b7755 989 {
1a8557b1
VZ
990 size_t n;
991 if ( startOnMonday )
992 n = wd == 6 ? 0 : wd + 1;
993 else
994 n = wd;
37df1f33
VZ
995 wxCoord dayw, dayh;
996 dc.GetTextExtent(m_weekdays[n], &dayw, &dayh);
562e3578 997 dc.DrawText(m_weekdays[n], x0 + (wd*m_widthCol) + ((m_widthCol- dayw) / 2), y); // center the day-name
9d9b7755 998 }
2ef31e80
VZ
999 }
1000
1001 // then the calendar itself
1002 dc.SetTextForeground(*wxBLACK);
1003 //dc.SetFont(*wxNORMAL_FONT);
1004
37df1f33 1005 y += m_heightRow;
2ef31e80 1006 wxDateTime date = GetStartDate();
37df1f33 1007
882a8f40 1008#if DEBUG_PAINT
f6bcfd97 1009 wxLogDebug("starting calendar from %s\n",
9d9b7755 1010 date.Format("%a %d-%m-%Y %H:%M:%S").c_str());
882a8f40 1011#endif
9d9b7755 1012
04ee05f9 1013 dc.SetBackgroundMode(wxBRUSHSTYLE_SOLID);
9d9b7755 1014 for ( size_t nWeek = 1; nWeek <= 6; nWeek++, y += m_heightRow )
2ef31e80 1015 {
9d9b7755 1016 // if the update region doesn't intersect this row, don't paint it
d9170b47 1017 if ( !IsExposed(x0, y, x0 + 7*m_widthCol, m_heightRow - 1) )
9d9b7755
VZ
1018 {
1019 date += wxDateSpan::Week();
1020
1021 continue;
1022 }
882a8f40 1023
1a8557b1 1024#if DEBUG_PAINT
f6bcfd97 1025 wxLogDebug("painting week %d at y = %d\n", nWeek, y);
882a8f40 1026#endif
9d9b7755 1027
4a10ea8b 1028 for ( int wd = 0; wd < 7; wd++ )
2ef31e80 1029 {
d9170b47 1030 dc.SetTextBackground(m_colBackground);
2ef31e80
VZ
1031 if ( IsDateShown(date) )
1032 {
882a8f40 1033 // don't use wxDate::Format() which prepends 0s
4f6aed9c
VZ
1034 unsigned int day = date.GetDay();
1035 wxString dayStr = wxString::Format(_T("%u"), day);
3965571c 1036 wxCoord width;
4f6aed9c
VZ
1037 dc.GetTextExtent(dayStr, &width, (wxCoord *)NULL);
1038
ca65c044
WS
1039 bool changedColours = false,
1040 changedFont = false;
4f6aed9c 1041
ca65c044 1042 bool isSel = false;
37df1f33 1043 wxCalendarDateAttr *attr = NULL;
2ef31e80 1044
37df1f33 1045 if ( date.GetMonth() != m_date.GetMonth() || !IsDateInRange(date) )
2ef31e80 1046 {
ddf13afc
VZ
1047 // draw the days of adjacent months in different colour
1048 dc.SetTextForeground(m_colSurrounding);
ca65c044 1049 changedColours = true;
4f6aed9c 1050 }
37df1f33 1051 else
4f6aed9c 1052 {
37df1f33
VZ
1053 isSel = date.IsSameDate(m_date);
1054 attr = m_attrs[day - 1];
4f6aed9c 1055
37df1f33 1056 if ( isSel )
4f6aed9c 1057 {
37df1f33
VZ
1058 dc.SetTextForeground(m_colHighlightFg);
1059 dc.SetTextBackground(m_colHighlightBg);
4f6aed9c 1060
ca65c044 1061 changedColours = true;
4f6aed9c 1062 }
37df1f33 1063 else if ( attr )
4f6aed9c 1064 {
37df1f33
VZ
1065 wxColour colFg, colBg;
1066
1067 if ( attr->IsHoliday() )
1068 {
1069 colFg = m_colHolidayFg;
1070 colBg = m_colHolidayBg;
1071 }
1072 else
1073 {
1074 colFg = attr->GetTextColour();
1075 colBg = attr->GetBackgroundColour();
1076 }
1077
1078 if ( colFg.Ok() )
1079 {
1080 dc.SetTextForeground(colFg);
ca65c044 1081 changedColours = true;
37df1f33
VZ
1082 }
1083
1084 if ( colBg.Ok() )
1085 {
1086 dc.SetTextBackground(colBg);
ca65c044 1087 changedColours = true;
37df1f33
VZ
1088 }
1089
1090 if ( attr->HasFont() )
1091 {
1092 dc.SetFont(attr->GetFont());
ca65c044 1093 changedFont = true;
37df1f33 1094 }
4f6aed9c 1095 }
2ef31e80
VZ
1096 }
1097
562e3578 1098 wxCoord x = wd*m_widthCol + (m_widthCol - width) / 2 + x0;
4f6aed9c 1099 dc.DrawText(dayStr, x, y + 1);
2ef31e80 1100
4f6aed9c
VZ
1101 if ( !isSel && attr && attr->HasBorder() )
1102 {
1103 wxColour colBorder;
1104 if ( attr->HasBorderColour() )
1105 {
1106 colBorder = attr->GetBorderColour();
1107 }
1108 else
1109 {
ab473127 1110 colBorder = GetForegroundColour();
4f6aed9c
VZ
1111 }
1112
04ee05f9 1113 wxPen pen(colBorder, 1, wxPENSTYLE_SOLID);
4f6aed9c
VZ
1114 dc.SetPen(pen);
1115 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1116
1117 switch ( attr->GetBorder() )
1118 {
1119 case wxCAL_BORDER_SQUARE:
1120 dc.DrawRectangle(x - 2, y,
1121 width + 4, m_heightRow);
1122 break;
1123
1124 case wxCAL_BORDER_ROUND:
1125 dc.DrawEllipse(x - 2, y,
1126 width + 4, m_heightRow);
1127 break;
1128
1129 default:
1130 wxFAIL_MSG(_T("unknown border type"));
1131 }
1132 }
1133
1134 if ( changedColours )
2ef31e80 1135 {
ab473127
RD
1136 dc.SetTextForeground(GetForegroundColour());
1137 dc.SetTextBackground(GetBackgroundColour());
2ef31e80 1138 }
4f6aed9c
VZ
1139
1140 if ( changedFont )
1141 {
ab473127 1142 dc.SetFont(GetFont());
4f6aed9c 1143 }
2ef31e80
VZ
1144 }
1145 //else: just don't draw it
1146
1147 date += wxDateSpan::Day();
1148 }
2ef31e80 1149 }
37df1f33
VZ
1150
1151 // Greying out out-of-range background
1152 bool showSurrounding = (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS) != 0;
1153
1154 date = ( showSurrounding ) ? GetStartDate() : wxDateTime(1, m_date.GetMonth(), m_date.GetYear());
1155 if ( !IsDateInRange(date) )
1156 {
1157 wxDateTime firstOOR = GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1158
1159 wxBrush oorbrush = *wxLIGHT_GREY_BRUSH;
04ee05f9 1160 oorbrush.SetStyle(wxBRUSHSTYLE_FDIAGONAL_HATCH);
37df1f33
VZ
1161
1162 HighlightRange(&dc, date, firstOOR, wxTRANSPARENT_PEN, &oorbrush);
1163 }
1164
1165 date = ( showSurrounding ) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date.GetMonth(), m_date.GetYear());
1166 if ( !IsDateInRange(date) )
1167 {
1168 wxDateTime firstOOR = GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1bcf0c7d 1169
37df1f33 1170 wxBrush oorbrush = *wxLIGHT_GREY_BRUSH;
04ee05f9 1171 oorbrush.SetStyle(wxBRUSHSTYLE_FDIAGONAL_HATCH);
37df1f33
VZ
1172
1173 HighlightRange(&dc, firstOOR, date, wxTRANSPARENT_PEN, &oorbrush);
1174 }
1175
882a8f40 1176#if DEBUG_PAINT
f6bcfd97 1177 wxLogDebug("+++ finished painting");
882a8f40 1178#endif
9d9b7755
VZ
1179}
1180
628e155d 1181void wxGenericCalendarCtrl::RefreshDate(const wxDateTime& date)
9d9b7755
VZ
1182{
1183 RecalcGeometry();
1184
1185 wxRect rect;
1186
1187 // always refresh the whole row at once because our OnPaint() will draw
1188 // the whole row anyhow - and this allows the small optimisation in
1189 // OnClick() below to work
d9170b47 1190 rect.x = wxMax( (GetSize().x - m_widthCol*7) /2 , 0 );
37df1f33
VZ
1191
1192 rect.y = (m_heightRow * GetWeek(date)) + m_rowOffset;
1193
9d9b7755
VZ
1194 rect.width = 7*m_widthCol;
1195 rect.height = m_heightRow;
1196
f6bcfd97
BP
1197#ifdef __WXMSW__
1198 // VZ: for some reason, the selected date seems to occupy more space under
1199 // MSW - this is probably some bug in the font size calculations, but I
1200 // don't know where exactly. This fix is ugly and leads to more
1201 // refreshes than really needed, but without it the selected days
1202 // leaves even more ugly underscores on screen.
1203 rect.Inflate(0, 1);
1204#endif // MSW
1205
882a8f40 1206#if DEBUG_PAINT
f6bcfd97 1207 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
9d9b7755
VZ
1208 GetWeek(date),
1209 rect.x, rect.y,
1210 rect.x + rect.width, rect.y + rect.height);
882a8f40 1211#endif
9d9b7755 1212
ca65c044 1213 Refresh(true, &rect);
2ef31e80
VZ
1214}
1215
628e155d 1216void wxGenericCalendarCtrl::HighlightRange(wxPaintDC* pDC, const wxDateTime& fromdate, const wxDateTime& todate, const wxPen* pPen, const wxBrush* pBrush)
37df1f33
VZ
1217{
1218 // Highlights the given range using pen and brush
1219 // Does nothing if todate < fromdate
1220
1221
1222#if DEBUG_PAINT
1223 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate.Format("%d %m %Y"), todate.Format("%d %m %Y"));
1224#endif
1225
1226 if ( todate >= fromdate )
1227 {
1228 // do stuff
1229 // date-coordinates
1bcf0c7d 1230 int fd, fw;
37df1f33
VZ
1231 int td, tw;
1232
1233 // implicit: both dates must be currently shown - checked by GetDateCoord
1234 if ( GetDateCoord(fromdate, &fd, &fw) && GetDateCoord(todate, &td, &tw) )
1235 {
1236#if DEBUG_PAINT
1237 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd, fw, td, tw);
1238#endif
1239 if ( ( (tw - fw) == 1 ) && ( td < fd ) )
1240 {
1241 // special case: interval 7 days or less not in same week
3103e8a9 1242 // split in two separate intervals
37df1f33
VZ
1243 wxDateTime tfd = fromdate + wxDateSpan::Days(7-fd);
1244 wxDateTime ftd = tfd + wxDateSpan::Day();
1245#if DEBUG_PAINT
3103e8a9 1246 wxLogDebug("Highlight: Separate segments");
37df1f33 1247#endif
3103e8a9 1248 // draw separately
37df1f33
VZ
1249 HighlightRange(pDC, fromdate, tfd, pPen, pBrush);
1250 HighlightRange(pDC, ftd, todate, pPen, pBrush);
1251 }
1252 else
1253 {
1254 int numpoints;
1255 wxPoint corners[8]; // potentially 8 corners in polygon
d9170b47 1256 wxCoord x0 = wxMax( (GetSize().x - m_widthCol*7) /2 , 0 );
37df1f33
VZ
1257
1258 if ( fw == tw )
1259 {
1260 // simple case: same week
1261 numpoints = 4;
d9170b47
WS
1262 corners[0] = wxPoint(x0 + (fd - 1) * m_widthCol, (fw * m_heightRow) + m_rowOffset);
1263 corners[1] = wxPoint(x0 + (fd - 1) * m_widthCol, ((fw + 1 ) * m_heightRow) + m_rowOffset);
1264 corners[2] = wxPoint(x0 + td * m_widthCol, ((tw + 1) * m_heightRow) + m_rowOffset);
1265 corners[3] = wxPoint(x0 + td * m_widthCol, (tw * m_heightRow) + m_rowOffset);
37df1f33
VZ
1266 }
1267 else
1268 {
1269 int cidx = 0;
1270 // "complex" polygon
d9170b47 1271 corners[cidx] = wxPoint(x0 + (fd - 1) * m_widthCol, (fw * m_heightRow) + m_rowOffset); cidx++;
1bcf0c7d 1272
37df1f33
VZ
1273 if ( fd > 1 )
1274 {
d9170b47
WS
1275 corners[cidx] = wxPoint(x0 + (fd - 1) * m_widthCol, ((fw + 1) * m_heightRow) + m_rowOffset); cidx++;
1276 corners[cidx] = wxPoint(x0, ((fw + 1) * m_heightRow) + m_rowOffset); cidx++;
37df1f33
VZ
1277 }
1278
d9170b47
WS
1279 corners[cidx] = wxPoint(x0, ((tw + 1) * m_heightRow) + m_rowOffset); cidx++;
1280 corners[cidx] = wxPoint(x0 + td * m_widthCol, ((tw + 1) * m_heightRow) + m_rowOffset); cidx++;
37df1f33
VZ
1281
1282 if ( td < 7 )
1283 {
d9170b47
WS
1284 corners[cidx] = wxPoint(x0 + td * m_widthCol, (tw * m_heightRow) + m_rowOffset); cidx++;
1285 corners[cidx] = wxPoint(x0 + 7 * m_widthCol, (tw * m_heightRow) + m_rowOffset); cidx++;
37df1f33
VZ
1286 }
1287
d9170b47 1288 corners[cidx] = wxPoint(x0 + 7 * m_widthCol, (fw * m_heightRow) + m_rowOffset); cidx++;
1bcf0c7d 1289
37df1f33
VZ
1290 numpoints = cidx;
1291 }
1292
1293 // draw the polygon
1294 pDC->SetBrush(*pBrush);
1295 pDC->SetPen(*pPen);
1296 pDC->DrawPolygon(numpoints, corners);
1297 }
1298 }
1299 }
1300 // else do nothing
1301#if DEBUG_PAINT
1302 wxLogDebug("--- HighlightRange ---");
1303#endif
1304}
1305
628e155d 1306bool wxGenericCalendarCtrl::GetDateCoord(const wxDateTime& date, int *day, int *week) const
37df1f33 1307{
ca65c044 1308 bool retval = true;
37df1f33
VZ
1309
1310#if DEBUG_PAINT
1311 wxLogDebug("+++ GetDateCoord: (%s) +++", date.Format("%d %m %Y"));
1312#endif
1313
1314 if ( IsDateShown(date) )
1315 {
1316 bool startOnMonday = ( GetWindowStyle() & wxCAL_MONDAY_FIRST ) != 0;
1bcf0c7d 1317
37df1f33
VZ
1318 // Find day
1319 *day = date.GetWeekDay();
1bcf0c7d 1320
37df1f33
VZ
1321 if ( *day == 0 ) // sunday
1322 {
1323 *day = ( startOnMonday ) ? 7 : 1;
1324 }
1325 else
1326 {
999836aa 1327 *day += ( startOnMonday ) ? 0 : 1;
37df1f33
VZ
1328 }
1329
1330 int targetmonth = date.GetMonth() + (12 * date.GetYear());
1331 int thismonth = m_date.GetMonth() + (12 * m_date.GetYear());
1332
1333 // Find week
1334 if ( targetmonth == thismonth )
1335 {
1336 *week = GetWeek(date);
1337 }
1338 else
1339 {
1340 if ( targetmonth < thismonth )
1341 {
1342 *week = 1; // trivial
1343 }
1344 else // targetmonth > thismonth
1345 {
1346 wxDateTime ldcm;
1347 int lastweek;
1348 int lastday;
1349
1350 // get the datecoord of the last day in the month currently shown
1351#if DEBUG_PAINT
1352 wxLogDebug(" +++ LDOM +++");
1353#endif
1354 GetDateCoord(ldcm.SetToLastMonthDay(m_date.GetMonth(), m_date.GetYear()), &lastday, &lastweek);
1355#if DEBUG_PAINT
1356 wxLogDebug(" --- LDOM ---");
1357#endif
1358
1359 wxTimeSpan span = date - ldcm;
1360
1361 int daysfromlast = span.GetDays();
1362#if DEBUG_PAINT
1363 wxLogDebug("daysfromlast: %i", daysfromlast);
1364#endif
1365 if ( daysfromlast + lastday > 7 ) // past week boundary
1366 {
1367 int wholeweeks = (daysfromlast / 7);
1368 *week = wholeweeks + lastweek;
1369 if ( (daysfromlast - (7 * wholeweeks) + lastday) > 7 )
1370 {
1371 *week += 1;
1372 }
1373 }
1374 else
1375 {
1376 *week = lastweek;
1377 }
1378 }
1379 }
1380 }
1381 else
1382 {
1383 *day = -1;
1384 *week = -1;
ca65c044 1385 retval = false;
37df1f33
VZ
1386 }
1387
1388#if DEBUG_PAINT
1389 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date.Format("%d %m %Y"), *day, *week);
1390#endif
1391
1392 return retval;
1393}
1394
2ef31e80
VZ
1395// ----------------------------------------------------------------------------
1396// mouse handling
1397// ----------------------------------------------------------------------------
1398
628e155d 1399void wxGenericCalendarCtrl::OnDClick(wxMouseEvent& event)
2ef31e80 1400{
0185cd09 1401 if ( HitTest(event.GetPosition()) != wxCAL_HITTEST_DAY )
2ef31e80
VZ
1402 {
1403 event.Skip();
1404 }
1405 else
1406 {
4f6aed9c 1407 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED);
0185cd09
VZ
1408 }
1409}
1410
628e155d 1411void wxGenericCalendarCtrl::OnClick(wxMouseEvent& event)
0185cd09
VZ
1412{
1413 wxDateTime date;
1414 wxDateTime::WeekDay wday;
1415 switch ( HitTest(event.GetPosition(), &date, &wday) )
1416 {
1417 case wxCAL_HITTEST_DAY:
37df1f33
VZ
1418 if ( IsDateInRange(date) )
1419 {
1420 ChangeDay(date);
2ef31e80 1421
628e155d 1422 GenerateEvent(wxEVT_CALENDAR_SEL_CHANGED);
a4fcd589
VZ
1423
1424 // we know that the month/year never change when the user
1425 // clicks on the control so there is no need to call
1426 // GenerateAllChangeEvents() here, we know which event to send
628e155d 1427 GenerateEvent(wxEVT_CALENDAR_DAY_CHANGED);
37df1f33 1428 }
0185cd09
VZ
1429 break;
1430
1431 case wxCAL_HITTEST_HEADER:
1432 {
628e155d
VZ
1433 wxCalendarEvent eventWd(this, GetDate(),
1434 wxEVT_CALENDAR_WEEKDAY_CLICKED);
1435 eventWd.SetWeekDay(wday);
4e115ed2 1436 (void)GetEventHandler()->ProcessEvent(eventWd);
0185cd09
VZ
1437 }
1438 break;
1439
37df1f33
VZ
1440 case wxCAL_HITTEST_DECMONTH:
1441 case wxCAL_HITTEST_INCMONTH:
1442 case wxCAL_HITTEST_SURROUNDING_WEEK:
1443 SetDateAndNotify(date); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1444 break;
1445
0185cd09
VZ
1446 default:
1447 wxFAIL_MSG(_T("unknown hittest code"));
1448 // fall through
1449
1450 case wxCAL_HITTEST_NOWHERE:
1451 event.Skip();
1452 break;
2ef31e80 1453 }
3ccd1b49
VZ
1454
1455 // as we don't (always) skip the message, we're not going to receive the
1456 // focus on click by default if we don't do it ourselves
1457 SetFocus();
2ef31e80
VZ
1458}
1459
628e155d 1460wxCalendarHitTestResult wxGenericCalendarCtrl::HitTest(const wxPoint& pos,
0185cd09
VZ
1461 wxDateTime *date,
1462 wxDateTime::WeekDay *wd)
2ef31e80 1463{
9d9b7755
VZ
1464 RecalcGeometry();
1465
afcdd052
VZ
1466 // the position where the calendar really begins
1467 wxCoord x0 = wxMax((GetSize().x - m_widthCol*7)/2, 0);
37df1f33 1468
afcdd052 1469 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
0185cd09 1470 {
37df1f33 1471 // Header: month
1bcf0c7d 1472
37df1f33
VZ
1473 // we need to find out if the hit is on left arrow, on month or on right arrow
1474 // left arrow?
afcdd052 1475 if ( m_leftArrowRect.Contains(pos) )
0185cd09 1476 {
37df1f33 1477 if ( date )
0185cd09 1478 {
37df1f33
VZ
1479 if ( IsDateInRange(m_date - wxDateSpan::Month()) )
1480 {
1481 *date = m_date - wxDateSpan::Month();
1482 }
1483 else
1484 {
1485 *date = GetLowerDateLimit();
1486 }
1487 }
1488
1489 return wxCAL_HITTEST_DECMONTH;
1490 }
1491
afcdd052 1492 if ( m_rightArrowRect.Contains(pos) )
37df1f33
VZ
1493 {
1494 if ( date )
1495 {
1496 if ( IsDateInRange(m_date + wxDateSpan::Month()) )
1497 {
1498 *date = m_date + wxDateSpan::Month();
1499 }
1500 else
1501 {
1502 *date = GetUpperDateLimit();
1503 }
0185cd09
VZ
1504 }
1505
37df1f33 1506 return wxCAL_HITTEST_INCMONTH;
0185cd09 1507 }
2ef31e80 1508
0185cd09 1509 }
2ef31e80 1510
afcdd052
VZ
1511 // header: week days
1512 int wday = (pos.x - x0) / m_widthCol;
1513 if ( pos.y < (m_heightRow + m_rowOffset) )
37df1f33 1514 {
afcdd052 1515 if ( pos.y > m_rowOffset )
37df1f33
VZ
1516 {
1517 if ( wd )
1518 {
1519 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST )
1520 {
1521 wday = wday == 6 ? 0 : wday + 1;
1522 }
1523
1524 *wd = (wxDateTime::WeekDay)wday;
1525 }
1526
1527 return wxCAL_HITTEST_HEADER;
1528 }
1529 else
1530 {
1531 return wxCAL_HITTEST_NOWHERE;
1532 }
1533 }
1534
afcdd052 1535 int week = (pos.y - (m_heightRow + m_rowOffset)) / m_heightRow;
2ef31e80 1536 if ( week >= 6 || wday >= 7 )
0185cd09
VZ
1537 {
1538 return wxCAL_HITTEST_NOWHERE;
1539 }
2ef31e80 1540
0185cd09 1541 wxDateTime dt = GetStartDate() + wxDateSpan::Days(7*week + wday);
2ef31e80 1542
0185cd09
VZ
1543 if ( IsDateShown(dt) )
1544 {
1545 if ( date )
1546 *date = dt;
9d9b7755 1547
37df1f33
VZ
1548 if ( dt.GetMonth() == m_date.GetMonth() )
1549 {
1550
1551 return wxCAL_HITTEST_DAY;
1552 }
1553 else
1554 {
1555 return wxCAL_HITTEST_SURROUNDING_WEEK;
1556 }
0185cd09
VZ
1557 }
1558 else
1559 {
1560 return wxCAL_HITTEST_NOWHERE;
1561 }
2ef31e80 1562}
9d9b7755
VZ
1563
1564// ----------------------------------------------------------------------------
1565// subcontrols events handling
1566// ----------------------------------------------------------------------------
1567
628e155d 1568void wxGenericCalendarCtrl::OnMonthChange(wxCommandEvent& event)
9d9b7755
VZ
1569{
1570 wxDateTime::Tm tm = m_date.GetTm();
1571
1572 wxDateTime::Month mon = (wxDateTime::Month)event.GetInt();
1573 if ( tm.mday > wxDateTime::GetNumberOfDays(mon, tm.year) )
1574 {
1575 tm.mday = wxDateTime::GetNumberOfDays(mon, tm.year);
1576 }
1577
37df1f33
VZ
1578 wxDateTime target = wxDateTime(tm.mday, mon, tm.year);
1579
1580 ChangeMonth(&target);
1581 SetDateAndNotify(target);
9d9b7755
VZ
1582}
1583
628e155d 1584void wxGenericCalendarCtrl::OnYearChange(wxCommandEvent& event)
9d9b7755 1585{
f0d5e7a2
VZ
1586 int year = (int)event.GetInt();
1587 if ( year == INT_MIN )
1588 {
1589 // invalid year in the spin control, ignore it
1590 return;
1591 }
1592
9d9b7755
VZ
1593 wxDateTime::Tm tm = m_date.GetTm();
1594
9d9b7755
VZ
1595 if ( tm.mday > wxDateTime::GetNumberOfDays(tm.mon, year) )
1596 {
1597 tm.mday = wxDateTime::GetNumberOfDays(tm.mon, year);
1598 }
1bcf0c7d 1599
37df1f33
VZ
1600 wxDateTime target = wxDateTime(tm.mday, tm.mon, year);
1601
1602 if ( ChangeYear(&target) )
1603 {
1604 SetDateAndNotify(target);
1605 }
1606 else
1607 {
1608 // In this case we don't want to change the date. That would put us
1609 // inside the same year but a strange number of months forward/back..
1610 m_spinYear->SetValue(target.GetYear());
1611 }
9d9b7755
VZ
1612}
1613
628e155d 1614void wxGenericCalendarCtrl::OnYearTextChange(wxCommandEvent& event)
61581d48
MW
1615{
1616 SetUserChangedYear();
1617 OnYearChange(event);
1618}
1619
d9170b47 1620// Responds to colour changes, and passes event on to children.
628e155d 1621void wxGenericCalendarCtrl::OnSysColourChanged(wxSysColourChangedEvent& event)
d9170b47
WS
1622{
1623 // reinit colours
1624 InitColours();
1625
1626 // Propagate the event to the children
1627 wxControl::OnSysColourChanged(event);
1628
1629 // Redraw control area
1630 SetBackgroundColour(m_colBackground);
1631 Refresh();
1632}
1633
9d9b7755
VZ
1634// ----------------------------------------------------------------------------
1635// keyboard interface
1636// ----------------------------------------------------------------------------
1637
628e155d 1638void wxGenericCalendarCtrl::OnChar(wxKeyEvent& event)
9d9b7755 1639{
37df1f33 1640 wxDateTime target;
12a3f227 1641 switch ( event.GetKeyCode() )
9d9b7755
VZ
1642 {
1643 case _T('+'):
1644 case WXK_ADD:
37df1f33
VZ
1645 target = m_date + wxDateSpan::Year();
1646 if ( ChangeYear(&target) )
1647 {
1648 SetDateAndNotify(target);
1649 }
9d9b7755
VZ
1650 break;
1651
1652 case _T('-'):
1653 case WXK_SUBTRACT:
37df1f33
VZ
1654 target = m_date - wxDateSpan::Year();
1655 if ( ChangeYear(&target) )
1656 {
1657 SetDateAndNotify(target);
1658 }
9d9b7755
VZ
1659 break;
1660
faa94f3e 1661 case WXK_PAGEUP:
37df1f33
VZ
1662 target = m_date - wxDateSpan::Month();
1663 ChangeMonth(&target);
1664 SetDateAndNotify(target); // always
9d9b7755
VZ
1665 break;
1666
faa94f3e 1667 case WXK_PAGEDOWN:
37df1f33
VZ
1668 target = m_date + wxDateSpan::Month();
1669 ChangeMonth(&target);
1670 SetDateAndNotify(target); // always
9d9b7755
VZ
1671 break;
1672
1673 case WXK_RIGHT:
1a8557b1 1674 if ( event.ControlDown() )
37df1f33
VZ
1675 {
1676 target = wxDateTime(m_date).SetToNextWeekDay(
1a8557b1 1677 GetWindowStyle() & wxCAL_MONDAY_FIRST
37df1f33
VZ
1678 ? wxDateTime::Sun : wxDateTime::Sat);
1679 if ( !IsDateInRange(target) )
1680 {
1681 target = GetUpperDateLimit();
1682 }
1683 SetDateAndNotify(target);
1684 }
1a8557b1
VZ
1685 else
1686 SetDateAndNotify(m_date + wxDateSpan::Day());
9d9b7755
VZ
1687 break;
1688
1689 case WXK_LEFT:
1a8557b1 1690 if ( event.ControlDown() )
37df1f33
VZ
1691 {
1692 target = wxDateTime(m_date).SetToPrevWeekDay(
1a8557b1 1693 GetWindowStyle() & wxCAL_MONDAY_FIRST
37df1f33
VZ
1694 ? wxDateTime::Mon : wxDateTime::Sun);
1695 if ( !IsDateInRange(target) )
1696 {
1697 target = GetLowerDateLimit();
1698 }
1699 SetDateAndNotify(target);
1700 }
1a8557b1
VZ
1701 else
1702 SetDateAndNotify(m_date - wxDateSpan::Day());
9d9b7755
VZ
1703 break;
1704
1705 case WXK_UP:
1706 SetDateAndNotify(m_date - wxDateSpan::Week());
1707 break;
1708
1709 case WXK_DOWN:
1710 SetDateAndNotify(m_date + wxDateSpan::Week());
1711 break;
1712
1713 case WXK_HOME:
1a8557b1
VZ
1714 if ( event.ControlDown() )
1715 SetDateAndNotify(wxDateTime::Today());
1716 else
1717 SetDateAndNotify(wxDateTime(1, m_date.GetMonth(), m_date.GetYear()));
1718 break;
1719
1720 case WXK_END:
1721 SetDateAndNotify(wxDateTime(m_date).SetToLastMonthDay());
9d9b7755
VZ
1722 break;
1723
4f6aed9c
VZ
1724 case WXK_RETURN:
1725 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED);
1726 break;
1727
9d9b7755
VZ
1728 default:
1729 event.Skip();
1730 }
1731}
1732
1733// ----------------------------------------------------------------------------
4f6aed9c 1734// holidays handling
9d9b7755
VZ
1735// ----------------------------------------------------------------------------
1736
628e155d 1737void wxGenericCalendarCtrl::EnableHolidayDisplay(bool display)
9d9b7755 1738{
4f6aed9c
VZ
1739 long style = GetWindowStyle();
1740 if ( display )
1741 style |= wxCAL_SHOW_HOLIDAYS;
1742 else
1743 style &= ~wxCAL_SHOW_HOLIDAYS;
1744
1745 SetWindowStyle(style);
1746
1747 if ( display )
1748 SetHolidayAttrs();
1749 else
1750 ResetHolidayAttrs();
1751
1752 Refresh();
1753}
1754
628e155d 1755void wxGenericCalendarCtrl::SetHolidayAttrs()
4f6aed9c
VZ
1756{
1757 if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS )
1758 {
1759 ResetHolidayAttrs();
1760
1761 wxDateTime::Tm tm = m_date.GetTm();
1762 wxDateTime dtStart(1, tm.mon, tm.year),
1763 dtEnd = dtStart.GetLastMonthDay();
1764
1765 wxDateTimeArray hol;
1766 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol);
1767
1768 size_t count = hol.GetCount();
1769 for ( size_t n = 0; n < count; n++ )
1770 {
1771 SetHoliday(hol[n].GetDay());
1772 }
1773 }
1774}
1775
628e155d 1776void wxGenericCalendarCtrl::SetHoliday(size_t day)
4f6aed9c
VZ
1777{
1778 wxCHECK_RET( day > 0 && day < 32, _T("invalid day in SetHoliday") );
0185cd09 1779
4f6aed9c
VZ
1780 wxCalendarDateAttr *attr = GetAttr(day);
1781 if ( !attr )
0185cd09 1782 {
4f6aed9c
VZ
1783 attr = new wxCalendarDateAttr;
1784 }
0185cd09 1785
ca65c044 1786 attr->SetHoliday(true);
4f6aed9c
VZ
1787
1788 // can't use SetAttr() because it would delete this pointer
1789 m_attrs[day - 1] = attr;
1790}
1791
628e155d 1792void wxGenericCalendarCtrl::ResetHolidayAttrs()
4f6aed9c
VZ
1793{
1794 for ( size_t day = 0; day < 31; day++ )
1795 {
1796 if ( m_attrs[day] )
1797 {
ca65c044 1798 m_attrs[day]->SetHoliday(false);
4f6aed9c 1799 }
0185cd09
VZ
1800 }
1801}
1802
628e155d
VZ
1803void wxGenericCalendarCtrl::Mark(size_t day, bool mark)
1804{
1805 wxCHECK_RET( day > 0 && day < 32, _T("invalid day in Mark") );
1806
1807 const wxCalendarDateAttr& m = wxCalendarDateAttr::GetMark();
1808 if (mark) {
1809 if ( m_attrs[day - 1] )
1810 AddAttr(m_attrs[day - 1], m);
1811 else
1812 SetAttr(day, new wxCalendarDateAttr(m));
1813 }
1814 else {
1815 if ( m_attrs[day - 1] )
1816 DelAttr(m_attrs[day - 1], m);
1817 }
1818}
ab473127
RD
1819
1820//static
1821wxVisualAttributes
628e155d 1822wxGenericCalendarCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
ab473127
RD
1823{
1824 // Use the same color scheme as wxListBox
1825 return wxListBox::GetClassDefaultAttributes(variant);
1826}
1827
1e6feb95 1828#endif // wxUSE_CALENDARCTRL