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 license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "calctrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/dcclient.h"
33 #include "wx/settings.h"
35 #include "wx/combobox.h"
36 #include "wx/stattext.h"
37 #include "wx/textctrl.h"
40 #if wxUSE_CALENDARCTRL
42 #include "wx/spinctrl.h"
44 #include "wx/calctrl.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 class wxMonthComboBox
: public wxComboBox
55 wxMonthComboBox(wxCalendarCtrl
*cal
);
57 void OnMonthChange(wxCommandEvent
& event
) { m_cal
->OnMonthChange(event
); }
60 wxCalendarCtrl
*m_cal
;
65 class wxYearSpinCtrl
: public wxSpinCtrl
68 wxYearSpinCtrl(wxCalendarCtrl
*cal
);
70 void OnYearTextChange(wxCommandEvent
& event
) { m_cal
->OnYearChange(event
); }
71 void OnYearChange(wxSpinEvent
& event
) { m_cal
->OnYearChange(event
); }
74 wxCalendarCtrl
*m_cal
;
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 BEGIN_EVENT_TABLE(wxCalendarCtrl
, wxControl
)
84 EVT_PAINT(wxCalendarCtrl::OnPaint
)
86 EVT_CHAR(wxCalendarCtrl::OnChar
)
88 EVT_LEFT_DOWN(wxCalendarCtrl::OnClick
)
89 EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick
)
92 BEGIN_EVENT_TABLE(wxMonthComboBox
, wxComboBox
)
93 EVT_COMBOBOX(-1, wxMonthComboBox::OnMonthChange
)
96 BEGIN_EVENT_TABLE(wxYearSpinCtrl
, wxSpinCtrl
)
97 EVT_TEXT(-1, wxYearSpinCtrl::OnYearTextChange
)
98 EVT_SPINCTRL(-1, wxYearSpinCtrl::OnYearChange
)
101 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl
, wxControl
)
102 IMPLEMENT_DYNAMIC_CLASS(wxCalendarEvent
, wxCommandEvent
)
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_SEL_CHANGED
)
109 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DAY_CHANGED
)
110 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_MONTH_CHANGED
)
111 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_YEAR_CHANGED
)
112 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DOUBLECLICKED
)
113 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_WEEKDAY_CLICKED
)
115 // ============================================================================
117 // ============================================================================
119 // ----------------------------------------------------------------------------
120 // wxMonthComboBox and wxYearSpinCtrl
121 // ----------------------------------------------------------------------------
123 wxMonthComboBox::wxMonthComboBox(wxCalendarCtrl
*cal
)
124 : wxComboBox(cal
->GetParent(), -1,
129 wxCB_READONLY
| wxCLIP_SIBLINGS
)
134 for ( m
= wxDateTime::Jan
; m
< wxDateTime::Inv_Month
; wxNextMonth(m
) )
136 Append(wxDateTime::GetMonthName(m
));
139 SetSelection(m_cal
->GetDate().GetMonth());
140 SetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
|wxSIZE_AUTO_HEIGHT
);
143 wxYearSpinCtrl::wxYearSpinCtrl(wxCalendarCtrl
*cal
)
144 : wxSpinCtrl(cal
->GetParent(), -1,
145 cal
->GetDate().Format(_T("%Y")),
148 wxSP_ARROW_KEYS
| wxCLIP_SIBLINGS
,
149 -4300, 10000, cal
->GetDate().GetYear())
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 wxCalendarCtrl::wxCalendarCtrl(wxWindow
*parent
,
160 const wxDateTime
& date
,
164 const wxString
& name
)
168 (void)Create(parent
, id
, date
, pos
, size
, style
, name
);
171 void wxCalendarCtrl::Init()
176 m_userChangedYear
= FALSE
;
181 wxDateTime::WeekDay wd
;
182 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
184 m_weekdays
[wd
] = wxDateTime::GetWeekDayName(wd
, wxDateTime::Name_Abbr
);
187 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
192 m_colHighlightFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
193 m_colHighlightBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
195 m_colHolidayFg
= *wxRED
;
196 // don't set m_colHolidayBg - by default, same as our bg colour
198 m_colHeaderFg
= *wxBLUE
;
199 m_colHeaderBg
= *wxLIGHT_GREY
;
202 bool wxCalendarCtrl::Create(wxWindow
*parent
,
204 const wxDateTime
& date
,
208 const wxString
& name
)
210 if ( !wxControl::Create(parent
, id
, pos
, size
,
211 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
212 wxDefaultValidator
, name
) )
217 // needed to get the arrow keys normally used for the dialog navigation
218 SetWindowStyle(style
| wxWANTS_CHARS
);
220 m_date
= date
.IsValid() ? date
: wxDateTime::Today();
222 m_lowdate
= wxDefaultDateTime
;
223 m_highdate
= wxDefaultDateTime
;
225 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
227 m_spinYear
= new wxYearSpinCtrl(this);
228 m_staticYear
= new wxStaticText(GetParent(), -1, m_date
.Format(_T("%Y")),
229 wxDefaultPosition
, wxDefaultSize
,
232 m_comboMonth
= new wxMonthComboBox(this);
233 m_staticMonth
= new wxStaticText(GetParent(), -1, m_date
.Format(_T("%B")),
234 wxDefaultPosition
, wxDefaultSize
,
238 ShowCurrentControls();
241 if ( size
.x
== -1 || size
.y
== -1 )
243 sizeReal
= DoGetBestSize();
254 // we need to set the position as well because the main control position
255 // is not the same as the one specified in pos if we have the controls
257 SetSize(pos
.x
, pos
.y
, sizeReal
.x
, sizeReal
.y
);
259 SetBackgroundColour(*wxWHITE
);
260 SetFont(*wxSWISS_FONT
);
267 wxCalendarCtrl::~wxCalendarCtrl()
269 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
275 // ----------------------------------------------------------------------------
276 // forward wxWin functions to subcontrols
277 // ----------------------------------------------------------------------------
279 bool wxCalendarCtrl::Destroy()
282 m_staticYear
->Destroy();
284 m_spinYear
->Destroy();
286 m_comboMonth
->Destroy();
288 m_staticMonth
->Destroy();
293 m_staticMonth
= NULL
;
295 return wxControl::Destroy();
298 bool wxCalendarCtrl::Show(bool show
)
300 if ( !wxControl::Show(show
) )
305 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
307 if ( GetMonthControl() )
309 GetMonthControl()->Show(show
);
310 GetYearControl()->Show(show
);
317 bool wxCalendarCtrl::Enable(bool enable
)
319 if ( !wxControl::Enable(enable
) )
324 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
326 GetMonthControl()->Enable(enable
);
327 GetYearControl()->Enable(enable
);
333 // ----------------------------------------------------------------------------
334 // enable/disable month/year controls
335 // ----------------------------------------------------------------------------
337 void wxCalendarCtrl::ShowCurrentControls()
339 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
341 if ( AllowMonthChange() )
343 m_comboMonth
->Show();
344 m_staticMonth
->Hide();
346 if ( AllowYearChange() )
349 m_staticYear
->Hide();
357 m_comboMonth
->Hide();
358 m_staticMonth
->Show();
361 // year change not allowed here
363 m_staticYear
->Show();
367 wxControl
*wxCalendarCtrl::GetMonthControl() const
369 return AllowMonthChange() ? (wxControl
*)m_comboMonth
: (wxControl
*)m_staticMonth
;
372 wxControl
*wxCalendarCtrl::GetYearControl() const
374 return AllowYearChange() ? (wxControl
*)m_spinYear
: (wxControl
*)m_staticYear
;
377 void wxCalendarCtrl::EnableYearChange(bool enable
)
379 if ( enable
!= AllowYearChange() )
381 long style
= GetWindowStyle();
383 style
&= ~wxCAL_NO_YEAR_CHANGE
;
385 style
|= wxCAL_NO_YEAR_CHANGE
;
386 SetWindowStyle(style
);
388 ShowCurrentControls();
389 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
396 void wxCalendarCtrl::EnableMonthChange(bool enable
)
398 if ( enable
!= AllowMonthChange() )
400 long style
= GetWindowStyle();
402 style
&= ~wxCAL_NO_MONTH_CHANGE
;
404 style
|= wxCAL_NO_MONTH_CHANGE
;
405 SetWindowStyle(style
);
407 ShowCurrentControls();
408 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
415 // ----------------------------------------------------------------------------
417 // ----------------------------------------------------------------------------
419 bool wxCalendarCtrl::SetDate(const wxDateTime
& date
)
423 bool sameMonth
= m_date
.GetMonth() == date
.GetMonth(),
424 sameYear
= m_date
.GetYear() == date
.GetYear();
426 if ( IsDateInRange(date
) )
428 if ( sameMonth
&& sameYear
)
430 // just change the day
435 if ( AllowMonthChange() && (AllowYearChange() || sameYear
) )
440 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
442 // update the controls
443 m_comboMonth
->SetSelection(m_date
.GetMonth());
445 if ( AllowYearChange() )
447 if ( !m_userChangedYear
)
448 m_spinYear
->SetValue(m_date
.Format(_T("%Y")));
449 else // don't overwrite what the user typed in
450 m_userChangedYear
= FALSE
;
454 // as the month changed, holidays did too
457 // update the calendar
471 void wxCalendarCtrl::ChangeDay(const wxDateTime
& date
)
473 if ( m_date
!= date
)
475 // we need to refresh the row containing the old date and the one
476 // containing the new one
477 wxDateTime dateOld
= m_date
;
480 RefreshDate(dateOld
);
482 // if the date is in the same row, it was already drawn correctly
483 if ( GetWeek(m_date
) != GetWeek(dateOld
) )
490 void wxCalendarCtrl::SetDateAndNotify(const wxDateTime
& date
)
492 wxDateTime::Tm tm1
= m_date
.GetTm(),
496 if ( tm1
.year
!= tm2
.year
)
497 type
= wxEVT_CALENDAR_YEAR_CHANGED
;
498 else if ( tm1
.mon
!= tm2
.mon
)
499 type
= wxEVT_CALENDAR_MONTH_CHANGED
;
500 else if ( tm1
.mday
!= tm2
.mday
)
501 type
= wxEVT_CALENDAR_DAY_CHANGED
;
507 GenerateEvents(type
, wxEVT_CALENDAR_SEL_CHANGED
);
511 // ----------------------------------------------------------------------------
513 // ----------------------------------------------------------------------------
515 bool wxCalendarCtrl::SetLowerDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
519 if ( !(date
.IsValid()) || ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : TRUE
) )
531 bool wxCalendarCtrl::SetUpperDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
535 if ( !(date
.IsValid()) || ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : TRUE
) )
547 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& lowerdate
/* = wxDefaultDateTime */, const wxDateTime
& upperdate
/* = wxDefaultDateTime */)
552 ( !( lowerdate
.IsValid() ) || ( ( upperdate
.IsValid() ) ? ( lowerdate
<= upperdate
) : TRUE
) ) &&
553 ( !( upperdate
.IsValid() ) || ( ( lowerdate
.IsValid() ) ? ( upperdate
>= lowerdate
) : TRUE
) ) )
555 m_lowdate
= lowerdate
;
556 m_highdate
= upperdate
;
566 // ----------------------------------------------------------------------------
568 // ----------------------------------------------------------------------------
570 wxDateTime
wxCalendarCtrl::GetStartDate() const
572 wxDateTime::Tm tm
= m_date
.GetTm();
574 wxDateTime date
= wxDateTime(1, tm
.mon
, tm
.year
);
577 date
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
578 ? wxDateTime::Mon
: wxDateTime::Sun
);
580 if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
)
582 // We want to offset the calendar if we start on the first..
583 if ( date
.GetDay() == 1 )
585 date
-= wxDateSpan::Week();
592 bool wxCalendarCtrl::IsDateShown(const wxDateTime
& date
) const
594 if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
596 return date
.GetMonth() == m_date
.GetMonth();
604 bool wxCalendarCtrl::IsDateInRange(const wxDateTime
& date
) const
607 // Check if the given date is in the range specified
608 retval
= ( ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : TRUE
)
609 && ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : TRUE
) );
613 bool wxCalendarCtrl::ChangeYear(wxDateTime
* target
) const
617 if ( !(IsDateInRange(*target
)) )
619 if ( target
->GetYear() < m_date
.GetYear() )
621 if ( target
->GetYear() >= GetLowerDateLimit().GetYear() )
623 *target
= GetLowerDateLimit();
633 if ( target
->GetYear() <= GetUpperDateLimit().GetYear() )
635 *target
= GetUpperDateLimit();
652 bool wxCalendarCtrl::ChangeMonth(wxDateTime
* target
) const
656 if ( !(IsDateInRange(*target
)) )
660 if ( target
->GetMonth() < m_date
.GetMonth() )
662 *target
= GetLowerDateLimit();
666 *target
= GetUpperDateLimit();
673 size_t wxCalendarCtrl::GetWeek(const wxDateTime
& date
) const
675 size_t retval
= date
.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
676 ? wxDateTime::Monday_First
677 : wxDateTime::Sunday_First
);
679 if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
681 // we need to offset an extra week if we "start" on the 1st of the month
682 wxDateTime::Tm tm
= date
.GetTm();
684 wxDateTime datetest
= wxDateTime(1, tm
.mon
, tm
.year
);
687 datetest
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
688 ? wxDateTime::Mon
: wxDateTime::Sun
);
690 if ( datetest
.GetDay() == 1 )
699 // ----------------------------------------------------------------------------
701 // ----------------------------------------------------------------------------
703 // this is a composite control and it must arrange its parts each time its
704 // size or position changes: the combobox and spinctrl are along the top of
705 // the available area and the calendar takes up therest of the space
707 // the static controls are supposed to be always smaller than combo/spin so we
708 // always use the latter for size calculations and position the static to take
711 // the constants used for the layout
712 #define VERT_MARGIN 5 // distance between combo and calendar
714 #define HORZ_MARGIN 5 // spin
716 #define HORZ_MARGIN 15 // spin
718 wxSize
wxCalendarCtrl::DoGetBestSize() const
720 // calc the size of the calendar
721 ((wxCalendarCtrl
*)this)->RecalcGeometry(); // const_cast
723 wxCoord width
= 7*m_widthCol
,
724 height
= 7*m_heightRow
+ m_rowOffset
+ VERT_MARGIN
;
726 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
728 // the combobox doesn't report its height correctly (it returns the
729 // height including the drop down list) so don't use it
730 height
+= m_spinYear
->GetBestSize().y
;
733 if ( !HasFlag(wxBORDER_NONE
) )
735 // the border would clip the last line otherwise
740 return wxSize(width
, height
);
743 void wxCalendarCtrl::DoSetSize(int x
, int y
,
744 int width
, int height
,
747 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
750 void wxCalendarCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
754 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
756 wxSize sizeCombo
= m_comboMonth
->GetSize();
757 wxSize sizeStatic
= m_staticMonth
->GetSize();
759 int dy
= (sizeCombo
.y
- sizeStatic
.y
) / 2;
761 m_comboMonth
->Move(x
, y
);
762 m_staticMonth
->SetSize(x
, y
+ dy
, sizeCombo
.x
, sizeStatic
.y
);
764 int xDiff
= sizeCombo
.x
+ HORZ_MARGIN
;
766 m_spinYear
->SetSize(x
+ xDiff
, y
, width
- xDiff
, sizeCombo
.y
);
767 m_staticYear
->SetSize(x
+ xDiff
, y
+ dy
, width
- xDiff
, sizeStatic
.y
);
769 wxSize sizeSpin
= m_spinYear
->GetSize();
770 yDiff
= wxMax(sizeSpin
.y
, sizeCombo
.y
) + VERT_MARGIN
;
772 else // no controls on the top
777 wxControl::DoMoveWindow(x
, y
+ yDiff
, width
, height
- yDiff
);
780 void wxCalendarCtrl::DoGetPosition(int *x
, int *y
) const
782 wxControl::DoGetPosition(x
, y
);
784 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
786 // our real top corner is not in this position
789 *y
-= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
794 void wxCalendarCtrl::DoGetSize(int *width
, int *height
) const
796 wxControl::DoGetSize(width
, height
);
798 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
800 // our real height is bigger
801 if ( height
&& GetMonthControl())
803 *height
+= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
808 void wxCalendarCtrl::RecalcGeometry()
810 if ( m_widthCol
!= 0 )
817 // determine the column width (we assume that the weekday names are always
818 // wider (in any language) than the numbers)
820 wxDateTime::WeekDay wd
;
821 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
824 dc
.GetTextExtent(m_weekdays
[wd
], &width
, &m_heightRow
);
825 if ( width
> m_widthCol
)
831 // leave some margins
835 m_rowOffset
= (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) ? m_heightRow
: 0; // conditional in relation to style
838 // ----------------------------------------------------------------------------
840 // ----------------------------------------------------------------------------
842 void wxCalendarCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
851 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
852 m_date
.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
858 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
860 // draw the sequential month-selector
862 dc
.SetBackgroundMode(wxTRANSPARENT
);
863 dc
.SetTextForeground(*wxBLACK
);
864 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
865 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
866 dc
.DrawRectangle(0, y
, 7*m_widthCol
, m_heightRow
);
868 // Get extent of month-name + year
869 wxCoord monthw
, monthh
;
870 wxString headertext
= m_date
.Format(wxT("%B %Y"));
871 dc
.GetTextExtent(headertext
, &monthw
, &monthh
);
873 // draw month-name centered above weekdays
874 wxCoord monthx
= ((m_widthCol
* 7) - monthw
) / 2;
875 wxCoord monthy
= ((m_heightRow
- monthh
) / 2) + y
;
876 dc
.DrawText(headertext
, monthx
, monthy
);
878 // calculate the "month-arrows"
879 wxPoint leftarrow
[3];
880 wxPoint rightarrow
[3];
882 int arrowheight
= monthh
/ 2;
884 leftarrow
[0] = wxPoint(0, arrowheight
/ 2);
885 leftarrow
[1] = wxPoint(arrowheight
/ 2, 0);
886 leftarrow
[2] = wxPoint(arrowheight
/ 2, arrowheight
- 1);
888 rightarrow
[0] = wxPoint(0, 0);
889 rightarrow
[1] = wxPoint(arrowheight
/ 2, arrowheight
/ 2);
890 rightarrow
[2] = wxPoint(0, arrowheight
- 1);
892 // draw the "month-arrows"
894 wxCoord arrowy
= (m_heightRow
- arrowheight
) / 2;
895 wxCoord larrowx
= (m_widthCol
- (arrowheight
/ 2)) / 2;
896 wxCoord rarrowx
= ((m_widthCol
- (arrowheight
/ 2)) / 2) + m_widthCol
*6;
897 m_leftArrowRect
= wxRect(0, 0, 0, 0);
898 m_rightArrowRect
= wxRect(0, 0, 0, 0);
900 if ( AllowMonthChange() )
902 wxDateTime ldpm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) - wxDateSpan::Day(); // last day prev month
903 // Check if range permits change
904 if ( IsDateInRange(ldpm
) && ( ( ldpm
.GetYear() == m_date
.GetYear() ) ? TRUE
: AllowYearChange() ) )
906 m_leftArrowRect
= wxRect(larrowx
- 3, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
907 dc
.SetBrush(wxBrush(*wxBLACK
, wxSOLID
));
908 dc
.SetPen(wxPen(*wxBLACK
, 1, wxSOLID
));
909 dc
.DrawPolygon(3, leftarrow
, larrowx
, arrowy
, wxWINDING_RULE
);
910 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
911 dc
.DrawRectangle(m_leftArrowRect
);
913 wxDateTime fdnm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) + wxDateSpan::Month(); // first day next month
914 if ( IsDateInRange(fdnm
) && ( ( fdnm
.GetYear() == m_date
.GetYear() ) ? TRUE
: AllowYearChange() ) )
916 m_rightArrowRect
= wxRect(rarrowx
- 4, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
917 dc
.SetBrush(wxBrush(*wxBLACK
, wxSOLID
));
918 dc
.SetPen(wxPen(*wxBLACK
, 1, wxSOLID
));
919 dc
.DrawPolygon(3, rightarrow
, rarrowx
, arrowy
, wxWINDING_RULE
);
920 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
921 dc
.DrawRectangle(m_rightArrowRect
);
928 // first draw the week days
929 if ( IsExposed(0, y
, 7*m_widthCol
, m_heightRow
) )
932 wxLogDebug("painting the header");
935 dc
.SetBackgroundMode(wxTRANSPARENT
);
936 dc
.SetTextForeground(m_colHeaderFg
);
937 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
938 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
939 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
941 bool startOnMonday
= (GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
942 for ( size_t wd
= 0; wd
< 7; wd
++ )
946 n
= wd
== 6 ? 0 : wd
+ 1;
950 dc
.GetTextExtent(m_weekdays
[n
], &dayw
, &dayh
);
951 dc
.DrawText(m_weekdays
[n
], (wd
*m_widthCol
) + ((m_widthCol
- dayw
) / 2), y
); // center the day-name
955 // then the calendar itself
956 dc
.SetTextForeground(*wxBLACK
);
957 //dc.SetFont(*wxNORMAL_FONT);
960 wxDateTime date
= GetStartDate();
963 wxLogDebug("starting calendar from %s\n",
964 date
.Format("%a %d-%m-%Y %H:%M:%S").c_str());
967 dc
.SetBackgroundMode(wxSOLID
);
968 for ( size_t nWeek
= 1; nWeek
<= 6; nWeek
++, y
+= m_heightRow
)
970 // if the update region doesn't intersect this row, don't paint it
971 if ( !IsExposed(0, y
, 7*m_widthCol
, m_heightRow
- 1) )
973 date
+= wxDateSpan::Week();
979 wxLogDebug("painting week %d at y = %d\n", nWeek
, y
);
982 for ( size_t wd
= 0; wd
< 7; wd
++ )
984 if ( IsDateShown(date
) )
986 // don't use wxDate::Format() which prepends 0s
987 unsigned int day
= date
.GetDay();
988 wxString dayStr
= wxString::Format(_T("%u"), day
);
990 dc
.GetTextExtent(dayStr
, &width
, (wxCoord
*)NULL
);
992 bool changedColours
= FALSE
,
996 wxCalendarDateAttr
*attr
= NULL
;
998 if ( date
.GetMonth() != m_date
.GetMonth() || !IsDateInRange(date
) )
1000 // surrounding week or out-of-range
1002 dc
.SetTextForeground(*wxLIGHT_GREY
);
1003 changedColours
= TRUE
;
1007 isSel
= date
.IsSameDate(m_date
);
1008 attr
= m_attrs
[day
- 1];
1012 dc
.SetTextForeground(m_colHighlightFg
);
1013 dc
.SetTextBackground(m_colHighlightBg
);
1015 changedColours
= TRUE
;
1019 wxColour colFg
, colBg
;
1021 if ( attr
->IsHoliday() )
1023 colFg
= m_colHolidayFg
;
1024 colBg
= m_colHolidayBg
;
1028 colFg
= attr
->GetTextColour();
1029 colBg
= attr
->GetBackgroundColour();
1034 dc
.SetTextForeground(colFg
);
1035 changedColours
= TRUE
;
1040 dc
.SetTextBackground(colBg
);
1041 changedColours
= TRUE
;
1044 if ( attr
->HasFont() )
1046 dc
.SetFont(attr
->GetFont());
1052 wxCoord x
= wd
*m_widthCol
+ (m_widthCol
- width
) / 2;
1053 dc
.DrawText(dayStr
, x
, y
+ 1);
1055 if ( !isSel
&& attr
&& attr
->HasBorder() )
1058 if ( attr
->HasBorderColour() )
1060 colBorder
= attr
->GetBorderColour();
1064 colBorder
= m_foregroundColour
;
1067 wxPen
pen(colBorder
, 1, wxSOLID
);
1069 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1071 switch ( attr
->GetBorder() )
1073 case wxCAL_BORDER_SQUARE
:
1074 dc
.DrawRectangle(x
- 2, y
,
1075 width
+ 4, m_heightRow
);
1078 case wxCAL_BORDER_ROUND
:
1079 dc
.DrawEllipse(x
- 2, y
,
1080 width
+ 4, m_heightRow
);
1084 wxFAIL_MSG(_T("unknown border type"));
1088 if ( changedColours
)
1090 dc
.SetTextForeground(m_foregroundColour
);
1091 dc
.SetTextBackground(m_backgroundColour
);
1099 //else: just don't draw it
1101 date
+= wxDateSpan::Day();
1105 // Greying out out-of-range background
1106 bool showSurrounding
= (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) != 0;
1108 date
= ( showSurrounding
) ? GetStartDate() : wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear());
1109 if ( !IsDateInRange(date
) )
1111 wxDateTime firstOOR
= GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1113 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1114 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1116 HighlightRange(&dc
, date
, firstOOR
, wxTRANSPARENT_PEN
, &oorbrush
);
1119 date
= ( showSurrounding
) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear());
1120 if ( !IsDateInRange(date
) )
1122 wxDateTime firstOOR
= GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1124 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1125 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1127 HighlightRange(&dc
, firstOOR
, date
, wxTRANSPARENT_PEN
, &oorbrush
);
1131 wxLogDebug("+++ finished painting");
1135 void wxCalendarCtrl::RefreshDate(const wxDateTime
& date
)
1141 // always refresh the whole row at once because our OnPaint() will draw
1142 // the whole row anyhow - and this allows the small optimisation in
1143 // OnClick() below to work
1146 rect
.y
= (m_heightRow
* GetWeek(date
)) + m_rowOffset
;
1148 rect
.width
= 7*m_widthCol
;
1149 rect
.height
= m_heightRow
;
1152 // VZ: for some reason, the selected date seems to occupy more space under
1153 // MSW - this is probably some bug in the font size calculations, but I
1154 // don't know where exactly. This fix is ugly and leads to more
1155 // refreshes than really needed, but without it the selected days
1156 // leaves even more ugly underscores on screen.
1161 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
1164 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1167 Refresh(TRUE
, &rect
);
1170 void wxCalendarCtrl::HighlightRange(wxPaintDC
* pDC
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, wxPen
* pPen
, wxBrush
* pBrush
)
1172 // Highlights the given range using pen and brush
1173 // Does nothing if todate < fromdate
1177 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate
.Format("%d %m %Y"), todate
.Format("%d %m %Y"));
1180 if ( todate
>= fromdate
)
1187 // implicit: both dates must be currently shown - checked by GetDateCoord
1188 if ( GetDateCoord(fromdate
, &fd
, &fw
) && GetDateCoord(todate
, &td
, &tw
) )
1191 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd
, fw
, td
, tw
);
1193 if ( ( (tw
- fw
) == 1 ) && ( td
< fd
) )
1195 // special case: interval 7 days or less not in same week
1196 // split in two seperate intervals
1197 wxDateTime tfd
= fromdate
+ wxDateSpan::Days(7-fd
);
1198 wxDateTime ftd
= tfd
+ wxDateSpan::Day();
1200 wxLogDebug("Highlight: Seperate segments");
1203 HighlightRange(pDC
, fromdate
, tfd
, pPen
, pBrush
);
1204 HighlightRange(pDC
, ftd
, todate
, pPen
, pBrush
);
1209 wxPoint corners
[8]; // potentially 8 corners in polygon
1213 // simple case: same week
1215 corners
[0] = wxPoint((fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
);
1216 corners
[1] = wxPoint((fd
- 1) * m_widthCol
, ((fw
+ 1 ) * m_heightRow
) + m_rowOffset
);
1217 corners
[2] = wxPoint(td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
);
1218 corners
[3] = wxPoint(td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
);
1223 // "complex" polygon
1224 corners
[cidx
] = wxPoint((fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1228 corners
[cidx
] = wxPoint((fd
- 1) * m_widthCol
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1229 corners
[cidx
] = wxPoint(0, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1232 corners
[cidx
] = wxPoint(0, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1233 corners
[cidx
] = wxPoint(td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1237 corners
[cidx
] = wxPoint(td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1238 corners
[cidx
] = wxPoint(7 * m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1241 corners
[cidx
] = wxPoint(7 * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1247 pDC
->SetBrush(*pBrush
);
1249 pDC
->DrawPolygon(numpoints
, corners
);
1255 wxLogDebug("--- HighlightRange ---");
1259 bool wxCalendarCtrl::GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const
1264 wxLogDebug("+++ GetDateCoord: (%s) +++", date
.Format("%d %m %Y"));
1267 if ( IsDateShown(date
) )
1269 bool startOnMonday
= ( GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1272 *day
= date
.GetWeekDay();
1274 if ( *day
== 0 ) // sunday
1276 *day
= ( startOnMonday
) ? 7 : 1;
1280 day
+= ( startOnMonday
) ? 0 : 1;
1283 int targetmonth
= date
.GetMonth() + (12 * date
.GetYear());
1284 int thismonth
= m_date
.GetMonth() + (12 * m_date
.GetYear());
1287 if ( targetmonth
== thismonth
)
1289 *week
= GetWeek(date
);
1293 if ( targetmonth
< thismonth
)
1295 *week
= 1; // trivial
1297 else // targetmonth > thismonth
1303 // get the datecoord of the last day in the month currently shown
1305 wxLogDebug(" +++ LDOM +++");
1307 GetDateCoord(ldcm
.SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear()), &lastday
, &lastweek
);
1309 wxLogDebug(" --- LDOM ---");
1312 wxTimeSpan span
= date
- ldcm
;
1314 int daysfromlast
= span
.GetDays();
1316 wxLogDebug("daysfromlast: %i", daysfromlast
);
1318 if ( daysfromlast
+ lastday
> 7 ) // past week boundary
1320 int wholeweeks
= (daysfromlast
/ 7);
1321 *week
= wholeweeks
+ lastweek
;
1322 if ( (daysfromlast
- (7 * wholeweeks
) + lastday
) > 7 )
1342 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date
.Format("%d %m %Y"), *day
, *week
);
1348 // ----------------------------------------------------------------------------
1350 // ----------------------------------------------------------------------------
1352 void wxCalendarCtrl::OnDClick(wxMouseEvent
& event
)
1354 if ( HitTest(event
.GetPosition()) != wxCAL_HITTEST_DAY
)
1360 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1364 void wxCalendarCtrl::OnClick(wxMouseEvent
& event
)
1367 wxDateTime::WeekDay wday
;
1368 switch ( HitTest(event
.GetPosition(), &date
, &wday
) )
1370 case wxCAL_HITTEST_DAY
:
1371 if ( IsDateInRange(date
) )
1375 GenerateEvents(wxEVT_CALENDAR_DAY_CHANGED
,
1376 wxEVT_CALENDAR_SEL_CHANGED
);
1380 case wxCAL_HITTEST_HEADER
:
1382 wxCalendarEvent
event(this, wxEVT_CALENDAR_WEEKDAY_CLICKED
);
1383 event
.m_wday
= wday
;
1384 (void)GetEventHandler()->ProcessEvent(event
);
1388 case wxCAL_HITTEST_DECMONTH
:
1389 case wxCAL_HITTEST_INCMONTH
:
1390 case wxCAL_HITTEST_SURROUNDING_WEEK
:
1391 SetDateAndNotify(date
); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1395 wxFAIL_MSG(_T("unknown hittest code"));
1398 case wxCAL_HITTEST_NOWHERE
:
1404 wxCalendarHitTestResult
wxCalendarCtrl::HitTest(const wxPoint
& pos
,
1406 wxDateTime::WeekDay
*wd
)
1412 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1413 if ( (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
1417 // we need to find out if the hit is on left arrow, on month or on right arrow
1419 if ( wxRegion(m_leftArrowRect
).Contains(pos
) == wxInRegion
)
1423 if ( IsDateInRange(m_date
- wxDateSpan::Month()) )
1425 *date
= m_date
- wxDateSpan::Month();
1429 *date
= GetLowerDateLimit();
1433 return wxCAL_HITTEST_DECMONTH
;
1436 if ( wxRegion(m_rightArrowRect
).Contains(pos
) == wxInRegion
)
1440 if ( IsDateInRange(m_date
+ wxDateSpan::Month()) )
1442 *date
= m_date
+ wxDateSpan::Month();
1446 *date
= GetUpperDateLimit();
1450 return wxCAL_HITTEST_INCMONTH
;
1455 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1457 int wday
= pos
.x
/ m_widthCol
;
1458 // if ( y < m_heightRow )
1459 if ( y
< (m_heightRow
+ m_rowOffset
) )
1461 if ( y
> m_rowOffset
)
1465 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST
)
1467 wday
= wday
== 6 ? 0 : wday
+ 1;
1470 *wd
= (wxDateTime::WeekDay
)wday
;
1473 return wxCAL_HITTEST_HEADER
;
1477 return wxCAL_HITTEST_NOWHERE
;
1481 // int week = (y - m_heightRow) / m_heightRow;
1482 int week
= (y
- (m_heightRow
+ m_rowOffset
)) / m_heightRow
;
1483 if ( week
>= 6 || wday
>= 7 )
1485 return wxCAL_HITTEST_NOWHERE
;
1488 wxDateTime dt
= GetStartDate() + wxDateSpan::Days(7*week
+ wday
);
1490 if ( IsDateShown(dt
) )
1495 if ( dt
.GetMonth() == m_date
.GetMonth() )
1498 return wxCAL_HITTEST_DAY
;
1502 return wxCAL_HITTEST_SURROUNDING_WEEK
;
1507 return wxCAL_HITTEST_NOWHERE
;
1511 // ----------------------------------------------------------------------------
1512 // subcontrols events handling
1513 // ----------------------------------------------------------------------------
1515 void wxCalendarCtrl::OnMonthChange(wxCommandEvent
& event
)
1517 wxDateTime::Tm tm
= m_date
.GetTm();
1519 wxDateTime::Month mon
= (wxDateTime::Month
)event
.GetInt();
1520 if ( tm
.mday
> wxDateTime::GetNumberOfDays(mon
, tm
.year
) )
1522 tm
.mday
= wxDateTime::GetNumberOfDays(mon
, tm
.year
);
1525 wxDateTime target
= wxDateTime(tm
.mday
, mon
, tm
.year
);
1527 ChangeMonth(&target
);
1528 SetDateAndNotify(target
);
1531 void wxCalendarCtrl::OnYearChange(wxCommandEvent
& event
)
1533 int year
= (int)event
.GetInt();
1534 if ( year
== INT_MIN
)
1536 // invalid year in the spin control, ignore it
1540 // set the flag for SetDate(): otherwise it would overwrite the year
1541 // typed in by the user
1542 m_userChangedYear
= TRUE
;
1544 wxDateTime::Tm tm
= m_date
.GetTm();
1546 if ( tm
.mday
> wxDateTime::GetNumberOfDays(tm
.mon
, year
) )
1548 tm
.mday
= wxDateTime::GetNumberOfDays(tm
.mon
, year
);
1551 wxDateTime target
= wxDateTime(tm
.mday
, tm
.mon
, year
);
1553 if ( ChangeYear(&target
) )
1555 SetDateAndNotify(target
);
1559 // In this case we don't want to change the date. That would put us
1560 // inside the same year but a strange number of months forward/back..
1561 m_spinYear
->SetValue(target
.GetYear());
1565 // ----------------------------------------------------------------------------
1566 // keyboard interface
1567 // ----------------------------------------------------------------------------
1569 void wxCalendarCtrl::OnChar(wxKeyEvent
& event
)
1572 switch ( event
.KeyCode() )
1576 target
= m_date
+ wxDateSpan::Year();
1577 if ( ChangeYear(&target
) )
1579 SetDateAndNotify(target
);
1585 target
= m_date
- wxDateSpan::Year();
1586 if ( ChangeYear(&target
) )
1588 SetDateAndNotify(target
);
1593 target
= m_date
- wxDateSpan::Month();
1594 ChangeMonth(&target
);
1595 SetDateAndNotify(target
); // always
1599 target
= m_date
+ wxDateSpan::Month();
1600 ChangeMonth(&target
);
1601 SetDateAndNotify(target
); // always
1605 if ( event
.ControlDown() )
1607 target
= wxDateTime(m_date
).SetToNextWeekDay(
1608 GetWindowStyle() & wxCAL_MONDAY_FIRST
1609 ? wxDateTime::Sun
: wxDateTime::Sat
);
1610 if ( !IsDateInRange(target
) )
1612 target
= GetUpperDateLimit();
1614 SetDateAndNotify(target
);
1617 SetDateAndNotify(m_date
+ wxDateSpan::Day());
1621 if ( event
.ControlDown() )
1623 target
= wxDateTime(m_date
).SetToPrevWeekDay(
1624 GetWindowStyle() & wxCAL_MONDAY_FIRST
1625 ? wxDateTime::Mon
: wxDateTime::Sun
);
1626 if ( !IsDateInRange(target
) )
1628 target
= GetLowerDateLimit();
1630 SetDateAndNotify(target
);
1633 SetDateAndNotify(m_date
- wxDateSpan::Day());
1637 SetDateAndNotify(m_date
- wxDateSpan::Week());
1641 SetDateAndNotify(m_date
+ wxDateSpan::Week());
1645 if ( event
.ControlDown() )
1646 SetDateAndNotify(wxDateTime::Today());
1648 SetDateAndNotify(wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear()));
1652 SetDateAndNotify(wxDateTime(m_date
).SetToLastMonthDay());
1656 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1664 // ----------------------------------------------------------------------------
1665 // holidays handling
1666 // ----------------------------------------------------------------------------
1668 void wxCalendarCtrl::EnableHolidayDisplay(bool display
)
1670 long style
= GetWindowStyle();
1672 style
|= wxCAL_SHOW_HOLIDAYS
;
1674 style
&= ~wxCAL_SHOW_HOLIDAYS
;
1676 SetWindowStyle(style
);
1681 ResetHolidayAttrs();
1686 void wxCalendarCtrl::SetHolidayAttrs()
1688 if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS
)
1690 ResetHolidayAttrs();
1692 wxDateTime::Tm tm
= m_date
.GetTm();
1693 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
1694 dtEnd
= dtStart
.GetLastMonthDay();
1696 wxDateTimeArray hol
;
1697 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
1699 size_t count
= hol
.GetCount();
1700 for ( size_t n
= 0; n
< count
; n
++ )
1702 SetHoliday(hol
[n
].GetDay());
1707 void wxCalendarCtrl::SetHoliday(size_t day
)
1709 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in SetHoliday") );
1711 wxCalendarDateAttr
*attr
= GetAttr(day
);
1714 attr
= new wxCalendarDateAttr
;
1717 attr
->SetHoliday(TRUE
);
1719 // can't use SetAttr() because it would delete this pointer
1720 m_attrs
[day
- 1] = attr
;
1723 void wxCalendarCtrl::ResetHolidayAttrs()
1725 for ( size_t day
= 0; day
< 31; day
++ )
1729 m_attrs
[day
]->SetHoliday(FALSE
);
1734 // ----------------------------------------------------------------------------
1736 // ----------------------------------------------------------------------------
1738 void wxCalendarEvent::Init()
1740 m_wday
= wxDateTime::Inv_WeekDay
;
1743 wxCalendarEvent::wxCalendarEvent(wxCalendarCtrl
*cal
, wxEventType type
)
1744 : wxCommandEvent(type
, cal
->GetId())
1746 m_date
= cal
->GetDate();
1747 SetEventObject(cal
);
1750 #endif // wxUSE_CALENDARCTRL