1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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
)
63 EVT_SYS_COLOUR_CHANGED(wxCalendarCtrl::OnSysColourChanged
)
66 #if wxUSE_EXTENDED_RTTI
67 WX_DEFINE_FLAGS( wxCalendarCtrlStyle
)
69 wxBEGIN_FLAGS( wxCalendarCtrlStyle
)
70 // new style border flags, we put them first to
71 // use them for streaming out
72 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
73 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
74 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
75 wxFLAGS_MEMBER(wxBORDER_RAISED
)
76 wxFLAGS_MEMBER(wxBORDER_STATIC
)
77 wxFLAGS_MEMBER(wxBORDER_NONE
)
79 // old style border flags
80 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
81 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
82 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
83 wxFLAGS_MEMBER(wxRAISED_BORDER
)
84 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
85 wxFLAGS_MEMBER(wxBORDER
)
87 // standard window styles
88 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
89 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
90 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
91 wxFLAGS_MEMBER(wxWANTS_CHARS
)
92 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
93 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
94 wxFLAGS_MEMBER(wxVSCROLL
)
95 wxFLAGS_MEMBER(wxHSCROLL
)
97 wxFLAGS_MEMBER(wxCAL_SUNDAY_FIRST
)
98 wxFLAGS_MEMBER(wxCAL_MONDAY_FIRST
)
99 wxFLAGS_MEMBER(wxCAL_SHOW_HOLIDAYS
)
100 wxFLAGS_MEMBER(wxCAL_NO_YEAR_CHANGE
)
101 wxFLAGS_MEMBER(wxCAL_NO_MONTH_CHANGE
)
102 wxFLAGS_MEMBER(wxCAL_SEQUENTIAL_MONTH_SELECTION
)
103 wxFLAGS_MEMBER(wxCAL_SHOW_SURROUNDING_WEEKS
)
105 wxEND_FLAGS( wxCalendarCtrlStyle
)
107 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCalendarCtrl
, wxControl
,"wx/calctrl.h")
109 wxBEGIN_PROPERTIES_TABLE(wxCalendarCtrl
)
110 wxEVENT_RANGE_PROPERTY( Updated
, wxEVT_CALENDAR_SEL_CHANGED
, wxEVT_CALENDAR_WEEKDAY_CLICKED
, wxCalendarEvent
)
111 wxHIDE_PROPERTY( Children
)
112 wxPROPERTY( Date
,wxDateTime
, SetDate
, GetDate
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
113 wxPROPERTY_FLAGS( WindowStyle
, wxCalendarCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
114 wxEND_PROPERTIES_TABLE()
116 wxBEGIN_HANDLERS_TABLE(wxCalendarCtrl
)
117 wxEND_HANDLERS_TABLE()
119 wxCONSTRUCTOR_6( wxCalendarCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxDateTime
, Date
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
121 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl
, wxControl
)
123 IMPLEMENT_DYNAMIC_CLASS(wxCalendarEvent
, wxDateEvent
)
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_SEL_CHANGED
)
130 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DAY_CHANGED
)
131 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_MONTH_CHANGED
)
132 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_YEAR_CHANGED
)
133 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DOUBLECLICKED
)
134 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_WEEKDAY_CLICKED
)
136 // ============================================================================
138 // ============================================================================
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 wxCalendarCtrl::wxCalendarCtrl(wxWindow
*parent
,
146 const wxDateTime
& date
,
150 const wxString
& name
)
154 (void)Create(parent
, id
, date
, pos
, size
, style
, name
);
157 void wxCalendarCtrl::Init()
162 m_staticMonth
= NULL
;
164 m_userChangedYear
= false;
169 wxDateTime::WeekDay wd
;
170 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
172 m_weekdays
[wd
] = wxDateTime::GetWeekDayName(wd
, wxDateTime::Name_Abbr
);
175 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
183 void wxCalendarCtrl::InitColours()
185 m_colHighlightFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
186 m_colHighlightBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
187 m_colBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
188 m_colSorrounding
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
190 m_colHolidayFg
= *wxRED
;
191 // don't set m_colHolidayBg - by default, same as our bg colour
193 m_colHeaderFg
= *wxBLUE
;
194 m_colHeaderBg
= *wxLIGHT_GREY
;
197 bool wxCalendarCtrl::Create(wxWindow
*parent
,
199 const wxDateTime
& date
,
203 const wxString
& name
)
205 if ( !wxControl::Create(parent
, id
, pos
, size
,
206 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
| wxFULL_REPAINT_ON_RESIZE
,
207 wxDefaultValidator
, name
) )
212 // needed to get the arrow keys normally used for the dialog navigation
213 SetWindowStyle(style
| wxWANTS_CHARS
);
215 m_date
= date
.IsValid() ? date
: wxDateTime::Today();
217 m_lowdate
= wxDefaultDateTime
;
218 m_highdate
= wxDefaultDateTime
;
220 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
222 CreateYearSpinCtrl();
223 m_staticYear
= new wxStaticText(GetParent(), wxID_ANY
, m_date
.Format(_T("%Y")),
224 wxDefaultPosition
, wxDefaultSize
,
227 CreateMonthComboBox();
228 m_staticMonth
= new wxStaticText(GetParent(), wxID_ANY
, m_date
.Format(_T("%B")),
229 wxDefaultPosition
, wxDefaultSize
,
233 ShowCurrentControls();
235 // we need to set the position as well because the main control position
236 // is not the same as the one specified in pos if we have the controls
238 SetInitialSize(size
);
241 // Since we don't paint the whole background make sure that the platform
242 // will use the right one.
243 SetBackgroundColour(m_colBackground
);
250 wxCalendarCtrl::~wxCalendarCtrl()
252 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
258 void wxCalendarCtrl::SetWindowStyleFlag(long style
)
260 // changing this style doesn't work because the controls are not
261 // created/shown/hidden accordingly
262 wxASSERT_MSG( (style
& wxCAL_SEQUENTIAL_MONTH_SELECTION
) ==
263 (m_windowStyle
& wxCAL_SEQUENTIAL_MONTH_SELECTION
),
264 _T("wxCAL_SEQUENTIAL_MONTH_SELECTION can't be changed after creation") );
266 wxControl::SetWindowStyleFlag(style
);
269 // ----------------------------------------------------------------------------
270 // Create the wxComboBox and wxSpinCtrl
271 // ----------------------------------------------------------------------------
273 void wxCalendarCtrl::CreateMonthComboBox()
275 m_comboMonth
= new wxComboBox(GetParent(), wxID_ANY
,
280 wxCB_READONLY
| wxCLIP_SIBLINGS
);
283 for ( m
= wxDateTime::Jan
; m
< wxDateTime::Inv_Month
; wxNextMonth(m
) )
285 m_comboMonth
->Append(wxDateTime::GetMonthName(m
));
288 m_comboMonth
->SetSelection(GetDate().GetMonth());
289 m_comboMonth
->SetSize(wxDefaultCoord
,
293 wxSIZE_AUTO_WIDTH
|wxSIZE_AUTO_HEIGHT
);
295 m_comboMonth
->Connect(wxEVT_COMMAND_COMBOBOX_SELECTED
,
296 wxCommandEventHandler(wxCalendarCtrl::OnMonthChange
),
300 void wxCalendarCtrl::CreateYearSpinCtrl()
302 m_spinYear
= new wxSpinCtrl(GetParent(), wxID_ANY
,
303 GetDate().Format(_T("%Y")),
306 wxSP_ARROW_KEYS
| wxCLIP_SIBLINGS
,
307 -4300, 10000, GetDate().GetYear());
309 m_spinYear
->Connect(wxEVT_COMMAND_TEXT_UPDATED
,
310 wxCommandEventHandler(wxCalendarCtrl::OnYearTextChange
),
313 m_spinYear
->Connect(wxEVT_COMMAND_SPINCTRL_UPDATED
,
314 wxCommandEventHandler(wxCalendarCtrl::OnYearChange
),
318 // ----------------------------------------------------------------------------
319 // forward wxWin functions to subcontrols
320 // ----------------------------------------------------------------------------
322 bool wxCalendarCtrl::Destroy()
325 m_staticYear
->Destroy();
327 m_spinYear
->Destroy();
329 m_comboMonth
->Destroy();
331 m_staticMonth
->Destroy();
336 m_staticMonth
= NULL
;
338 return wxControl::Destroy();
341 bool wxCalendarCtrl::Show(bool show
)
343 if ( !wxControl::Show(show
) )
348 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
350 if ( GetMonthControl() )
352 GetMonthControl()->Show(show
);
353 GetYearControl()->Show(show
);
360 bool wxCalendarCtrl::Enable(bool enable
)
362 if ( !wxControl::Enable(enable
) )
367 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
369 GetMonthControl()->Enable(enable
);
370 GetYearControl()->Enable(enable
);
376 // ----------------------------------------------------------------------------
377 // enable/disable month/year controls
378 // ----------------------------------------------------------------------------
380 void wxCalendarCtrl::ShowCurrentControls()
382 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
384 if ( AllowMonthChange() )
386 m_comboMonth
->Show();
387 m_staticMonth
->Hide();
389 if ( AllowYearChange() )
392 m_staticYear
->Hide();
400 m_comboMonth
->Hide();
401 m_staticMonth
->Show();
404 // year change not allowed here
406 m_staticYear
->Show();
408 //else: these controls are not even created, don't show/hide them
411 wxControl
*wxCalendarCtrl::GetMonthControl() const
413 return AllowMonthChange() ? (wxControl
*)m_comboMonth
: (wxControl
*)m_staticMonth
;
416 wxControl
*wxCalendarCtrl::GetYearControl() const
418 return AllowYearChange() ? (wxControl
*)m_spinYear
: (wxControl
*)m_staticYear
;
421 void wxCalendarCtrl::EnableYearChange(bool enable
)
423 if ( enable
!= AllowYearChange() )
425 long style
= GetWindowStyle();
427 style
&= ~wxCAL_NO_YEAR_CHANGE
;
429 style
|= wxCAL_NO_YEAR_CHANGE
;
430 SetWindowStyle(style
);
432 ShowCurrentControls();
433 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
440 void wxCalendarCtrl::EnableMonthChange(bool enable
)
442 if ( enable
!= AllowMonthChange() )
444 long style
= GetWindowStyle();
446 style
&= ~wxCAL_NO_MONTH_CHANGE
;
448 style
|= wxCAL_NO_MONTH_CHANGE
;
449 SetWindowStyle(style
);
451 ShowCurrentControls();
452 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
459 // ----------------------------------------------------------------------------
461 // ----------------------------------------------------------------------------
463 bool wxCalendarCtrl::SetDate(const wxDateTime
& date
)
467 bool sameMonth
= m_date
.GetMonth() == date
.GetMonth(),
468 sameYear
= m_date
.GetYear() == date
.GetYear();
470 if ( IsDateInRange(date
) )
472 if ( sameMonth
&& sameYear
)
474 // just change the day
479 if ( AllowMonthChange() && (AllowYearChange() || sameYear
) )
484 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
486 // update the controls
487 m_comboMonth
->SetSelection(m_date
.GetMonth());
489 if ( AllowYearChange() )
491 if ( !m_userChangedYear
)
492 m_spinYear
->SetValue(m_date
.Format(_T("%Y")));
496 // as the month changed, holidays did too
499 // update the calendar
510 m_userChangedYear
= false;
515 void wxCalendarCtrl::ChangeDay(const wxDateTime
& date
)
517 if ( m_date
!= date
)
519 // we need to refresh the row containing the old date and the one
520 // containing the new one
521 wxDateTime dateOld
= m_date
;
524 RefreshDate(dateOld
);
526 // if the date is in the same row, it was already drawn correctly
527 if ( GetWeek(m_date
) != GetWeek(dateOld
) )
534 void wxCalendarCtrl::SetDateAndNotify(const wxDateTime
& date
)
536 wxDateTime::Tm tm1
= m_date
.GetTm(),
540 if ( tm1
.year
!= tm2
.year
)
541 type
= wxEVT_CALENDAR_YEAR_CHANGED
;
542 else if ( tm1
.mon
!= tm2
.mon
)
543 type
= wxEVT_CALENDAR_MONTH_CHANGED
;
544 else if ( tm1
.mday
!= tm2
.mday
)
545 type
= wxEVT_CALENDAR_DAY_CHANGED
;
551 GenerateEvents(type
, wxEVT_CALENDAR_SEL_CHANGED
);
555 // ----------------------------------------------------------------------------
557 // ----------------------------------------------------------------------------
559 bool wxCalendarCtrl::SetLowerDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
563 if ( !(date
.IsValid()) || ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : true ) )
575 bool wxCalendarCtrl::SetUpperDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
579 if ( !(date
.IsValid()) || ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : true ) )
591 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& lowerdate
/* = wxDefaultDateTime */, const wxDateTime
& upperdate
/* = wxDefaultDateTime */)
596 ( !( lowerdate
.IsValid() ) || ( ( upperdate
.IsValid() ) ? ( lowerdate
<= upperdate
) : true ) ) &&
597 ( !( upperdate
.IsValid() ) || ( ( lowerdate
.IsValid() ) ? ( upperdate
>= lowerdate
) : true ) ) )
599 m_lowdate
= lowerdate
;
600 m_highdate
= upperdate
;
610 // ----------------------------------------------------------------------------
612 // ----------------------------------------------------------------------------
614 wxDateTime
wxCalendarCtrl::GetStartDate() const
616 wxDateTime::Tm tm
= m_date
.GetTm();
618 wxDateTime date
= wxDateTime(1, tm
.mon
, tm
.year
);
621 date
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
622 ? wxDateTime::Mon
: wxDateTime::Sun
);
624 if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
)
626 // We want to offset the calendar if we start on the first..
627 if ( date
.GetDay() == 1 )
629 date
-= wxDateSpan::Week();
636 bool wxCalendarCtrl::IsDateShown(const wxDateTime
& date
) const
638 if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
640 return date
.GetMonth() == m_date
.GetMonth();
648 bool wxCalendarCtrl::IsDateInRange(const wxDateTime
& date
) const
650 // Check if the given date is in the range specified
651 return ( ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : true )
652 && ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : true ) );
655 bool wxCalendarCtrl::ChangeYear(wxDateTime
* target
) const
659 if ( !(IsDateInRange(*target
)) )
661 if ( target
->GetYear() < m_date
.GetYear() )
663 if ( target
->GetYear() >= GetLowerDateLimit().GetYear() )
665 *target
= GetLowerDateLimit();
675 if ( target
->GetYear() <= GetUpperDateLimit().GetYear() )
677 *target
= GetUpperDateLimit();
694 bool wxCalendarCtrl::ChangeMonth(wxDateTime
* target
) const
698 if ( !(IsDateInRange(*target
)) )
702 if ( target
->GetMonth() < m_date
.GetMonth() )
704 *target
= GetLowerDateLimit();
708 *target
= GetUpperDateLimit();
715 size_t wxCalendarCtrl::GetWeek(const wxDateTime
& date
) const
717 size_t retval
= date
.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
718 ? wxDateTime::Monday_First
719 : wxDateTime::Sunday_First
);
721 if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
723 // we need to offset an extra week if we "start" on the 1st of the month
724 wxDateTime::Tm tm
= date
.GetTm();
726 wxDateTime datetest
= wxDateTime(1, tm
.mon
, tm
.year
);
729 datetest
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
730 ? wxDateTime::Mon
: wxDateTime::Sun
);
732 if ( datetest
.GetDay() == 1 )
741 // ----------------------------------------------------------------------------
743 // ----------------------------------------------------------------------------
745 // this is a composite control and it must arrange its parts each time its
746 // size or position changes: the combobox and spinctrl are along the top of
747 // the available area and the calendar takes up therest of the space
749 // the static controls are supposed to be always smaller than combo/spin so we
750 // always use the latter for size calculations and position the static to take
753 // the constants used for the layout
754 #define VERT_MARGIN 5 // distance between combo and calendar
756 #define HORZ_MARGIN 5 // spin
758 #define HORZ_MARGIN 15 // spin
760 wxSize
wxCalendarCtrl::DoGetBestSize() const
762 // calc the size of the calendar
763 ((wxCalendarCtrl
*)this)->RecalcGeometry(); // const_cast
765 wxCoord width
= 7*m_widthCol
,
766 height
= 7*m_heightRow
+ m_rowOffset
+ VERT_MARGIN
;
768 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
770 // the combobox doesn't report its height correctly (it returns the
771 // height including the drop down list) so don't use it
772 height
+= m_spinYear
->GetBestSize().y
;
775 wxCoord w2
= m_comboMonth
->GetBestSize().x
+ HORZ_MARGIN
+ GetCharWidth()*6;
780 if ( !HasFlag(wxBORDER_NONE
) )
782 // the border would clip the last line otherwise
787 wxSize
best(width
, height
);
792 void wxCalendarCtrl::DoSetSize(int x
, int y
,
793 int width
, int height
,
796 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
799 void wxCalendarCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
803 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) && m_staticMonth
)
805 wxSize sizeCombo
= m_comboMonth
->GetEffectiveMinSize();
806 wxSize sizeStatic
= m_staticMonth
->GetSize();
807 wxSize sizeSpin
= m_spinYear
->GetSize();
809 // wxMSW sometimes reports the wrong combo height,
810 // so on this platform we'll use the spin control
813 int maxHeight
= sizeSpin
.y
;
814 int requiredSpinHeight
= -1;
816 int maxHeight
= sizeCombo
.y
;
817 int requiredSpinHeight
= sizeCombo
.y
;
819 int dy
= (maxHeight
- sizeStatic
.y
) / 2;
820 m_comboMonth
->Move(x
, y
);
821 m_staticMonth
->SetSize(x
, y
+ dy
, sizeCombo
.x
, -1, sizeStatic
.y
);
823 int xDiff
= sizeCombo
.x
+ HORZ_MARGIN
;
825 m_spinYear
->SetSize(x
+ xDiff
, y
, width
- xDiff
, requiredSpinHeight
);
826 m_staticYear
->SetSize(x
+ xDiff
, y
+ dy
, width
- xDiff
, sizeStatic
.y
);
828 yDiff
= wxMax(sizeSpin
.y
, maxHeight
) + VERT_MARGIN
;
830 else // no controls on the top
835 wxControl::DoMoveWindow(x
, y
+ yDiff
, width
, height
- yDiff
);
838 void wxCalendarCtrl::DoGetPosition(int *x
, int *y
) const
840 wxControl::DoGetPosition(x
, y
);
842 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) && GetMonthControl() )
844 // our real top corner is not in this position
847 *y
-= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
853 void wxCalendarCtrl::DoGetSize(int *width
, int *height
) const
855 wxControl::DoGetSize(width
, height
);
857 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
859 // our real height is bigger
860 if ( height
&& GetMonthControl())
862 *height
+= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
868 void wxCalendarCtrl::RecalcGeometry()
872 dc
.SetFont(GetFont());
874 // determine the column width (weekday names are not necessarily wider
875 // than the numbers (in some languages), so let's not assume that they are)
877 for ( int day
= 10; day
<= 31; day
++)
880 dc
.GetTextExtent(wxString::Format(wxT("%d"), day
), &width
, &m_heightRow
);
881 if ( width
> m_widthCol
)
883 // 1.5 times the width gives nice margins even if the weekday
885 m_widthCol
= width
+width
/2;
888 wxDateTime::WeekDay wd
;
889 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
892 dc
.GetTextExtent(m_weekdays
[wd
], &width
, &m_heightRow
);
893 if ( width
> m_widthCol
)
899 // leave some margins
903 m_rowOffset
= HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) ? m_heightRow
: 0; // conditional in relation to style
906 // ----------------------------------------------------------------------------
908 // ----------------------------------------------------------------------------
910 void wxCalendarCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
914 dc
.SetFont(GetFont());
919 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
920 m_date
.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
925 wxCoord x0
= wxMax( (GetSize().x
- m_widthCol
*7) /2 , 0 );
927 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
929 // draw the sequential month-selector
931 dc
.SetBackgroundMode(wxTRANSPARENT
);
932 dc
.SetTextForeground(*wxBLACK
);
933 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
934 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
935 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
937 // Get extent of month-name + year
938 wxCoord monthw
, monthh
;
939 wxString headertext
= m_date
.Format(wxT("%B %Y"));
940 dc
.GetTextExtent(headertext
, &monthw
, &monthh
);
942 // draw month-name centered above weekdays
943 wxCoord monthx
= ((m_widthCol
* 7) - monthw
) / 2 + x0
;
944 wxCoord monthy
= ((m_heightRow
- monthh
) / 2) + y
;
945 dc
.DrawText(headertext
, monthx
, monthy
);
947 // calculate the "month-arrows"
948 wxPoint leftarrow
[3];
949 wxPoint rightarrow
[3];
951 int arrowheight
= monthh
/ 2;
953 leftarrow
[0] = wxPoint(0, arrowheight
/ 2);
954 leftarrow
[1] = wxPoint(arrowheight
/ 2, 0);
955 leftarrow
[2] = wxPoint(arrowheight
/ 2, arrowheight
- 1);
957 rightarrow
[0] = wxPoint(0,0);
958 rightarrow
[1] = wxPoint(arrowheight
/ 2, arrowheight
/ 2);
959 rightarrow
[2] = wxPoint(0, arrowheight
- 1);
961 // draw the "month-arrows"
963 wxCoord arrowy
= (m_heightRow
- arrowheight
) / 2;
964 wxCoord larrowx
= (m_widthCol
- (arrowheight
/ 2)) / 2 + x0
;
965 wxCoord rarrowx
= ((m_widthCol
- (arrowheight
/ 2)) / 2) + m_widthCol
*6 + x0
;
966 m_leftArrowRect
= m_rightArrowRect
= wxRect(0,0,0,0);
968 if ( AllowMonthChange() )
970 wxDateTime ldpm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) - wxDateSpan::Day(); // last day prev month
971 // Check if range permits change
972 if ( IsDateInRange(ldpm
) && ( ( ldpm
.GetYear() == m_date
.GetYear() ) ? true : AllowYearChange() ) )
974 m_leftArrowRect
= wxRect(larrowx
- 3, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
975 dc
.SetBrush(*wxBLACK_BRUSH
);
976 dc
.SetPen(*wxBLACK_PEN
);
977 dc
.DrawPolygon(3, leftarrow
, larrowx
, arrowy
, wxWINDING_RULE
);
978 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
979 dc
.DrawRectangle(m_leftArrowRect
);
981 wxDateTime fdnm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) + wxDateSpan::Month(); // first day next month
982 if ( IsDateInRange(fdnm
) && ( ( fdnm
.GetYear() == m_date
.GetYear() ) ? true : AllowYearChange() ) )
984 m_rightArrowRect
= wxRect(rarrowx
- 4, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
985 dc
.SetBrush(*wxBLACK_BRUSH
);
986 dc
.SetPen(*wxBLACK_PEN
);
987 dc
.DrawPolygon(3, rightarrow
, rarrowx
, arrowy
, wxWINDING_RULE
);
988 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
989 dc
.DrawRectangle(m_rightArrowRect
);
996 // first draw the week days
997 if ( IsExposed(x0
, y
, x0
+ 7*m_widthCol
, m_heightRow
) )
1000 wxLogDebug("painting the header");
1003 dc
.SetBackgroundMode(wxTRANSPARENT
);
1004 dc
.SetTextForeground(m_colHeaderFg
);
1005 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
1006 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
1007 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
1009 bool startOnMonday
= (GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1010 for ( int wd
= 0; wd
< 7; wd
++ )
1013 if ( startOnMonday
)
1014 n
= wd
== 6 ? 0 : wd
+ 1;
1018 dc
.GetTextExtent(m_weekdays
[n
], &dayw
, &dayh
);
1019 dc
.DrawText(m_weekdays
[n
], x0
+ (wd
*m_widthCol
) + ((m_widthCol
- dayw
) / 2), y
); // center the day-name
1023 // then the calendar itself
1024 dc
.SetTextForeground(*wxBLACK
);
1025 //dc.SetFont(*wxNORMAL_FONT);
1028 wxDateTime date
= GetStartDate();
1031 wxLogDebug("starting calendar from %s\n",
1032 date
.Format("%a %d-%m-%Y %H:%M:%S").c_str());
1035 dc
.SetBackgroundMode(wxSOLID
);
1036 for ( size_t nWeek
= 1; nWeek
<= 6; nWeek
++, y
+= m_heightRow
)
1038 // if the update region doesn't intersect this row, don't paint it
1039 if ( !IsExposed(x0
, y
, x0
+ 7*m_widthCol
, m_heightRow
- 1) )
1041 date
+= wxDateSpan::Week();
1047 wxLogDebug("painting week %d at y = %d\n", nWeek
, y
);
1050 for ( int wd
= 0; wd
< 7; wd
++ )
1052 dc
.SetTextBackground(m_colBackground
);
1053 if ( IsDateShown(date
) )
1055 // don't use wxDate::Format() which prepends 0s
1056 unsigned int day
= date
.GetDay();
1057 wxString dayStr
= wxString::Format(_T("%u"), day
);
1059 dc
.GetTextExtent(dayStr
, &width
, (wxCoord
*)NULL
);
1061 bool changedColours
= false,
1062 changedFont
= false;
1065 wxCalendarDateAttr
*attr
= NULL
;
1067 if ( date
.GetMonth() != m_date
.GetMonth() || !IsDateInRange(date
) )
1069 // surrounding week or out-of-range
1071 dc
.SetTextForeground(m_colSorrounding
);
1072 changedColours
= true;
1076 isSel
= date
.IsSameDate(m_date
);
1077 attr
= m_attrs
[day
- 1];
1081 dc
.SetTextForeground(m_colHighlightFg
);
1082 dc
.SetTextBackground(m_colHighlightBg
);
1084 changedColours
= true;
1088 wxColour colFg
, colBg
;
1090 if ( attr
->IsHoliday() )
1092 colFg
= m_colHolidayFg
;
1093 colBg
= m_colHolidayBg
;
1097 colFg
= attr
->GetTextColour();
1098 colBg
= attr
->GetBackgroundColour();
1103 dc
.SetTextForeground(colFg
);
1104 changedColours
= true;
1109 dc
.SetTextBackground(colBg
);
1110 changedColours
= true;
1113 if ( attr
->HasFont() )
1115 dc
.SetFont(attr
->GetFont());
1121 wxCoord x
= wd
*m_widthCol
+ (m_widthCol
- width
) / 2 + x0
;
1122 dc
.DrawText(dayStr
, x
, y
+ 1);
1124 if ( !isSel
&& attr
&& attr
->HasBorder() )
1127 if ( attr
->HasBorderColour() )
1129 colBorder
= attr
->GetBorderColour();
1133 colBorder
= GetForegroundColour();
1136 wxPen
pen(colBorder
, 1, wxSOLID
);
1138 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1140 switch ( attr
->GetBorder() )
1142 case wxCAL_BORDER_SQUARE
:
1143 dc
.DrawRectangle(x
- 2, y
,
1144 width
+ 4, m_heightRow
);
1147 case wxCAL_BORDER_ROUND
:
1148 dc
.DrawEllipse(x
- 2, y
,
1149 width
+ 4, m_heightRow
);
1153 wxFAIL_MSG(_T("unknown border type"));
1157 if ( changedColours
)
1159 dc
.SetTextForeground(GetForegroundColour());
1160 dc
.SetTextBackground(GetBackgroundColour());
1165 dc
.SetFont(GetFont());
1168 //else: just don't draw it
1170 date
+= wxDateSpan::Day();
1174 // Greying out out-of-range background
1175 bool showSurrounding
= (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) != 0;
1177 date
= ( showSurrounding
) ? GetStartDate() : wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear());
1178 if ( !IsDateInRange(date
) )
1180 wxDateTime firstOOR
= GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1182 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1183 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1185 HighlightRange(&dc
, date
, firstOOR
, wxTRANSPARENT_PEN
, &oorbrush
);
1188 date
= ( showSurrounding
) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear());
1189 if ( !IsDateInRange(date
) )
1191 wxDateTime firstOOR
= GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1193 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1194 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1196 HighlightRange(&dc
, firstOOR
, date
, wxTRANSPARENT_PEN
, &oorbrush
);
1200 wxLogDebug("+++ finished painting");
1204 void wxCalendarCtrl::RefreshDate(const wxDateTime
& date
)
1210 // always refresh the whole row at once because our OnPaint() will draw
1211 // the whole row anyhow - and this allows the small optimisation in
1212 // OnClick() below to work
1213 rect
.x
= wxMax( (GetSize().x
- m_widthCol
*7) /2 , 0 );
1215 rect
.y
= (m_heightRow
* GetWeek(date
)) + m_rowOffset
;
1217 rect
.width
= 7*m_widthCol
;
1218 rect
.height
= m_heightRow
;
1221 // VZ: for some reason, the selected date seems to occupy more space under
1222 // MSW - this is probably some bug in the font size calculations, but I
1223 // don't know where exactly. This fix is ugly and leads to more
1224 // refreshes than really needed, but without it the selected days
1225 // leaves even more ugly underscores on screen.
1230 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
1233 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1236 Refresh(true, &rect
);
1239 void wxCalendarCtrl::HighlightRange(wxPaintDC
* pDC
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, const wxPen
* pPen
, const wxBrush
* pBrush
)
1241 // Highlights the given range using pen and brush
1242 // Does nothing if todate < fromdate
1246 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate
.Format("%d %m %Y"), todate
.Format("%d %m %Y"));
1249 if ( todate
>= fromdate
)
1256 // implicit: both dates must be currently shown - checked by GetDateCoord
1257 if ( GetDateCoord(fromdate
, &fd
, &fw
) && GetDateCoord(todate
, &td
, &tw
) )
1260 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd
, fw
, td
, tw
);
1262 if ( ( (tw
- fw
) == 1 ) && ( td
< fd
) )
1264 // special case: interval 7 days or less not in same week
1265 // split in two separate intervals
1266 wxDateTime tfd
= fromdate
+ wxDateSpan::Days(7-fd
);
1267 wxDateTime ftd
= tfd
+ wxDateSpan::Day();
1269 wxLogDebug("Highlight: Separate segments");
1272 HighlightRange(pDC
, fromdate
, tfd
, pPen
, pBrush
);
1273 HighlightRange(pDC
, ftd
, todate
, pPen
, pBrush
);
1278 wxPoint corners
[8]; // potentially 8 corners in polygon
1279 wxCoord x0
= wxMax( (GetSize().x
- m_widthCol
*7) /2 , 0 );
1283 // simple case: same week
1285 corners
[0] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
);
1286 corners
[1] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, ((fw
+ 1 ) * m_heightRow
) + m_rowOffset
);
1287 corners
[2] = wxPoint(x0
+ td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
);
1288 corners
[3] = wxPoint(x0
+ td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
);
1293 // "complex" polygon
1294 corners
[cidx
] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1298 corners
[cidx
] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1299 corners
[cidx
] = wxPoint(x0
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1302 corners
[cidx
] = wxPoint(x0
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1303 corners
[cidx
] = wxPoint(x0
+ td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1307 corners
[cidx
] = wxPoint(x0
+ td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1308 corners
[cidx
] = wxPoint(x0
+ 7 * m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1311 corners
[cidx
] = wxPoint(x0
+ 7 * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1317 pDC
->SetBrush(*pBrush
);
1319 pDC
->DrawPolygon(numpoints
, corners
);
1325 wxLogDebug("--- HighlightRange ---");
1329 bool wxCalendarCtrl::GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const
1334 wxLogDebug("+++ GetDateCoord: (%s) +++", date
.Format("%d %m %Y"));
1337 if ( IsDateShown(date
) )
1339 bool startOnMonday
= ( GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1342 *day
= date
.GetWeekDay();
1344 if ( *day
== 0 ) // sunday
1346 *day
= ( startOnMonday
) ? 7 : 1;
1350 *day
+= ( startOnMonday
) ? 0 : 1;
1353 int targetmonth
= date
.GetMonth() + (12 * date
.GetYear());
1354 int thismonth
= m_date
.GetMonth() + (12 * m_date
.GetYear());
1357 if ( targetmonth
== thismonth
)
1359 *week
= GetWeek(date
);
1363 if ( targetmonth
< thismonth
)
1365 *week
= 1; // trivial
1367 else // targetmonth > thismonth
1373 // get the datecoord of the last day in the month currently shown
1375 wxLogDebug(" +++ LDOM +++");
1377 GetDateCoord(ldcm
.SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear()), &lastday
, &lastweek
);
1379 wxLogDebug(" --- LDOM ---");
1382 wxTimeSpan span
= date
- ldcm
;
1384 int daysfromlast
= span
.GetDays();
1386 wxLogDebug("daysfromlast: %i", daysfromlast
);
1388 if ( daysfromlast
+ lastday
> 7 ) // past week boundary
1390 int wholeweeks
= (daysfromlast
/ 7);
1391 *week
= wholeweeks
+ lastweek
;
1392 if ( (daysfromlast
- (7 * wholeweeks
) + lastday
) > 7 )
1412 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date
.Format("%d %m %Y"), *day
, *week
);
1418 // ----------------------------------------------------------------------------
1420 // ----------------------------------------------------------------------------
1422 void wxCalendarCtrl::OnDClick(wxMouseEvent
& event
)
1424 if ( HitTest(event
.GetPosition()) != wxCAL_HITTEST_DAY
)
1430 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1434 void wxCalendarCtrl::OnClick(wxMouseEvent
& event
)
1437 wxDateTime::WeekDay wday
;
1438 switch ( HitTest(event
.GetPosition(), &date
, &wday
) )
1440 case wxCAL_HITTEST_DAY
:
1441 if ( IsDateInRange(date
) )
1445 GenerateEvents(wxEVT_CALENDAR_DAY_CHANGED
,
1446 wxEVT_CALENDAR_SEL_CHANGED
);
1450 case wxCAL_HITTEST_HEADER
:
1452 wxCalendarEvent
eventWd(this, wxEVT_CALENDAR_WEEKDAY_CLICKED
);
1453 eventWd
.m_wday
= wday
;
1454 (void)GetEventHandler()->ProcessEvent(eventWd
);
1458 case wxCAL_HITTEST_DECMONTH
:
1459 case wxCAL_HITTEST_INCMONTH
:
1460 case wxCAL_HITTEST_SURROUNDING_WEEK
:
1461 SetDateAndNotify(date
); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1465 wxFAIL_MSG(_T("unknown hittest code"));
1468 case wxCAL_HITTEST_NOWHERE
:
1474 wxCalendarHitTestResult
wxCalendarCtrl::HitTest(const wxPoint
& pos
,
1476 wxDateTime::WeekDay
*wd
)
1479 // use the correct x-pos
1480 wxCoord x0
= wxMax((GetSize().x
- m_widthCol
*7) /2, 0);
1481 wxPoint pos_corr
= pos
;
1484 wxCoord y
= pos_corr
.y
;
1486 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1487 if ( (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
1491 // we need to find out if the hit is on left arrow, on month or on right arrow
1493 if ( wxRegion(m_leftArrowRect
).Contains(pos_corr
) == wxInRegion
)
1497 if ( IsDateInRange(m_date
- wxDateSpan::Month()) )
1499 *date
= m_date
- wxDateSpan::Month();
1503 *date
= GetLowerDateLimit();
1507 return wxCAL_HITTEST_DECMONTH
;
1510 if ( wxRegion(m_rightArrowRect
).Contains(pos_corr
) == wxInRegion
)
1514 if ( IsDateInRange(m_date
+ wxDateSpan::Month()) )
1516 *date
= m_date
+ wxDateSpan::Month();
1520 *date
= GetUpperDateLimit();
1524 return wxCAL_HITTEST_INCMONTH
;
1529 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1532 int wday
= pos_corr
.x
/ m_widthCol
;
1533 // if ( y < m_heightRow )
1534 if ( y
< (m_heightRow
+ m_rowOffset
) )
1536 if ( y
> m_rowOffset
)
1540 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST
)
1542 wday
= wday
== 6 ? 0 : wday
+ 1;
1545 *wd
= (wxDateTime::WeekDay
)wday
;
1548 return wxCAL_HITTEST_HEADER
;
1552 return wxCAL_HITTEST_NOWHERE
;
1556 // int week = (y - m_heightRow) / m_heightRow;
1557 int week
= (y
- (m_heightRow
+ m_rowOffset
)) / m_heightRow
;
1558 if ( week
>= 6 || wday
>= 7 )
1560 return wxCAL_HITTEST_NOWHERE
;
1563 wxDateTime dt
= GetStartDate() + wxDateSpan::Days(7*week
+ wday
);
1565 if ( IsDateShown(dt
) )
1570 if ( dt
.GetMonth() == m_date
.GetMonth() )
1573 return wxCAL_HITTEST_DAY
;
1577 return wxCAL_HITTEST_SURROUNDING_WEEK
;
1582 return wxCAL_HITTEST_NOWHERE
;
1586 // ----------------------------------------------------------------------------
1587 // subcontrols events handling
1588 // ----------------------------------------------------------------------------
1590 void wxCalendarCtrl::OnMonthChange(wxCommandEvent
& event
)
1592 wxDateTime::Tm tm
= m_date
.GetTm();
1594 wxDateTime::Month mon
= (wxDateTime::Month
)event
.GetInt();
1595 if ( tm
.mday
> wxDateTime::GetNumberOfDays(mon
, tm
.year
) )
1597 tm
.mday
= wxDateTime::GetNumberOfDays(mon
, tm
.year
);
1600 wxDateTime target
= wxDateTime(tm
.mday
, mon
, tm
.year
);
1602 ChangeMonth(&target
);
1603 SetDateAndNotify(target
);
1606 void wxCalendarCtrl::OnYearChange(wxCommandEvent
& event
)
1608 int year
= (int)event
.GetInt();
1609 if ( year
== INT_MIN
)
1611 // invalid year in the spin control, ignore it
1615 wxDateTime::Tm tm
= m_date
.GetTm();
1617 if ( tm
.mday
> wxDateTime::GetNumberOfDays(tm
.mon
, year
) )
1619 tm
.mday
= wxDateTime::GetNumberOfDays(tm
.mon
, year
);
1622 wxDateTime target
= wxDateTime(tm
.mday
, tm
.mon
, year
);
1624 if ( ChangeYear(&target
) )
1626 SetDateAndNotify(target
);
1630 // In this case we don't want to change the date. That would put us
1631 // inside the same year but a strange number of months forward/back..
1632 m_spinYear
->SetValue(target
.GetYear());
1636 void wxCalendarCtrl::OnYearTextChange(wxCommandEvent
& event
)
1638 SetUserChangedYear();
1639 OnYearChange(event
);
1642 // Responds to colour changes, and passes event on to children.
1643 void wxCalendarCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1648 // Propagate the event to the children
1649 wxControl::OnSysColourChanged(event
);
1651 // Redraw control area
1652 SetBackgroundColour(m_colBackground
);
1656 // ----------------------------------------------------------------------------
1657 // keyboard interface
1658 // ----------------------------------------------------------------------------
1660 void wxCalendarCtrl::OnChar(wxKeyEvent
& event
)
1663 switch ( event
.GetKeyCode() )
1667 target
= m_date
+ wxDateSpan::Year();
1668 if ( ChangeYear(&target
) )
1670 SetDateAndNotify(target
);
1676 target
= m_date
- wxDateSpan::Year();
1677 if ( ChangeYear(&target
) )
1679 SetDateAndNotify(target
);
1684 target
= m_date
- wxDateSpan::Month();
1685 ChangeMonth(&target
);
1686 SetDateAndNotify(target
); // always
1690 target
= m_date
+ wxDateSpan::Month();
1691 ChangeMonth(&target
);
1692 SetDateAndNotify(target
); // always
1696 if ( event
.ControlDown() )
1698 target
= wxDateTime(m_date
).SetToNextWeekDay(
1699 GetWindowStyle() & wxCAL_MONDAY_FIRST
1700 ? wxDateTime::Sun
: wxDateTime::Sat
);
1701 if ( !IsDateInRange(target
) )
1703 target
= GetUpperDateLimit();
1705 SetDateAndNotify(target
);
1708 SetDateAndNotify(m_date
+ wxDateSpan::Day());
1712 if ( event
.ControlDown() )
1714 target
= wxDateTime(m_date
).SetToPrevWeekDay(
1715 GetWindowStyle() & wxCAL_MONDAY_FIRST
1716 ? wxDateTime::Mon
: wxDateTime::Sun
);
1717 if ( !IsDateInRange(target
) )
1719 target
= GetLowerDateLimit();
1721 SetDateAndNotify(target
);
1724 SetDateAndNotify(m_date
- wxDateSpan::Day());
1728 SetDateAndNotify(m_date
- wxDateSpan::Week());
1732 SetDateAndNotify(m_date
+ wxDateSpan::Week());
1736 if ( event
.ControlDown() )
1737 SetDateAndNotify(wxDateTime::Today());
1739 SetDateAndNotify(wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear()));
1743 SetDateAndNotify(wxDateTime(m_date
).SetToLastMonthDay());
1747 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1755 // ----------------------------------------------------------------------------
1756 // holidays handling
1757 // ----------------------------------------------------------------------------
1759 void wxCalendarCtrl::EnableHolidayDisplay(bool display
)
1761 long style
= GetWindowStyle();
1763 style
|= wxCAL_SHOW_HOLIDAYS
;
1765 style
&= ~wxCAL_SHOW_HOLIDAYS
;
1767 SetWindowStyle(style
);
1772 ResetHolidayAttrs();
1777 void wxCalendarCtrl::SetHolidayAttrs()
1779 if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS
)
1781 ResetHolidayAttrs();
1783 wxDateTime::Tm tm
= m_date
.GetTm();
1784 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
1785 dtEnd
= dtStart
.GetLastMonthDay();
1787 wxDateTimeArray hol
;
1788 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
1790 size_t count
= hol
.GetCount();
1791 for ( size_t n
= 0; n
< count
; n
++ )
1793 SetHoliday(hol
[n
].GetDay());
1798 void wxCalendarCtrl::SetHoliday(size_t day
)
1800 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in SetHoliday") );
1802 wxCalendarDateAttr
*attr
= GetAttr(day
);
1805 attr
= new wxCalendarDateAttr
;
1808 attr
->SetHoliday(true);
1810 // can't use SetAttr() because it would delete this pointer
1811 m_attrs
[day
- 1] = attr
;
1814 void wxCalendarCtrl::ResetHolidayAttrs()
1816 for ( size_t day
= 0; day
< 31; day
++ )
1820 m_attrs
[day
]->SetHoliday(false);
1828 wxCalendarCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
1830 // Use the same color scheme as wxListBox
1831 return wxListBox::GetClassDefaultAttributes(variant
);
1834 #endif // wxUSE_CALENDARCTRL