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"
39 #if wxUSE_CALENDARCTRL
41 #include "wx/calctrl.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 class wxMonthComboBox
: public wxComboBox
52 wxMonthComboBox(wxCalendarCtrl
*cal
);
54 void OnMonthChange(wxCommandEvent
& event
) { m_cal
->OnMonthChange(event
); }
57 wxCalendarCtrl
*m_cal
;
62 class wxYearSpinCtrl
: public wxSpinCtrl
65 wxYearSpinCtrl(wxCalendarCtrl
*cal
);
67 void OnYearChange(wxSpinEvent
& event
) { m_cal
->OnYearChange(event
); }
70 wxCalendarCtrl
*m_cal
;
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 BEGIN_EVENT_TABLE(wxCalendarCtrl
, wxControl
)
80 EVT_PAINT(wxCalendarCtrl::OnPaint
)
82 EVT_CHAR(wxCalendarCtrl::OnChar
)
84 EVT_LEFT_DOWN(wxCalendarCtrl::OnClick
)
85 EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick
)
88 BEGIN_EVENT_TABLE(wxMonthComboBox
, wxComboBox
)
89 EVT_COMBOBOX(-1, wxMonthComboBox::OnMonthChange
)
92 BEGIN_EVENT_TABLE(wxYearSpinCtrl
, wxSpinCtrl
)
93 EVT_SPINCTRL(-1, wxYearSpinCtrl::OnYearChange
)
96 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl
, wxControl
)
97 IMPLEMENT_DYNAMIC_CLASS(wxCalendarEvent
, wxCommandEvent
)
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_SEL_CHANGED
)
104 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DAY_CHANGED
)
105 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_MONTH_CHANGED
)
106 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_YEAR_CHANGED
)
107 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DOUBLECLICKED
)
108 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_WEEKDAY_CLICKED
)
110 // ============================================================================
112 // ============================================================================
114 // ----------------------------------------------------------------------------
115 // wxMonthComboBox and wxYearSpinCtrl
116 // ----------------------------------------------------------------------------
118 wxMonthComboBox::wxMonthComboBox(wxCalendarCtrl
*cal
)
119 : wxComboBox(cal
->GetParent(), -1,
124 wxCB_READONLY
| wxCLIP_SIBLINGS
)
129 for ( m
= wxDateTime::Jan
; m
< wxDateTime::Inv_Month
; wxNextMonth(m
) )
131 Append(wxDateTime::GetMonthName(m
));
134 SetSelection(m_cal
->GetDate().GetMonth());
135 SetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
|wxSIZE_AUTO_HEIGHT
);
138 wxYearSpinCtrl::wxYearSpinCtrl(wxCalendarCtrl
*cal
)
139 : wxSpinCtrl(cal
->GetParent(), -1,
140 cal
->GetDate().Format(_T("%Y")),
143 wxSP_ARROW_KEYS
| wxCLIP_SIBLINGS
,
144 -4300, 10000, cal
->GetDate().GetYear())
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 void wxCalendarCtrl::Init()
161 wxDateTime::WeekDay wd
;
162 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
164 m_weekdays
[wd
] = wxDateTime::GetWeekDayName(wd
, wxDateTime::Name_Abbr
);
167 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
172 m_colHighlightFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
173 m_colHighlightBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
175 m_colHolidayFg
= *wxRED
;
176 // don't set m_colHolidayBg - by default, same as our bg colour
178 m_colHeaderFg
= *wxBLUE
;
179 m_colHeaderBg
= *wxLIGHT_GREY
;
182 bool wxCalendarCtrl::Create(wxWindow
*parent
,
184 const wxDateTime
& date
,
188 const wxString
& name
)
190 if ( !wxControl::Create(parent
, id
, pos
, size
,
191 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
192 wxDefaultValidator
, name
) )
197 // needed to get the arrow keys normally used for the dialog navigation
198 SetWindowStyle(style
| wxWANTS_CHARS
);
200 m_date
= date
.IsValid() ? date
: wxDateTime::Today();
202 m_lowdate
= wxDefaultDateTime
;
203 m_highdate
= wxDefaultDateTime
;
205 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
207 m_spinYear
= new wxYearSpinCtrl(this);
208 m_staticYear
= new wxStaticText(GetParent(), -1, m_date
.Format(_T("%Y")),
209 wxDefaultPosition
, wxDefaultSize
,
212 m_comboMonth
= new wxMonthComboBox(this);
213 m_staticMonth
= new wxStaticText(GetParent(), -1, m_date
.Format(_T("%B")),
214 wxDefaultPosition
, wxDefaultSize
,
218 ShowCurrentControls();
221 if ( size
.x
== -1 || size
.y
== -1 )
223 sizeReal
= DoGetBestSize();
234 // we need to set the position as well because the main control position
235 // is not the same as the one specified in pos if we have the controls
237 SetSize(pos
.x
, pos
.y
, sizeReal
.x
, sizeReal
.y
);
239 SetBackgroundColour(*wxWHITE
);
240 SetFont(*wxSWISS_FONT
);
247 wxCalendarCtrl::~wxCalendarCtrl()
249 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
255 // ----------------------------------------------------------------------------
256 // forward wxWin functions to subcontrols
257 // ----------------------------------------------------------------------------
259 bool wxCalendarCtrl::Destroy()
261 if( m_staticYear
) m_staticYear
->Destroy();
262 if( m_spinYear
) m_spinYear
->Destroy();
263 if( m_comboMonth
) m_comboMonth
->Destroy();
264 if( m_staticMonth
) m_staticMonth
->Destroy();
266 m_staticYear
= 0; m_spinYear
= 0; m_comboMonth
= 0; m_staticMonth
= 0;
268 return wxControl::Destroy();
271 bool wxCalendarCtrl::Show(bool show
)
273 if ( !wxControl::Show(show
) )
278 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
280 if ( GetMonthControl() )
282 GetMonthControl()->Show(show
);
283 GetYearControl()->Show(show
);
290 bool wxCalendarCtrl::Enable(bool enable
)
292 if ( !wxControl::Enable(enable
) )
297 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
299 GetMonthControl()->Enable(enable
);
300 GetYearControl()->Enable(enable
);
306 // ----------------------------------------------------------------------------
307 // enable/disable month/year controls
308 // ----------------------------------------------------------------------------
310 void wxCalendarCtrl::ShowCurrentControls()
312 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
314 if ( AllowMonthChange() )
316 m_comboMonth
->Show();
317 m_staticMonth
->Hide();
319 if ( AllowYearChange() )
322 m_staticYear
->Hide();
330 m_comboMonth
->Hide();
331 m_staticMonth
->Show();
334 // year change not allowed here
336 m_staticYear
->Show();
340 wxControl
*wxCalendarCtrl::GetMonthControl() const
342 return AllowMonthChange() ? (wxControl
*)m_comboMonth
: (wxControl
*)m_staticMonth
;
345 wxControl
*wxCalendarCtrl::GetYearControl() const
347 return AllowYearChange() ? (wxControl
*)m_spinYear
: (wxControl
*)m_staticYear
;
350 void wxCalendarCtrl::EnableYearChange(bool enable
)
352 if ( enable
!= AllowYearChange() )
354 long style
= GetWindowStyle();
356 style
&= ~wxCAL_NO_YEAR_CHANGE
;
358 style
|= wxCAL_NO_YEAR_CHANGE
;
359 SetWindowStyle(style
);
361 ShowCurrentControls();
362 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
369 void wxCalendarCtrl::EnableMonthChange(bool enable
)
371 if ( enable
!= AllowMonthChange() )
373 long style
= GetWindowStyle();
375 style
&= ~wxCAL_NO_MONTH_CHANGE
;
377 style
|= wxCAL_NO_MONTH_CHANGE
;
378 SetWindowStyle(style
);
380 ShowCurrentControls();
381 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
392 bool wxCalendarCtrl::SetDate(const wxDateTime
& date
)
396 bool sameMonth
= m_date
.GetMonth() == date
.GetMonth(),
397 sameYear
= m_date
.GetYear() == date
.GetYear();
399 if ( IsDateInRange(date
) )
401 if ( sameMonth
&& sameYear
)
403 // just change the day
409 if ( !AllowMonthChange() || (!AllowYearChange() && !sameYear
) )
419 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
421 // update the controls
422 m_comboMonth
->SetSelection(m_date
.GetMonth());
424 if ( AllowYearChange() )
426 m_spinYear
->SetValue(m_date
.Format(_T("%Y")));
430 // as the month changed, holidays did too
433 // update the calendar
441 void wxCalendarCtrl::ChangeDay(const wxDateTime
& date
)
443 if ( m_date
!= date
)
445 // we need to refresh the row containing the old date and the one
446 // containing the new one
447 wxDateTime dateOld
= m_date
;
450 RefreshDate(dateOld
);
452 // if the date is in the same row, it was already drawn correctly
453 if ( GetWeek(m_date
) != GetWeek(dateOld
) )
460 void wxCalendarCtrl::SetDateAndNotify(const wxDateTime
& date
)
462 wxDateTime::Tm tm1
= m_date
.GetTm(),
466 if ( tm1
.year
!= tm2
.year
)
467 type
= wxEVT_CALENDAR_YEAR_CHANGED
;
468 else if ( tm1
.mon
!= tm2
.mon
)
469 type
= wxEVT_CALENDAR_MONTH_CHANGED
;
470 else if ( tm1
.mday
!= tm2
.mday
)
471 type
= wxEVT_CALENDAR_DAY_CHANGED
;
477 GenerateEvents(type
, wxEVT_CALENDAR_SEL_CHANGED
);
481 // ----------------------------------------------------------------------------
483 // ----------------------------------------------------------------------------
485 bool wxCalendarCtrl::SetLowerDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
489 if ( !(date
.IsValid()) || ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : TRUE
) )
501 bool wxCalendarCtrl::SetUpperDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
505 if ( !(date
.IsValid()) || ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : TRUE
) )
517 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& lowerdate
/* = wxDefaultDateTime */, const wxDateTime
& upperdate
/* = wxDefaultDateTime */)
522 ( !( lowerdate
.IsValid() ) || ( ( upperdate
.IsValid() ) ? ( lowerdate
<= upperdate
) : TRUE
) ) &&
523 ( !( upperdate
.IsValid() ) || ( ( lowerdate
.IsValid() ) ? ( upperdate
>= lowerdate
) : TRUE
) ) )
525 m_lowdate
= lowerdate
;
526 m_highdate
= upperdate
;
536 // ----------------------------------------------------------------------------
538 // ----------------------------------------------------------------------------
540 wxDateTime
wxCalendarCtrl::GetStartDate() const
542 wxDateTime::Tm tm
= m_date
.GetTm();
544 wxDateTime date
= wxDateTime(1, tm
.mon
, tm
.year
);
547 date
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
548 ? wxDateTime::Mon
: wxDateTime::Sun
);
550 if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
)
552 // We want to offset the calendar if we start on the first..
553 if ( date
.GetDay() == 1 )
555 date
-= wxDateSpan::Week();
562 bool wxCalendarCtrl::IsDateShown(const wxDateTime
& date
) const
564 if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
566 return date
.GetMonth() == m_date
.GetMonth();
574 bool wxCalendarCtrl::IsDateInRange(const wxDateTime
& date
) const
577 // Check if the given date is in the range specified
578 retval
= ( ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : TRUE
)
579 && ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : TRUE
) );
583 bool wxCalendarCtrl::ChangeYear(wxDateTime
* target
) const
587 if ( !(IsDateInRange(*target
)) )
589 if ( target
->GetYear() < m_date
.GetYear() )
591 if ( target
->GetYear() >= GetLowerDateLimit().GetYear() )
593 *target
= GetLowerDateLimit();
603 if ( target
->GetYear() <= GetUpperDateLimit().GetYear() )
605 *target
= GetUpperDateLimit();
622 bool wxCalendarCtrl::ChangeMonth(wxDateTime
* target
) const
626 if ( !(IsDateInRange(*target
)) )
630 if ( target
->GetMonth() < m_date
.GetMonth() )
632 *target
= GetLowerDateLimit();
636 *target
= GetUpperDateLimit();
643 size_t wxCalendarCtrl::GetWeek(const wxDateTime
& date
) const
645 size_t retval
= date
.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
646 ? wxDateTime::Monday_First
647 : wxDateTime::Sunday_First
);
649 if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
651 // we need to offset an extra week if we "start" on the 1st of the month
652 wxDateTime::Tm tm
= date
.GetTm();
654 wxDateTime datetest
= wxDateTime(1, tm
.mon
, tm
.year
);
657 datetest
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
658 ? wxDateTime::Mon
: wxDateTime::Sun
);
660 if ( datetest
.GetDay() == 1 )
669 // ----------------------------------------------------------------------------
671 // ----------------------------------------------------------------------------
673 // this is a composite control and it must arrange its parts each time its
674 // size or position changes: the combobox and spinctrl are along the top of
675 // the available area and the calendar takes up therest of the space
677 // the static controls are supposed to be always smaller than combo/spin so we
678 // always use the latter for size calculations and position the static to take
681 // the constants used for the layout
682 #define VERT_MARGIN 5 // distance between combo and calendar
683 #define HORZ_MARGIN 15 // spin
685 wxSize
wxCalendarCtrl::DoGetBestSize() const
687 // calc the size of the calendar
688 ((wxCalendarCtrl
*)this)->RecalcGeometry(); // const_cast
690 wxCoord width
= 7*m_widthCol
,
691 height
= 7*m_heightRow
+ m_rowOffset
+ VERT_MARGIN
;
693 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
695 // the combobox doesn't report its height correctly (it returns the
696 // height including the drop down list) so don't use it
697 height
+= m_spinYear
->GetBestSize().y
;
700 if ( !HasFlag(wxBORDER_NONE
) )
702 // the border would clip the last line otherwise
707 return wxSize(width
, height
);
710 void wxCalendarCtrl::DoSetSize(int x
, int y
,
711 int width
, int height
,
714 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
717 void wxCalendarCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
721 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
723 wxSize sizeCombo
= m_comboMonth
->GetSize();
724 wxSize sizeStatic
= m_staticMonth
->GetSize();
726 int dy
= (sizeCombo
.y
- sizeStatic
.y
) / 2;
728 m_comboMonth
->Move(x
, y
);
729 m_staticMonth
->SetSize(x
, y
+ dy
, sizeCombo
.x
, sizeStatic
.y
);
731 int xDiff
= sizeCombo
.x
+ HORZ_MARGIN
;
733 m_spinYear
->SetSize(x
+ xDiff
, y
, width
- xDiff
, sizeCombo
.y
);
734 m_staticYear
->SetSize(x
+ xDiff
, y
+ dy
, width
- xDiff
, sizeStatic
.y
);
736 wxSize sizeSpin
= m_spinYear
->GetSize();
737 yDiff
= wxMax(sizeSpin
.y
, sizeCombo
.y
) + VERT_MARGIN
;
739 else // no controls on the top
744 wxControl::DoMoveWindow(x
, y
+ yDiff
, width
, height
- yDiff
);
747 void wxCalendarCtrl::DoGetPosition(int *x
, int *y
) const
749 wxControl::DoGetPosition(x
, y
);
751 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
753 // our real top corner is not in this position
756 *y
-= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
761 void wxCalendarCtrl::DoGetSize(int *width
, int *height
) const
763 wxControl::DoGetSize(width
, height
);
765 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
767 // our real height is bigger
770 *height
+= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
775 void wxCalendarCtrl::RecalcGeometry()
777 if ( m_widthCol
!= 0 )
784 // determine the column width (we assume that the weekday names are always
785 // wider (in any language) than the numbers)
787 wxDateTime::WeekDay wd
;
788 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
791 dc
.GetTextExtent(m_weekdays
[wd
], &width
, &m_heightRow
);
792 if ( width
> m_widthCol
)
798 // leave some margins
802 m_rowOffset
= (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) ? m_heightRow
: 0; // conditional in relation to style
805 // ----------------------------------------------------------------------------
807 // ----------------------------------------------------------------------------
809 void wxCalendarCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
818 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
819 m_date
.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
825 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
827 // draw the sequential month-selector
829 dc
.SetBackgroundMode(wxTRANSPARENT
);
830 dc
.SetTextForeground(*wxBLACK
);
831 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
832 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
833 dc
.DrawRectangle(0, y
, 7*m_widthCol
, m_heightRow
);
835 // Get extent of month-name + year
836 wxCoord monthw
, monthh
;
837 wxString headertext
= m_date
.Format(wxT("%B %Y"));
838 dc
.GetTextExtent(headertext
, &monthw
, &monthh
);
840 // draw month-name centered above weekdays
841 wxCoord monthx
= ((m_widthCol
* 7) - monthw
) / 2;
842 wxCoord monthy
= ((m_heightRow
- monthh
) / 2) + y
;
843 dc
.DrawText(headertext
, monthx
, monthy
);
845 // calculate the "month-arrows"
846 wxPoint leftarrow
[3];
847 wxPoint rightarrow
[3];
849 int arrowheight
= monthh
/ 2;
851 leftarrow
[0] = wxPoint(0, arrowheight
/ 2);
852 leftarrow
[1] = wxPoint(arrowheight
/ 2, 0);
853 leftarrow
[2] = wxPoint(arrowheight
/ 2, arrowheight
- 1);
855 rightarrow
[0] = wxPoint(0, 0);
856 rightarrow
[1] = wxPoint(arrowheight
/ 2, arrowheight
/ 2);
857 rightarrow
[2] = wxPoint(0, arrowheight
- 1);
859 // draw the "month-arrows"
861 wxCoord arrowy
= (m_heightRow
- arrowheight
) / 2;
862 wxCoord larrowx
= (m_widthCol
- (arrowheight
/ 2)) / 2;
863 wxCoord rarrowx
= ((m_widthCol
- (arrowheight
/ 2)) / 2) + m_widthCol
*6;
864 m_leftArrowRect
= wxRect(0, 0, 0, 0);
865 m_rightArrowRect
= wxRect(0, 0, 0, 0);
867 if ( AllowMonthChange() )
869 wxDateTime ldpm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) - wxDateSpan::Day(); // last day prev month
870 // Check if range permits change
871 if ( IsDateInRange(ldpm
) && ( ( ldpm
.GetYear() == m_date
.GetYear() ) ? TRUE
: AllowYearChange() ) )
873 m_leftArrowRect
= wxRect(larrowx
- 3, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
874 dc
.SetBrush(wxBrush(*wxBLACK
, wxSOLID
));
875 dc
.SetPen(wxPen(*wxBLACK
, 1, wxSOLID
));
876 dc
.DrawPolygon(3, leftarrow
, larrowx
, arrowy
, wxWINDING_RULE
);
877 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
878 dc
.DrawRectangle(m_leftArrowRect
);
880 wxDateTime fdnm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) + wxDateSpan::Month(); // first day next month
881 if ( IsDateInRange(fdnm
) && ( ( fdnm
.GetYear() == m_date
.GetYear() ) ? TRUE
: AllowYearChange() ) )
883 m_rightArrowRect
= wxRect(rarrowx
- 4, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
884 dc
.SetBrush(wxBrush(*wxBLACK
, wxSOLID
));
885 dc
.SetPen(wxPen(*wxBLACK
, 1, wxSOLID
));
886 dc
.DrawPolygon(3, rightarrow
, rarrowx
, arrowy
, wxWINDING_RULE
);
887 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
888 dc
.DrawRectangle(m_rightArrowRect
);
895 // first draw the week days
896 if ( IsExposed(0, y
, 7*m_widthCol
, m_heightRow
) )
899 wxLogDebug("painting the header");
902 dc
.SetBackgroundMode(wxTRANSPARENT
);
903 dc
.SetTextForeground(m_colHeaderFg
);
904 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
905 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
906 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
908 bool startOnMonday
= (GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
909 for ( size_t wd
= 0; wd
< 7; wd
++ )
913 n
= wd
== 6 ? 0 : wd
+ 1;
917 dc
.GetTextExtent(m_weekdays
[n
], &dayw
, &dayh
);
918 dc
.DrawText(m_weekdays
[n
], (wd
*m_widthCol
) + ((m_widthCol
- dayw
) / 2), y
); // center the day-name
922 // then the calendar itself
923 dc
.SetTextForeground(*wxBLACK
);
924 //dc.SetFont(*wxNORMAL_FONT);
927 wxDateTime date
= GetStartDate();
930 wxLogDebug("starting calendar from %s\n",
931 date
.Format("%a %d-%m-%Y %H:%M:%S").c_str());
934 dc
.SetBackgroundMode(wxSOLID
);
935 for ( size_t nWeek
= 1; nWeek
<= 6; nWeek
++, y
+= m_heightRow
)
937 // if the update region doesn't intersect this row, don't paint it
938 if ( !IsExposed(0, y
, 7*m_widthCol
, m_heightRow
- 1) )
940 date
+= wxDateSpan::Week();
946 wxLogDebug("painting week %d at y = %d\n", nWeek
, y
);
949 for ( size_t wd
= 0; wd
< 7; wd
++ )
951 if ( IsDateShown(date
) )
953 // don't use wxDate::Format() which prepends 0s
954 unsigned int day
= date
.GetDay();
955 wxString dayStr
= wxString::Format(_T("%u"), day
);
957 dc
.GetTextExtent(dayStr
, &width
, (wxCoord
*)NULL
);
959 bool changedColours
= FALSE
,
963 wxCalendarDateAttr
*attr
= NULL
;
965 if ( date
.GetMonth() != m_date
.GetMonth() || !IsDateInRange(date
) )
967 // surrounding week or out-of-range
969 dc
.SetTextForeground(*wxLIGHT_GREY
);
970 changedColours
= TRUE
;
974 isSel
= date
.IsSameDate(m_date
);
975 attr
= m_attrs
[day
- 1];
979 dc
.SetTextForeground(m_colHighlightFg
);
980 dc
.SetTextBackground(m_colHighlightBg
);
982 changedColours
= TRUE
;
986 wxColour colFg
, colBg
;
988 if ( attr
->IsHoliday() )
990 colFg
= m_colHolidayFg
;
991 colBg
= m_colHolidayBg
;
995 colFg
= attr
->GetTextColour();
996 colBg
= attr
->GetBackgroundColour();
1001 dc
.SetTextForeground(colFg
);
1002 changedColours
= TRUE
;
1007 dc
.SetTextBackground(colBg
);
1008 changedColours
= TRUE
;
1011 if ( attr
->HasFont() )
1013 dc
.SetFont(attr
->GetFont());
1019 wxCoord x
= wd
*m_widthCol
+ (m_widthCol
- width
) / 2;
1020 dc
.DrawText(dayStr
, x
, y
+ 1);
1022 if ( !isSel
&& attr
&& attr
->HasBorder() )
1025 if ( attr
->HasBorderColour() )
1027 colBorder
= attr
->GetBorderColour();
1031 colBorder
= m_foregroundColour
;
1034 wxPen
pen(colBorder
, 1, wxSOLID
);
1036 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1038 switch ( attr
->GetBorder() )
1040 case wxCAL_BORDER_SQUARE
:
1041 dc
.DrawRectangle(x
- 2, y
,
1042 width
+ 4, m_heightRow
);
1045 case wxCAL_BORDER_ROUND
:
1046 dc
.DrawEllipse(x
- 2, y
,
1047 width
+ 4, m_heightRow
);
1051 wxFAIL_MSG(_T("unknown border type"));
1055 if ( changedColours
)
1057 dc
.SetTextForeground(m_foregroundColour
);
1058 dc
.SetTextBackground(m_backgroundColour
);
1066 //else: just don't draw it
1068 date
+= wxDateSpan::Day();
1072 // Greying out out-of-range background
1073 bool showSurrounding
= (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) != 0;
1075 date
= ( showSurrounding
) ? GetStartDate() : wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear());
1076 if ( !IsDateInRange(date
) )
1078 wxDateTime firstOOR
= GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1080 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1081 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1083 HighlightRange(&dc
, date
, firstOOR
, wxTRANSPARENT_PEN
, &oorbrush
);
1086 date
= ( showSurrounding
) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear());
1087 if ( !IsDateInRange(date
) )
1089 wxDateTime firstOOR
= GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1091 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1092 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1094 HighlightRange(&dc
, firstOOR
, date
, wxTRANSPARENT_PEN
, &oorbrush
);
1098 wxLogDebug("+++ finished painting");
1102 void wxCalendarCtrl::RefreshDate(const wxDateTime
& date
)
1108 // always refresh the whole row at once because our OnPaint() will draw
1109 // the whole row anyhow - and this allows the small optimisation in
1110 // OnClick() below to work
1113 rect
.y
= (m_heightRow
* GetWeek(date
)) + m_rowOffset
;
1115 rect
.width
= 7*m_widthCol
;
1116 rect
.height
= m_heightRow
;
1119 // VZ: for some reason, the selected date seems to occupy more space under
1120 // MSW - this is probably some bug in the font size calculations, but I
1121 // don't know where exactly. This fix is ugly and leads to more
1122 // refreshes than really needed, but without it the selected days
1123 // leaves even more ugly underscores on screen.
1128 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
1131 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1134 Refresh(TRUE
, &rect
);
1137 void wxCalendarCtrl::HighlightRange(wxPaintDC
* pDC
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, wxPen
* pPen
, wxBrush
* pBrush
)
1139 // Highlights the given range using pen and brush
1140 // Does nothing if todate < fromdate
1144 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate
.Format("%d %m %Y"), todate
.Format("%d %m %Y"));
1147 if ( todate
>= fromdate
)
1154 // implicit: both dates must be currently shown - checked by GetDateCoord
1155 if ( GetDateCoord(fromdate
, &fd
, &fw
) && GetDateCoord(todate
, &td
, &tw
) )
1158 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd
, fw
, td
, tw
);
1160 if ( ( (tw
- fw
) == 1 ) && ( td
< fd
) )
1162 // special case: interval 7 days or less not in same week
1163 // split in two seperate intervals
1164 wxDateTime tfd
= fromdate
+ wxDateSpan::Days(7-fd
);
1165 wxDateTime ftd
= tfd
+ wxDateSpan::Day();
1167 wxLogDebug("Highlight: Seperate segments");
1170 HighlightRange(pDC
, fromdate
, tfd
, pPen
, pBrush
);
1171 HighlightRange(pDC
, ftd
, todate
, pPen
, pBrush
);
1176 wxPoint corners
[8]; // potentially 8 corners in polygon
1180 // simple case: same week
1182 corners
[0] = wxPoint((fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
);
1183 corners
[1] = wxPoint((fd
- 1) * m_widthCol
, ((fw
+ 1 ) * m_heightRow
) + m_rowOffset
);
1184 corners
[2] = wxPoint(td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
);
1185 corners
[3] = wxPoint(td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
);
1190 // "complex" polygon
1191 corners
[cidx
] = wxPoint((fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1195 corners
[cidx
] = wxPoint((fd
- 1) * m_widthCol
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1196 corners
[cidx
] = wxPoint(0, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1199 corners
[cidx
] = wxPoint(0, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1200 corners
[cidx
] = wxPoint(td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1204 corners
[cidx
] = wxPoint(td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1205 corners
[cidx
] = wxPoint(7 * m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1208 corners
[cidx
] = wxPoint(7 * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1214 pDC
->SetBrush(*pBrush
);
1216 pDC
->DrawPolygon(numpoints
, corners
);
1222 wxLogDebug("--- HighlightRange ---");
1226 bool wxCalendarCtrl::GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const
1231 wxLogDebug("+++ GetDateCoord: (%s) +++", date
.Format("%d %m %Y"));
1234 if ( IsDateShown(date
) )
1236 bool startOnMonday
= ( GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1239 *day
= date
.GetWeekDay();
1241 if ( *day
== 0 ) // sunday
1243 *day
= ( startOnMonday
) ? 7 : 1;
1247 day
+= ( startOnMonday
) ? 0 : 1;
1250 int targetmonth
= date
.GetMonth() + (12 * date
.GetYear());
1251 int thismonth
= m_date
.GetMonth() + (12 * m_date
.GetYear());
1254 if ( targetmonth
== thismonth
)
1256 *week
= GetWeek(date
);
1260 if ( targetmonth
< thismonth
)
1262 *week
= 1; // trivial
1264 else // targetmonth > thismonth
1270 // get the datecoord of the last day in the month currently shown
1272 wxLogDebug(" +++ LDOM +++");
1274 GetDateCoord(ldcm
.SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear()), &lastday
, &lastweek
);
1276 wxLogDebug(" --- LDOM ---");
1279 wxTimeSpan span
= date
- ldcm
;
1281 int daysfromlast
= span
.GetDays();
1283 wxLogDebug("daysfromlast: %i", daysfromlast
);
1285 if ( daysfromlast
+ lastday
> 7 ) // past week boundary
1287 int wholeweeks
= (daysfromlast
/ 7);
1288 *week
= wholeweeks
+ lastweek
;
1289 if ( (daysfromlast
- (7 * wholeweeks
) + lastday
) > 7 )
1309 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date
.Format("%d %m %Y"), *day
, *week
);
1315 // ----------------------------------------------------------------------------
1317 // ----------------------------------------------------------------------------
1319 void wxCalendarCtrl::OnDClick(wxMouseEvent
& event
)
1321 if ( HitTest(event
.GetPosition()) != wxCAL_HITTEST_DAY
)
1327 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1331 void wxCalendarCtrl::OnClick(wxMouseEvent
& event
)
1334 wxDateTime::WeekDay wday
;
1335 switch ( HitTest(event
.GetPosition(), &date
, &wday
) )
1337 case wxCAL_HITTEST_DAY
:
1338 if ( IsDateInRange(date
) )
1342 GenerateEvents(wxEVT_CALENDAR_DAY_CHANGED
,
1343 wxEVT_CALENDAR_SEL_CHANGED
);
1347 case wxCAL_HITTEST_HEADER
:
1349 wxCalendarEvent
event(this, wxEVT_CALENDAR_WEEKDAY_CLICKED
);
1350 event
.m_wday
= wday
;
1351 (void)GetEventHandler()->ProcessEvent(event
);
1355 case wxCAL_HITTEST_DECMONTH
:
1356 case wxCAL_HITTEST_INCMONTH
:
1357 case wxCAL_HITTEST_SURROUNDING_WEEK
:
1358 SetDateAndNotify(date
); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1362 wxFAIL_MSG(_T("unknown hittest code"));
1365 case wxCAL_HITTEST_NOWHERE
:
1371 wxCalendarHitTestResult
wxCalendarCtrl::HitTest(const wxPoint
& pos
,
1373 wxDateTime::WeekDay
*wd
)
1379 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1380 if ( (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
1384 // we need to find out if the hit is on left arrow, on month or on right arrow
1386 if ( wxRegion(m_leftArrowRect
).Contains(pos
) == wxInRegion
)
1390 if ( IsDateInRange(m_date
- wxDateSpan::Month()) )
1392 *date
= m_date
- wxDateSpan::Month();
1396 *date
= GetLowerDateLimit();
1400 return wxCAL_HITTEST_DECMONTH
;
1403 if ( wxRegion(m_rightArrowRect
).Contains(pos
) == wxInRegion
)
1407 if ( IsDateInRange(m_date
+ wxDateSpan::Month()) )
1409 *date
= m_date
+ wxDateSpan::Month();
1413 *date
= GetUpperDateLimit();
1417 return wxCAL_HITTEST_INCMONTH
;
1422 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1424 int wday
= pos
.x
/ m_widthCol
;
1425 // if ( y < m_heightRow )
1426 if ( y
< (m_heightRow
+ m_rowOffset
) )
1428 if ( y
> m_rowOffset
)
1432 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST
)
1434 wday
= wday
== 6 ? 0 : wday
+ 1;
1437 *wd
= (wxDateTime::WeekDay
)wday
;
1440 return wxCAL_HITTEST_HEADER
;
1444 return wxCAL_HITTEST_NOWHERE
;
1448 // int week = (y - m_heightRow) / m_heightRow;
1449 int week
= (y
- (m_heightRow
+ m_rowOffset
)) / m_heightRow
;
1450 if ( week
>= 6 || wday
>= 7 )
1452 return wxCAL_HITTEST_NOWHERE
;
1455 wxDateTime dt
= GetStartDate() + wxDateSpan::Days(7*week
+ wday
);
1457 if ( IsDateShown(dt
) )
1462 if ( dt
.GetMonth() == m_date
.GetMonth() )
1465 return wxCAL_HITTEST_DAY
;
1469 return wxCAL_HITTEST_SURROUNDING_WEEK
;
1474 return wxCAL_HITTEST_NOWHERE
;
1478 // ----------------------------------------------------------------------------
1479 // subcontrols events handling
1480 // ----------------------------------------------------------------------------
1482 void wxCalendarCtrl::OnMonthChange(wxCommandEvent
& event
)
1484 wxDateTime::Tm tm
= m_date
.GetTm();
1486 wxDateTime::Month mon
= (wxDateTime::Month
)event
.GetInt();
1487 if ( tm
.mday
> wxDateTime::GetNumberOfDays(mon
, tm
.year
) )
1489 tm
.mday
= wxDateTime::GetNumberOfDays(mon
, tm
.year
);
1492 wxDateTime target
= wxDateTime(tm
.mday
, mon
, tm
.year
);
1494 ChangeMonth(&target
);
1495 SetDateAndNotify(target
);
1498 void wxCalendarCtrl::OnYearChange(wxSpinEvent
& event
)
1500 wxDateTime::Tm tm
= m_date
.GetTm();
1502 int year
= (int)event
.GetInt();
1503 if ( tm
.mday
> wxDateTime::GetNumberOfDays(tm
.mon
, year
) )
1505 tm
.mday
= wxDateTime::GetNumberOfDays(tm
.mon
, year
);
1508 wxDateTime target
= wxDateTime(tm
.mday
, tm
.mon
, year
);
1510 if ( ChangeYear(&target
) )
1512 SetDateAndNotify(target
);
1516 // In this case we don't want to change the date. That would put us
1517 // inside the same year but a strange number of months forward/back..
1518 m_spinYear
->SetValue(target
.GetYear());
1523 // ----------------------------------------------------------------------------
1524 // keyboard interface
1525 // ----------------------------------------------------------------------------
1527 void wxCalendarCtrl::OnChar(wxKeyEvent
& event
)
1530 switch ( event
.KeyCode() )
1534 target
= m_date
+ wxDateSpan::Year();
1535 if ( ChangeYear(&target
) )
1537 SetDateAndNotify(target
);
1543 target
= m_date
- wxDateSpan::Year();
1544 if ( ChangeYear(&target
) )
1546 SetDateAndNotify(target
);
1551 target
= m_date
- wxDateSpan::Month();
1552 ChangeMonth(&target
);
1553 SetDateAndNotify(target
); // always
1557 target
= m_date
+ wxDateSpan::Month();
1558 ChangeMonth(&target
);
1559 SetDateAndNotify(target
); // always
1563 if ( event
.ControlDown() )
1565 target
= wxDateTime(m_date
).SetToNextWeekDay(
1566 GetWindowStyle() & wxCAL_MONDAY_FIRST
1567 ? wxDateTime::Sun
: wxDateTime::Sat
);
1568 if ( !IsDateInRange(target
) )
1570 target
= GetUpperDateLimit();
1572 SetDateAndNotify(target
);
1575 SetDateAndNotify(m_date
+ wxDateSpan::Day());
1579 if ( event
.ControlDown() )
1581 target
= wxDateTime(m_date
).SetToPrevWeekDay(
1582 GetWindowStyle() & wxCAL_MONDAY_FIRST
1583 ? wxDateTime::Mon
: wxDateTime::Sun
);
1584 if ( !IsDateInRange(target
) )
1586 target
= GetLowerDateLimit();
1588 SetDateAndNotify(target
);
1591 SetDateAndNotify(m_date
- wxDateSpan::Day());
1595 SetDateAndNotify(m_date
- wxDateSpan::Week());
1599 SetDateAndNotify(m_date
+ wxDateSpan::Week());
1603 if ( event
.ControlDown() )
1604 SetDateAndNotify(wxDateTime::Today());
1606 SetDateAndNotify(wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear()));
1610 SetDateAndNotify(wxDateTime(m_date
).SetToLastMonthDay());
1614 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1622 // ----------------------------------------------------------------------------
1623 // holidays handling
1624 // ----------------------------------------------------------------------------
1626 void wxCalendarCtrl::EnableHolidayDisplay(bool display
)
1628 long style
= GetWindowStyle();
1630 style
|= wxCAL_SHOW_HOLIDAYS
;
1632 style
&= ~wxCAL_SHOW_HOLIDAYS
;
1634 SetWindowStyle(style
);
1639 ResetHolidayAttrs();
1644 void wxCalendarCtrl::SetHolidayAttrs()
1646 if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS
)
1648 ResetHolidayAttrs();
1650 wxDateTime::Tm tm
= m_date
.GetTm();
1651 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
1652 dtEnd
= dtStart
.GetLastMonthDay();
1654 wxDateTimeArray hol
;
1655 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
1657 size_t count
= hol
.GetCount();
1658 for ( size_t n
= 0; n
< count
; n
++ )
1660 SetHoliday(hol
[n
].GetDay());
1665 void wxCalendarCtrl::SetHoliday(size_t day
)
1667 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in SetHoliday") );
1669 wxCalendarDateAttr
*attr
= GetAttr(day
);
1672 attr
= new wxCalendarDateAttr
;
1675 attr
->SetHoliday(TRUE
);
1677 // can't use SetAttr() because it would delete this pointer
1678 m_attrs
[day
- 1] = attr
;
1681 void wxCalendarCtrl::ResetHolidayAttrs()
1683 for ( size_t day
= 0; day
< 31; day
++ )
1687 m_attrs
[day
]->SetHoliday(FALSE
);
1692 // ----------------------------------------------------------------------------
1694 // ----------------------------------------------------------------------------
1696 void wxCalendarEvent::Init()
1698 m_wday
= wxDateTime::Inv_WeekDay
;
1701 wxCalendarEvent::wxCalendarEvent(wxCalendarCtrl
*cal
, wxEventType type
)
1702 : wxCommandEvent(type
, cal
->GetId())
1704 m_date
= cal
->GetDate();
1705 SetEventObject(cal
);
1708 #endif // wxUSE_CALENDARCTRL