1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/calctrl.cpp
3 // Purpose: implementation fo the generic wxCalendarCtrl
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/dcclient.h"
29 #include "wx/settings.h"
31 #include "wx/combobox.h"
32 #include "wx/listbox.h"
33 #include "wx/stattext.h"
34 #include "wx/textctrl.h"
37 #if wxUSE_CALENDARCTRL
39 #include "wx/spinctrl.h"
41 // if wxDatePickerCtrl code doesn't define the date event, do it here as we
43 #if !wxUSE_DATEPICKCTRL
44 #define _WX_DEFINE_DATE_EVENTS_
47 #include "wx/calctrl.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 BEGIN_EVENT_TABLE(wxCalendarCtrl
, wxControl
)
56 EVT_PAINT(wxCalendarCtrl::OnPaint
)
58 EVT_CHAR(wxCalendarCtrl::OnChar
)
60 EVT_LEFT_DOWN(wxCalendarCtrl::OnClick
)
61 EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick
)
64 #if wxUSE_EXTENDED_RTTI
65 WX_DEFINE_FLAGS( wxCalendarCtrlStyle
)
67 wxBEGIN_FLAGS( wxCalendarCtrlStyle
)
68 // new style border flags, we put them first to
69 // use them for streaming out
70 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
71 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
72 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
73 wxFLAGS_MEMBER(wxBORDER_RAISED
)
74 wxFLAGS_MEMBER(wxBORDER_STATIC
)
75 wxFLAGS_MEMBER(wxBORDER_NONE
)
77 // old style border flags
78 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
79 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
80 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
81 wxFLAGS_MEMBER(wxRAISED_BORDER
)
82 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
83 wxFLAGS_MEMBER(wxBORDER
)
85 // standard window styles
86 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
87 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
88 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
89 wxFLAGS_MEMBER(wxWANTS_CHARS
)
90 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
91 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
92 wxFLAGS_MEMBER(wxVSCROLL
)
93 wxFLAGS_MEMBER(wxHSCROLL
)
95 wxFLAGS_MEMBER(wxCAL_SUNDAY_FIRST
)
96 wxFLAGS_MEMBER(wxCAL_MONDAY_FIRST
)
97 wxFLAGS_MEMBER(wxCAL_SHOW_HOLIDAYS
)
98 wxFLAGS_MEMBER(wxCAL_NO_YEAR_CHANGE
)
99 wxFLAGS_MEMBER(wxCAL_NO_MONTH_CHANGE
)
100 wxFLAGS_MEMBER(wxCAL_SEQUENTIAL_MONTH_SELECTION
)
101 wxFLAGS_MEMBER(wxCAL_SHOW_SURROUNDING_WEEKS
)
103 wxEND_FLAGS( wxCalendarCtrlStyle
)
105 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCalendarCtrl
, wxControl
,"wx/calctrl.h")
107 wxBEGIN_PROPERTIES_TABLE(wxCalendarCtrl
)
108 wxEVENT_RANGE_PROPERTY( Updated
, wxEVT_CALENDAR_SEL_CHANGED
, wxEVT_CALENDAR_WEEKDAY_CLICKED
, wxCalendarEvent
)
109 wxHIDE_PROPERTY( Children
)
110 wxPROPERTY( Date
,wxDateTime
, SetDate
, GetDate
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
111 wxPROPERTY_FLAGS( WindowStyle
, wxCalendarCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
112 wxEND_PROPERTIES_TABLE()
114 wxBEGIN_HANDLERS_TABLE(wxCalendarCtrl
)
115 wxEND_HANDLERS_TABLE()
117 wxCONSTRUCTOR_6( wxCalendarCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxDateTime
, Date
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
119 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl
, wxControl
)
121 IMPLEMENT_DYNAMIC_CLASS(wxCalendarEvent
, wxDateEvent
)
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_SEL_CHANGED
)
128 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DAY_CHANGED
)
129 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_MONTH_CHANGED
)
130 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_YEAR_CHANGED
)
131 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DOUBLECLICKED
)
132 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_WEEKDAY_CLICKED
)
134 // ============================================================================
136 // ============================================================================
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 wxCalendarCtrl::wxCalendarCtrl(wxWindow
*parent
,
144 const wxDateTime
& date
,
148 const wxString
& name
)
152 (void)Create(parent
, id
, date
, pos
, size
, style
, name
);
155 void wxCalendarCtrl::Init()
160 m_staticMonth
= NULL
;
162 m_userChangedYear
= false;
167 wxDateTime::WeekDay wd
;
168 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
170 m_weekdays
[wd
] = wxDateTime::GetWeekDayName(wd
, wxDateTime::Name_Abbr
);
173 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
178 m_colHighlightFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
179 m_colHighlightBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
181 m_colHolidayFg
= *wxRED
;
182 // don't set m_colHolidayBg - by default, same as our bg colour
184 m_colHeaderFg
= *wxBLUE
;
185 m_colHeaderBg
= *wxLIGHT_GREY
;
188 bool wxCalendarCtrl::Create(wxWindow
*parent
,
190 const wxDateTime
& date
,
194 const wxString
& name
)
196 if ( !wxControl::Create(parent
, id
, pos
, size
,
197 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
198 wxDefaultValidator
, name
) )
203 // needed to get the arrow keys normally used for the dialog navigation
204 SetWindowStyle(style
| wxWANTS_CHARS
);
206 m_date
= date
.IsValid() ? date
: wxDateTime::Today();
208 m_lowdate
= wxDefaultDateTime
;
209 m_highdate
= wxDefaultDateTime
;
211 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
213 CreateYearSpinCtrl();
214 m_staticYear
= new wxStaticText(GetParent(), wxID_ANY
, m_date
.Format(_T("%Y")),
215 wxDefaultPosition
, wxDefaultSize
,
218 CreateMonthComboBox();
219 m_staticMonth
= new wxStaticText(GetParent(), wxID_ANY
, m_date
.Format(_T("%B")),
220 wxDefaultPosition
, wxDefaultSize
,
224 ShowCurrentControls();
226 // we need to set the position as well because the main control position
227 // is not the same as the one specified in pos if we have the controls
232 // Since we don't paint the whole background make sure that the platform
233 // will use the right one.
234 SetBackgroundColour(GetBackgroundColour());
241 wxCalendarCtrl::~wxCalendarCtrl()
243 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
249 // ----------------------------------------------------------------------------
250 // Create the wxComboBox and wxSpinCtrl
251 // ----------------------------------------------------------------------------
253 void wxCalendarCtrl::CreateMonthComboBox()
255 m_comboMonth
= new wxComboBox(GetParent(), wxID_ANY
,
260 wxCB_READONLY
| wxCLIP_SIBLINGS
);
263 for ( m
= wxDateTime::Jan
; m
< wxDateTime::Inv_Month
; wxNextMonth(m
) )
265 m_comboMonth
->Append(wxDateTime::GetMonthName(m
));
268 m_comboMonth
->SetSelection(GetDate().GetMonth());
269 m_comboMonth
->SetSize(wxDefaultCoord
,
273 wxSIZE_AUTO_WIDTH
|wxSIZE_AUTO_HEIGHT
);
275 m_comboMonth
->Connect(wxEVT_COMMAND_COMBOBOX_SELECTED
,
276 wxCommandEventHandler(wxCalendarCtrl::OnMonthChange
),
280 void wxCalendarCtrl::CreateYearSpinCtrl()
282 m_spinYear
= new wxSpinCtrl(GetParent(), wxID_ANY
,
283 GetDate().Format(_T("%Y")),
286 wxSP_ARROW_KEYS
| wxCLIP_SIBLINGS
,
287 -4300, 10000, GetDate().GetYear());
289 m_spinYear
->Connect(wxEVT_COMMAND_TEXT_UPDATED
,
290 wxCommandEventHandler(wxCalendarCtrl::OnYearTextChange
),
293 m_spinYear
->Connect(wxEVT_COMMAND_SPINCTRL_UPDATED
,
294 wxCommandEventHandler(wxCalendarCtrl::OnYearChange
),
298 // ----------------------------------------------------------------------------
299 // forward wxWin functions to subcontrols
300 // ----------------------------------------------------------------------------
302 bool wxCalendarCtrl::Destroy()
305 m_staticYear
->Destroy();
307 m_spinYear
->Destroy();
309 m_comboMonth
->Destroy();
311 m_staticMonth
->Destroy();
316 m_staticMonth
= NULL
;
318 return wxControl::Destroy();
321 bool wxCalendarCtrl::Show(bool show
)
323 if ( !wxControl::Show(show
) )
328 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
330 if ( GetMonthControl() )
332 GetMonthControl()->Show(show
);
333 GetYearControl()->Show(show
);
340 bool wxCalendarCtrl::Enable(bool enable
)
342 if ( !wxControl::Enable(enable
) )
347 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
349 GetMonthControl()->Enable(enable
);
350 GetYearControl()->Enable(enable
);
356 // ----------------------------------------------------------------------------
357 // enable/disable month/year controls
358 // ----------------------------------------------------------------------------
360 void wxCalendarCtrl::ShowCurrentControls()
362 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
364 if ( AllowMonthChange() )
366 m_comboMonth
->Show();
367 m_staticMonth
->Hide();
369 if ( AllowYearChange() )
372 m_staticYear
->Hide();
380 m_comboMonth
->Hide();
381 m_staticMonth
->Show();
384 // year change not allowed here
386 m_staticYear
->Show();
390 wxControl
*wxCalendarCtrl::GetMonthControl() const
392 return AllowMonthChange() ? (wxControl
*)m_comboMonth
: (wxControl
*)m_staticMonth
;
395 wxControl
*wxCalendarCtrl::GetYearControl() const
397 return AllowYearChange() ? (wxControl
*)m_spinYear
: (wxControl
*)m_staticYear
;
400 void wxCalendarCtrl::EnableYearChange(bool enable
)
402 if ( enable
!= AllowYearChange() )
404 long style
= GetWindowStyle();
406 style
&= ~wxCAL_NO_YEAR_CHANGE
;
408 style
|= wxCAL_NO_YEAR_CHANGE
;
409 SetWindowStyle(style
);
411 ShowCurrentControls();
412 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
419 void wxCalendarCtrl::EnableMonthChange(bool enable
)
421 if ( enable
!= AllowMonthChange() )
423 long style
= GetWindowStyle();
425 style
&= ~wxCAL_NO_MONTH_CHANGE
;
427 style
|= wxCAL_NO_MONTH_CHANGE
;
428 SetWindowStyle(style
);
430 ShowCurrentControls();
431 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
438 // ----------------------------------------------------------------------------
440 // ----------------------------------------------------------------------------
442 bool wxCalendarCtrl::SetDate(const wxDateTime
& date
)
446 bool sameMonth
= m_date
.GetMonth() == date
.GetMonth(),
447 sameYear
= m_date
.GetYear() == date
.GetYear();
449 if ( IsDateInRange(date
) )
451 if ( sameMonth
&& sameYear
)
453 // just change the day
458 if ( AllowMonthChange() && (AllowYearChange() || sameYear
) )
463 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
465 // update the controls
466 m_comboMonth
->SetSelection(m_date
.GetMonth());
468 if ( AllowYearChange() )
470 if ( !m_userChangedYear
)
471 m_spinYear
->SetValue(m_date
.Format(_T("%Y")));
475 // as the month changed, holidays did too
478 // update the calendar
489 m_userChangedYear
= false;
494 void wxCalendarCtrl::ChangeDay(const wxDateTime
& date
)
496 if ( m_date
!= date
)
498 // we need to refresh the row containing the old date and the one
499 // containing the new one
500 wxDateTime dateOld
= m_date
;
503 RefreshDate(dateOld
);
505 // if the date is in the same row, it was already drawn correctly
506 if ( GetWeek(m_date
) != GetWeek(dateOld
) )
513 void wxCalendarCtrl::SetDateAndNotify(const wxDateTime
& date
)
515 wxDateTime::Tm tm1
= m_date
.GetTm(),
519 if ( tm1
.year
!= tm2
.year
)
520 type
= wxEVT_CALENDAR_YEAR_CHANGED
;
521 else if ( tm1
.mon
!= tm2
.mon
)
522 type
= wxEVT_CALENDAR_MONTH_CHANGED
;
523 else if ( tm1
.mday
!= tm2
.mday
)
524 type
= wxEVT_CALENDAR_DAY_CHANGED
;
530 GenerateEvents(type
, wxEVT_CALENDAR_SEL_CHANGED
);
534 // ----------------------------------------------------------------------------
536 // ----------------------------------------------------------------------------
538 bool wxCalendarCtrl::SetLowerDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
542 if ( !(date
.IsValid()) || ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : true ) )
554 bool wxCalendarCtrl::SetUpperDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
558 if ( !(date
.IsValid()) || ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : true ) )
570 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& lowerdate
/* = wxDefaultDateTime */, const wxDateTime
& upperdate
/* = wxDefaultDateTime */)
575 ( !( lowerdate
.IsValid() ) || ( ( upperdate
.IsValid() ) ? ( lowerdate
<= upperdate
) : true ) ) &&
576 ( !( upperdate
.IsValid() ) || ( ( lowerdate
.IsValid() ) ? ( upperdate
>= lowerdate
) : true ) ) )
578 m_lowdate
= lowerdate
;
579 m_highdate
= upperdate
;
589 // ----------------------------------------------------------------------------
591 // ----------------------------------------------------------------------------
593 wxDateTime
wxCalendarCtrl::GetStartDate() const
595 wxDateTime::Tm tm
= m_date
.GetTm();
597 wxDateTime date
= wxDateTime(1, tm
.mon
, tm
.year
);
600 date
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
601 ? wxDateTime::Mon
: wxDateTime::Sun
);
603 if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
)
605 // We want to offset the calendar if we start on the first..
606 if ( date
.GetDay() == 1 )
608 date
-= wxDateSpan::Week();
615 bool wxCalendarCtrl::IsDateShown(const wxDateTime
& date
) const
617 if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
619 return date
.GetMonth() == m_date
.GetMonth();
627 bool wxCalendarCtrl::IsDateInRange(const wxDateTime
& date
) const
629 // Check if the given date is in the range specified
630 return ( ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : true )
631 && ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : true ) );
634 bool wxCalendarCtrl::ChangeYear(wxDateTime
* target
) const
638 if ( !(IsDateInRange(*target
)) )
640 if ( target
->GetYear() < m_date
.GetYear() )
642 if ( target
->GetYear() >= GetLowerDateLimit().GetYear() )
644 *target
= GetLowerDateLimit();
654 if ( target
->GetYear() <= GetUpperDateLimit().GetYear() )
656 *target
= GetUpperDateLimit();
673 bool wxCalendarCtrl::ChangeMonth(wxDateTime
* target
) const
677 if ( !(IsDateInRange(*target
)) )
681 if ( target
->GetMonth() < m_date
.GetMonth() )
683 *target
= GetLowerDateLimit();
687 *target
= GetUpperDateLimit();
694 size_t wxCalendarCtrl::GetWeek(const wxDateTime
& date
) const
696 size_t retval
= date
.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
697 ? wxDateTime::Monday_First
698 : wxDateTime::Sunday_First
);
700 if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
702 // we need to offset an extra week if we "start" on the 1st of the month
703 wxDateTime::Tm tm
= date
.GetTm();
705 wxDateTime datetest
= wxDateTime(1, tm
.mon
, tm
.year
);
708 datetest
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
709 ? wxDateTime::Mon
: wxDateTime::Sun
);
711 if ( datetest
.GetDay() == 1 )
720 // ----------------------------------------------------------------------------
722 // ----------------------------------------------------------------------------
724 // this is a composite control and it must arrange its parts each time its
725 // size or position changes: the combobox and spinctrl are along the top of
726 // the available area and the calendar takes up therest of the space
728 // the static controls are supposed to be always smaller than combo/spin so we
729 // always use the latter for size calculations and position the static to take
732 // the constants used for the layout
733 #define VERT_MARGIN 5 // distance between combo and calendar
735 #define HORZ_MARGIN 5 // spin
737 #define HORZ_MARGIN 15 // spin
739 wxSize
wxCalendarCtrl::DoGetBestSize() const
741 // calc the size of the calendar
742 ((wxCalendarCtrl
*)this)->RecalcGeometry(); // const_cast
744 wxCoord width
= 7*m_widthCol
,
745 height
= 7*m_heightRow
+ m_rowOffset
+ VERT_MARGIN
;
747 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
749 // the combobox doesn't report its height correctly (it returns the
750 // height including the drop down list) so don't use it
751 height
+= m_spinYear
->GetBestSize().y
;
754 wxCoord w2
= m_comboMonth
->GetBestSize().x
+ HORZ_MARGIN
+ GetCharWidth()*6;
759 if ( !HasFlag(wxBORDER_NONE
) )
761 // the border would clip the last line otherwise
766 wxSize
best(width
, height
);
771 void wxCalendarCtrl::DoSetSize(int x
, int y
,
772 int width
, int height
,
775 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
778 void wxCalendarCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
782 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
784 wxSize sizeCombo
= m_comboMonth
->GetSize();
785 wxSize sizeStatic
= m_staticMonth
->GetSize();
786 wxSize sizeSpin
= m_spinYear
->GetSize();
787 int dy
= (sizeCombo
.y
- sizeStatic
.y
) / 2;
788 m_comboMonth
->Move(x
, y
);
789 m_staticMonth
->SetSize(x
, y
+ dy
, sizeCombo
.x
, sizeStatic
.y
);
791 int xDiff
= sizeCombo
.x
+ HORZ_MARGIN
;
793 m_spinYear
->SetSize(x
+ xDiff
, y
, width
- xDiff
, sizeCombo
.y
);
794 m_staticYear
->SetSize(x
+ xDiff
, y
+ dy
, width
- xDiff
, sizeStatic
.y
);
796 yDiff
= wxMax(sizeSpin
.y
, sizeCombo
.y
) + VERT_MARGIN
;
798 else // no controls on the top
803 wxControl::DoMoveWindow(x
, y
+ yDiff
, width
, height
- yDiff
);
806 void wxCalendarCtrl::DoGetPosition(int *x
, int *y
) const
808 wxControl::DoGetPosition(x
, y
);
810 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
812 // our real top corner is not in this position
815 *y
-= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
820 void wxCalendarCtrl::DoGetSize(int *width
, int *height
) const
822 wxControl::DoGetSize(width
, height
);
824 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
826 // our real height is bigger
827 if ( height
&& GetMonthControl())
829 *height
+= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
834 void wxCalendarCtrl::RecalcGeometry()
838 dc
.SetFont(GetFont());
840 // determine the column width (weekday names are not necessarily wider
841 // than the numbers (in some languages), so let's not assume that they are)
843 for ( int day
= 10; day
<= 31; day
++)
846 dc
.GetTextExtent(wxString::Format(wxT("%d"), day
), &width
, &m_heightRow
);
847 if ( width
> m_widthCol
)
849 // 1.5 times the width gives nice margins even if the weekday
851 m_widthCol
= width
+width
/2;
854 wxDateTime::WeekDay wd
;
855 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
858 dc
.GetTextExtent(m_weekdays
[wd
], &width
, &m_heightRow
);
859 if ( width
> m_widthCol
)
865 // leave some margins
869 m_rowOffset
= (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) ? m_heightRow
: 0; // conditional in relation to style
872 // ----------------------------------------------------------------------------
874 // ----------------------------------------------------------------------------
876 void wxCalendarCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
880 dc
.SetFont(GetFont());
885 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
886 m_date
.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
891 wxCoord x0
= (GetSize().x
- m_widthCol
*7) /2;
894 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
896 // draw the sequential month-selector
898 dc
.SetBackgroundMode(wxTRANSPARENT
);
899 dc
.SetTextForeground(*wxBLACK
);
900 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
901 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
902 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
904 // Get extent of month-name + year
905 wxCoord monthw
, monthh
;
906 wxString headertext
= m_date
.Format(wxT("%B %Y"));
907 dc
.GetTextExtent(headertext
, &monthw
, &monthh
);
909 // draw month-name centered above weekdays
910 wxCoord monthx
= ((m_widthCol
* 7) - monthw
) / 2 + x0
;
911 wxCoord monthy
= ((m_heightRow
- monthh
) / 2) + y
;
912 dc
.DrawText(headertext
, monthx
, monthy
);
914 // calculate the "month-arrows"
915 wxPoint leftarrow
[3];
916 wxPoint rightarrow
[3];
918 int arrowheight
= monthh
/ 2;
920 leftarrow
[0] = wxPoint(0, arrowheight
/ 2);
921 leftarrow
[1] = wxPoint(arrowheight
/ 2, 0);
922 leftarrow
[2] = wxPoint(arrowheight
/ 2, arrowheight
- 1);
924 rightarrow
[0] = wxPoint(0,0);
925 rightarrow
[1] = wxPoint(arrowheight
/ 2, arrowheight
/ 2);
926 rightarrow
[2] = wxPoint(0, arrowheight
- 1);
928 // draw the "month-arrows"
930 wxCoord arrowy
= (m_heightRow
- arrowheight
) / 2;
931 wxCoord larrowx
= (m_widthCol
- (arrowheight
/ 2)) / 2 + x0
;
932 wxCoord rarrowx
= ((m_widthCol
- (arrowheight
/ 2)) / 2) + m_widthCol
*6 + x0
;
933 m_leftArrowRect
= m_rightArrowRect
= wxRect(0,0,0,0);
935 if ( AllowMonthChange() )
937 wxDateTime ldpm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) - wxDateSpan::Day(); // last day prev month
938 // Check if range permits change
939 if ( IsDateInRange(ldpm
) && ( ( ldpm
.GetYear() == m_date
.GetYear() ) ? true : AllowYearChange() ) )
941 m_leftArrowRect
= wxRect(larrowx
- 3, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
942 dc
.SetBrush(wxBrush(*wxBLACK
, wxSOLID
));
943 dc
.SetPen(wxPen(*wxBLACK
, 1, wxSOLID
));
944 dc
.DrawPolygon(3, leftarrow
, larrowx
, arrowy
, wxWINDING_RULE
);
945 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
946 dc
.DrawRectangle(m_leftArrowRect
);
948 wxDateTime fdnm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) + wxDateSpan::Month(); // first day next month
949 if ( IsDateInRange(fdnm
) && ( ( fdnm
.GetYear() == m_date
.GetYear() ) ? true : AllowYearChange() ) )
951 m_rightArrowRect
= wxRect(rarrowx
- 4, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
952 dc
.SetBrush(wxBrush(*wxBLACK
, wxSOLID
));
953 dc
.SetPen(wxPen(*wxBLACK
, 1, wxSOLID
));
954 dc
.DrawPolygon(3, rightarrow
, rarrowx
, arrowy
, wxWINDING_RULE
);
955 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
956 dc
.DrawRectangle(m_rightArrowRect
);
963 // first draw the week days
964 if ( IsExposed(x0
, y
, 7*m_widthCol
, m_heightRow
) )
967 wxLogDebug("painting the header");
970 dc
.SetBackgroundMode(wxTRANSPARENT
);
971 dc
.SetTextForeground(m_colHeaderFg
);
972 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
973 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
974 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
976 bool startOnMonday
= (GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
977 for ( size_t wd
= 0; wd
< 7; wd
++ )
981 n
= wd
== 6 ? 0 : wd
+ 1;
985 dc
.GetTextExtent(m_weekdays
[n
], &dayw
, &dayh
);
986 dc
.DrawText(m_weekdays
[n
], x0
+ (wd
*m_widthCol
) + ((m_widthCol
- dayw
) / 2), y
); // center the day-name
990 // then the calendar itself
991 dc
.SetTextForeground(*wxBLACK
);
992 //dc.SetFont(*wxNORMAL_FONT);
995 wxDateTime date
= GetStartDate();
998 wxLogDebug("starting calendar from %s\n",
999 date
.Format("%a %d-%m-%Y %H:%M:%S").c_str());
1002 dc
.SetBackgroundMode(wxSOLID
);
1003 for ( size_t nWeek
= 1; nWeek
<= 6; nWeek
++, y
+= m_heightRow
)
1005 // if the update region doesn't intersect this row, don't paint it
1006 if ( !IsExposed(0, y
, 7*m_widthCol
, m_heightRow
- 1) )
1008 date
+= wxDateSpan::Week();
1014 wxLogDebug("painting week %d at y = %d\n", nWeek
, y
);
1017 for ( size_t wd
= 0; wd
< 7; wd
++ )
1019 if ( IsDateShown(date
) )
1021 // don't use wxDate::Format() which prepends 0s
1022 unsigned int day
= date
.GetDay();
1023 wxString dayStr
= wxString::Format(_T("%u"), day
);
1025 dc
.GetTextExtent(dayStr
, &width
, (wxCoord
*)NULL
);
1027 bool changedColours
= false,
1028 changedFont
= false;
1031 wxCalendarDateAttr
*attr
= NULL
;
1033 if ( date
.GetMonth() != m_date
.GetMonth() || !IsDateInRange(date
) )
1035 // surrounding week or out-of-range
1037 dc
.SetTextForeground(*wxLIGHT_GREY
);
1038 changedColours
= true;
1042 isSel
= date
.IsSameDate(m_date
);
1043 attr
= m_attrs
[day
- 1];
1047 dc
.SetTextForeground(m_colHighlightFg
);
1048 dc
.SetTextBackground(m_colHighlightBg
);
1050 changedColours
= true;
1054 wxColour colFg
, colBg
;
1056 if ( attr
->IsHoliday() )
1058 colFg
= m_colHolidayFg
;
1059 colBg
= m_colHolidayBg
;
1063 colFg
= attr
->GetTextColour();
1064 colBg
= attr
->GetBackgroundColour();
1069 dc
.SetTextForeground(colFg
);
1070 changedColours
= true;
1075 dc
.SetTextBackground(colBg
);
1076 changedColours
= true;
1079 if ( attr
->HasFont() )
1081 dc
.SetFont(attr
->GetFont());
1087 wxCoord x
= wd
*m_widthCol
+ (m_widthCol
- width
) / 2 + x0
;
1088 dc
.DrawText(dayStr
, x
, y
+ 1);
1090 if ( !isSel
&& attr
&& attr
->HasBorder() )
1093 if ( attr
->HasBorderColour() )
1095 colBorder
= attr
->GetBorderColour();
1099 colBorder
= GetForegroundColour();
1102 wxPen
pen(colBorder
, 1, wxSOLID
);
1104 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1106 switch ( attr
->GetBorder() )
1108 case wxCAL_BORDER_SQUARE
:
1109 dc
.DrawRectangle(x
- 2, y
,
1110 width
+ 4, m_heightRow
);
1113 case wxCAL_BORDER_ROUND
:
1114 dc
.DrawEllipse(x
- 2, y
,
1115 width
+ 4, m_heightRow
);
1119 wxFAIL_MSG(_T("unknown border type"));
1123 if ( changedColours
)
1125 dc
.SetTextForeground(GetForegroundColour());
1126 dc
.SetTextBackground(GetBackgroundColour());
1131 dc
.SetFont(GetFont());
1134 //else: just don't draw it
1136 date
+= wxDateSpan::Day();
1140 // Greying out out-of-range background
1141 bool showSurrounding
= (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) != 0;
1143 date
= ( showSurrounding
) ? GetStartDate() : wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear());
1144 if ( !IsDateInRange(date
) )
1146 wxDateTime firstOOR
= GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1148 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1149 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1151 HighlightRange(&dc
, date
, firstOOR
, wxTRANSPARENT_PEN
, &oorbrush
);
1154 date
= ( showSurrounding
) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear());
1155 if ( !IsDateInRange(date
) )
1157 wxDateTime firstOOR
= GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1159 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1160 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1162 HighlightRange(&dc
, firstOOR
, date
, wxTRANSPARENT_PEN
, &oorbrush
);
1166 wxLogDebug("+++ finished painting");
1170 void wxCalendarCtrl::RefreshDate(const wxDateTime
& date
)
1176 // always refresh the whole row at once because our OnPaint() will draw
1177 // the whole row anyhow - and this allows the small optimisation in
1178 // OnClick() below to work
1181 rect
.y
= (m_heightRow
* GetWeek(date
)) + m_rowOffset
;
1183 rect
.width
= 7*m_widthCol
;
1184 rect
.height
= m_heightRow
;
1187 // VZ: for some reason, the selected date seems to occupy more space under
1188 // MSW - this is probably some bug in the font size calculations, but I
1189 // don't know where exactly. This fix is ugly and leads to more
1190 // refreshes than really needed, but without it the selected days
1191 // leaves even more ugly underscores on screen.
1196 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
1199 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1202 Refresh(true, &rect
);
1205 void wxCalendarCtrl::HighlightRange(wxPaintDC
* pDC
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, wxPen
* pPen
, wxBrush
* pBrush
)
1207 // Highlights the given range using pen and brush
1208 // Does nothing if todate < fromdate
1212 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate
.Format("%d %m %Y"), todate
.Format("%d %m %Y"));
1215 if ( todate
>= fromdate
)
1222 // implicit: both dates must be currently shown - checked by GetDateCoord
1223 if ( GetDateCoord(fromdate
, &fd
, &fw
) && GetDateCoord(todate
, &td
, &tw
) )
1226 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd
, fw
, td
, tw
);
1228 if ( ( (tw
- fw
) == 1 ) && ( td
< fd
) )
1230 // special case: interval 7 days or less not in same week
1231 // split in two separate intervals
1232 wxDateTime tfd
= fromdate
+ wxDateSpan::Days(7-fd
);
1233 wxDateTime ftd
= tfd
+ wxDateSpan::Day();
1235 wxLogDebug("Highlight: Separate segments");
1238 HighlightRange(pDC
, fromdate
, tfd
, pPen
, pBrush
);
1239 HighlightRange(pDC
, ftd
, todate
, pPen
, pBrush
);
1244 wxPoint corners
[8]; // potentially 8 corners in polygon
1248 // simple case: same week
1250 corners
[0] = wxPoint((fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
);
1251 corners
[1] = wxPoint((fd
- 1) * m_widthCol
, ((fw
+ 1 ) * m_heightRow
) + m_rowOffset
);
1252 corners
[2] = wxPoint(td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
);
1253 corners
[3] = wxPoint(td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
);
1258 // "complex" polygon
1259 corners
[cidx
] = wxPoint((fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1263 corners
[cidx
] = wxPoint((fd
- 1) * m_widthCol
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1264 corners
[cidx
] = wxPoint(0, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1267 corners
[cidx
] = wxPoint(0, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1268 corners
[cidx
] = wxPoint(td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1272 corners
[cidx
] = wxPoint(td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1273 corners
[cidx
] = wxPoint(7 * m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1276 corners
[cidx
] = wxPoint(7 * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1282 pDC
->SetBrush(*pBrush
);
1284 pDC
->DrawPolygon(numpoints
, corners
);
1290 wxLogDebug("--- HighlightRange ---");
1294 bool wxCalendarCtrl::GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const
1299 wxLogDebug("+++ GetDateCoord: (%s) +++", date
.Format("%d %m %Y"));
1302 if ( IsDateShown(date
) )
1304 bool startOnMonday
= ( GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1307 *day
= date
.GetWeekDay();
1309 if ( *day
== 0 ) // sunday
1311 *day
= ( startOnMonday
) ? 7 : 1;
1315 *day
+= ( startOnMonday
) ? 0 : 1;
1318 int targetmonth
= date
.GetMonth() + (12 * date
.GetYear());
1319 int thismonth
= m_date
.GetMonth() + (12 * m_date
.GetYear());
1322 if ( targetmonth
== thismonth
)
1324 *week
= GetWeek(date
);
1328 if ( targetmonth
< thismonth
)
1330 *week
= 1; // trivial
1332 else // targetmonth > thismonth
1338 // get the datecoord of the last day in the month currently shown
1340 wxLogDebug(" +++ LDOM +++");
1342 GetDateCoord(ldcm
.SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear()), &lastday
, &lastweek
);
1344 wxLogDebug(" --- LDOM ---");
1347 wxTimeSpan span
= date
- ldcm
;
1349 int daysfromlast
= span
.GetDays();
1351 wxLogDebug("daysfromlast: %i", daysfromlast
);
1353 if ( daysfromlast
+ lastday
> 7 ) // past week boundary
1355 int wholeweeks
= (daysfromlast
/ 7);
1356 *week
= wholeweeks
+ lastweek
;
1357 if ( (daysfromlast
- (7 * wholeweeks
) + lastday
) > 7 )
1377 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date
.Format("%d %m %Y"), *day
, *week
);
1383 // ----------------------------------------------------------------------------
1385 // ----------------------------------------------------------------------------
1387 void wxCalendarCtrl::OnDClick(wxMouseEvent
& event
)
1389 if ( HitTest(event
.GetPosition()) != wxCAL_HITTEST_DAY
)
1395 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1399 void wxCalendarCtrl::OnClick(wxMouseEvent
& event
)
1402 wxDateTime::WeekDay wday
;
1403 switch ( HitTest(event
.GetPosition(), &date
, &wday
) )
1405 case wxCAL_HITTEST_DAY
:
1406 if ( IsDateInRange(date
) )
1410 GenerateEvents(wxEVT_CALENDAR_DAY_CHANGED
,
1411 wxEVT_CALENDAR_SEL_CHANGED
);
1415 case wxCAL_HITTEST_HEADER
:
1417 wxCalendarEvent
event(this, wxEVT_CALENDAR_WEEKDAY_CLICKED
);
1418 event
.m_wday
= wday
;
1419 (void)GetEventHandler()->ProcessEvent(event
);
1423 case wxCAL_HITTEST_DECMONTH
:
1424 case wxCAL_HITTEST_INCMONTH
:
1425 case wxCAL_HITTEST_SURROUNDING_WEEK
:
1426 SetDateAndNotify(date
); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1430 wxFAIL_MSG(_T("unknown hittest code"));
1433 case wxCAL_HITTEST_NOWHERE
:
1439 wxCalendarHitTestResult
wxCalendarCtrl::HitTest(const wxPoint
& pos
,
1441 wxDateTime::WeekDay
*wd
)
1447 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1448 if ( (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
1452 // we need to find out if the hit is on left arrow, on month or on right arrow
1454 if ( wxRegion(m_leftArrowRect
).Contains(pos
) == wxInRegion
)
1458 if ( IsDateInRange(m_date
- wxDateSpan::Month()) )
1460 *date
= m_date
- wxDateSpan::Month();
1464 *date
= GetLowerDateLimit();
1468 return wxCAL_HITTEST_DECMONTH
;
1471 if ( wxRegion(m_rightArrowRect
).Contains(pos
) == wxInRegion
)
1475 if ( IsDateInRange(m_date
+ wxDateSpan::Month()) )
1477 *date
= m_date
+ wxDateSpan::Month();
1481 *date
= GetUpperDateLimit();
1485 return wxCAL_HITTEST_INCMONTH
;
1490 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1492 int wday
= pos
.x
/ m_widthCol
;
1493 // if ( y < m_heightRow )
1494 if ( y
< (m_heightRow
+ m_rowOffset
) )
1496 if ( y
> m_rowOffset
)
1500 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST
)
1502 wday
= wday
== 6 ? 0 : wday
+ 1;
1505 *wd
= (wxDateTime::WeekDay
)wday
;
1508 return wxCAL_HITTEST_HEADER
;
1512 return wxCAL_HITTEST_NOWHERE
;
1516 // int week = (y - m_heightRow) / m_heightRow;
1517 int week
= (y
- (m_heightRow
+ m_rowOffset
)) / m_heightRow
;
1518 if ( week
>= 6 || wday
>= 7 )
1520 return wxCAL_HITTEST_NOWHERE
;
1523 wxDateTime dt
= GetStartDate() + wxDateSpan::Days(7*week
+ wday
);
1525 if ( IsDateShown(dt
) )
1530 if ( dt
.GetMonth() == m_date
.GetMonth() )
1533 return wxCAL_HITTEST_DAY
;
1537 return wxCAL_HITTEST_SURROUNDING_WEEK
;
1542 return wxCAL_HITTEST_NOWHERE
;
1546 // ----------------------------------------------------------------------------
1547 // subcontrols events handling
1548 // ----------------------------------------------------------------------------
1550 void wxCalendarCtrl::OnMonthChange(wxCommandEvent
& event
)
1552 wxDateTime::Tm tm
= m_date
.GetTm();
1554 wxDateTime::Month mon
= (wxDateTime::Month
)event
.GetInt();
1555 if ( tm
.mday
> wxDateTime::GetNumberOfDays(mon
, tm
.year
) )
1557 tm
.mday
= wxDateTime::GetNumberOfDays(mon
, tm
.year
);
1560 wxDateTime target
= wxDateTime(tm
.mday
, mon
, tm
.year
);
1562 ChangeMonth(&target
);
1563 SetDateAndNotify(target
);
1566 void wxCalendarCtrl::OnYearChange(wxCommandEvent
& event
)
1568 int year
= (int)event
.GetInt();
1569 if ( year
== INT_MIN
)
1571 // invalid year in the spin control, ignore it
1575 wxDateTime::Tm tm
= m_date
.GetTm();
1577 if ( tm
.mday
> wxDateTime::GetNumberOfDays(tm
.mon
, year
) )
1579 tm
.mday
= wxDateTime::GetNumberOfDays(tm
.mon
, year
);
1582 wxDateTime target
= wxDateTime(tm
.mday
, tm
.mon
, year
);
1584 if ( ChangeYear(&target
) )
1586 SetDateAndNotify(target
);
1590 // In this case we don't want to change the date. That would put us
1591 // inside the same year but a strange number of months forward/back..
1592 m_spinYear
->SetValue(target
.GetYear());
1596 void wxCalendarCtrl::OnYearTextChange(wxCommandEvent
& event
)
1598 SetUserChangedYear();
1599 OnYearChange(event
);
1602 // ----------------------------------------------------------------------------
1603 // keyboard interface
1604 // ----------------------------------------------------------------------------
1606 void wxCalendarCtrl::OnChar(wxKeyEvent
& event
)
1609 switch ( event
.GetKeyCode() )
1613 target
= m_date
+ wxDateSpan::Year();
1614 if ( ChangeYear(&target
) )
1616 SetDateAndNotify(target
);
1622 target
= m_date
- wxDateSpan::Year();
1623 if ( ChangeYear(&target
) )
1625 SetDateAndNotify(target
);
1630 target
= m_date
- wxDateSpan::Month();
1631 ChangeMonth(&target
);
1632 SetDateAndNotify(target
); // always
1636 target
= m_date
+ wxDateSpan::Month();
1637 ChangeMonth(&target
);
1638 SetDateAndNotify(target
); // always
1642 if ( event
.ControlDown() )
1644 target
= wxDateTime(m_date
).SetToNextWeekDay(
1645 GetWindowStyle() & wxCAL_MONDAY_FIRST
1646 ? wxDateTime::Sun
: wxDateTime::Sat
);
1647 if ( !IsDateInRange(target
) )
1649 target
= GetUpperDateLimit();
1651 SetDateAndNotify(target
);
1654 SetDateAndNotify(m_date
+ wxDateSpan::Day());
1658 if ( event
.ControlDown() )
1660 target
= wxDateTime(m_date
).SetToPrevWeekDay(
1661 GetWindowStyle() & wxCAL_MONDAY_FIRST
1662 ? wxDateTime::Mon
: wxDateTime::Sun
);
1663 if ( !IsDateInRange(target
) )
1665 target
= GetLowerDateLimit();
1667 SetDateAndNotify(target
);
1670 SetDateAndNotify(m_date
- wxDateSpan::Day());
1674 SetDateAndNotify(m_date
- wxDateSpan::Week());
1678 SetDateAndNotify(m_date
+ wxDateSpan::Week());
1682 if ( event
.ControlDown() )
1683 SetDateAndNotify(wxDateTime::Today());
1685 SetDateAndNotify(wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear()));
1689 SetDateAndNotify(wxDateTime(m_date
).SetToLastMonthDay());
1693 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1701 // ----------------------------------------------------------------------------
1702 // holidays handling
1703 // ----------------------------------------------------------------------------
1705 void wxCalendarCtrl::EnableHolidayDisplay(bool display
)
1707 long style
= GetWindowStyle();
1709 style
|= wxCAL_SHOW_HOLIDAYS
;
1711 style
&= ~wxCAL_SHOW_HOLIDAYS
;
1713 SetWindowStyle(style
);
1718 ResetHolidayAttrs();
1723 void wxCalendarCtrl::SetHolidayAttrs()
1725 if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS
)
1727 ResetHolidayAttrs();
1729 wxDateTime::Tm tm
= m_date
.GetTm();
1730 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
1731 dtEnd
= dtStart
.GetLastMonthDay();
1733 wxDateTimeArray hol
;
1734 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
1736 size_t count
= hol
.GetCount();
1737 for ( size_t n
= 0; n
< count
; n
++ )
1739 SetHoliday(hol
[n
].GetDay());
1744 void wxCalendarCtrl::SetHoliday(size_t day
)
1746 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in SetHoliday") );
1748 wxCalendarDateAttr
*attr
= GetAttr(day
);
1751 attr
= new wxCalendarDateAttr
;
1754 attr
->SetHoliday(true);
1756 // can't use SetAttr() because it would delete this pointer
1757 m_attrs
[day
- 1] = attr
;
1760 void wxCalendarCtrl::ResetHolidayAttrs()
1762 for ( size_t day
= 0; day
< 31; day
++ )
1766 m_attrs
[day
]->SetHoliday(false);
1774 wxCalendarCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
1776 // Use the same color scheme as wxListBox
1777 return wxListBox::GetClassDefaultAttributes(variant
);
1780 #endif // wxUSE_CALENDARCTRL