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/spinctrl.h"
43 #include "wx/calctrl.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 class wxMonthComboBox
: public wxComboBox
54 wxMonthComboBox(wxCalendarCtrl
*cal
);
56 void OnMonthChange(wxCommandEvent
& event
) { m_cal
->OnMonthChange(event
); }
59 wxCalendarCtrl
*m_cal
;
64 class wxYearSpinCtrl
: public wxSpinCtrl
67 wxYearSpinCtrl(wxCalendarCtrl
*cal
);
69 void OnYearTextChange(wxCommandEvent
& event
) { m_cal
->OnYearChange(event
); }
70 void OnYearChange(wxSpinEvent
& event
) { m_cal
->OnYearChange(event
); }
73 wxCalendarCtrl
*m_cal
;
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 BEGIN_EVENT_TABLE(wxCalendarCtrl
, wxControl
)
83 EVT_PAINT(wxCalendarCtrl::OnPaint
)
85 EVT_CHAR(wxCalendarCtrl::OnChar
)
87 EVT_LEFT_DOWN(wxCalendarCtrl::OnClick
)
88 EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick
)
91 BEGIN_EVENT_TABLE(wxMonthComboBox
, wxComboBox
)
92 EVT_COMBOBOX(-1, wxMonthComboBox::OnMonthChange
)
95 BEGIN_EVENT_TABLE(wxYearSpinCtrl
, wxSpinCtrl
)
96 EVT_TEXT(-1, wxYearSpinCtrl::OnYearTextChange
)
97 EVT_SPINCTRL(-1, wxYearSpinCtrl::OnYearChange
)
100 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl
, wxControl
)
101 IMPLEMENT_DYNAMIC_CLASS(wxCalendarEvent
, wxCommandEvent
)
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_SEL_CHANGED
)
108 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DAY_CHANGED
)
109 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_MONTH_CHANGED
)
110 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_YEAR_CHANGED
)
111 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DOUBLECLICKED
)
112 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_WEEKDAY_CLICKED
)
114 // ============================================================================
116 // ============================================================================
118 // ----------------------------------------------------------------------------
119 // wxMonthComboBox and wxYearSpinCtrl
120 // ----------------------------------------------------------------------------
122 wxMonthComboBox::wxMonthComboBox(wxCalendarCtrl
*cal
)
123 : wxComboBox(cal
->GetParent(), -1,
128 wxCB_READONLY
| wxCLIP_SIBLINGS
)
133 for ( m
= wxDateTime::Jan
; m
< wxDateTime::Inv_Month
; wxNextMonth(m
) )
135 Append(wxDateTime::GetMonthName(m
));
138 SetSelection(m_cal
->GetDate().GetMonth());
139 SetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
|wxSIZE_AUTO_HEIGHT
);
142 wxYearSpinCtrl::wxYearSpinCtrl(wxCalendarCtrl
*cal
)
143 : wxSpinCtrl(cal
->GetParent(), -1,
144 cal
->GetDate().Format(_T("%Y")),
147 wxSP_ARROW_KEYS
| wxCLIP_SIBLINGS
,
148 -4300, 10000, cal
->GetDate().GetYear())
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 void wxCalendarCtrl::Init()
162 m_userChangedYear
= FALSE
;
167 wxDateTime::WeekDay wd
;
168 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
170 m_weekdays
[wd
] = wxDateTime::GetWeekDayName(wd
, wxDateTime::Name_Abbr
);
173 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
178 m_colHighlightFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
179 m_colHighlightBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
181 m_colHolidayFg
= *wxRED
;
182 // don't set m_colHolidayBg - by default, same as our bg colour
184 m_colHeaderFg
= *wxBLUE
;
185 m_colHeaderBg
= *wxLIGHT_GREY
;
188 bool wxCalendarCtrl::Create(wxWindow
*parent
,
190 const wxDateTime
& date
,
194 const wxString
& name
)
196 if ( !wxControl::Create(parent
, id
, pos
, size
,
197 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
198 wxDefaultValidator
, name
) )
203 // needed to get the arrow keys normally used for the dialog navigation
204 SetWindowStyle(style
| wxWANTS_CHARS
);
206 m_date
= date
.IsValid() ? date
: wxDateTime::Today();
208 m_lowdate
= wxDefaultDateTime
;
209 m_highdate
= wxDefaultDateTime
;
211 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
213 m_spinYear
= new wxYearSpinCtrl(this);
214 m_staticYear
= new wxStaticText(GetParent(), -1, m_date
.Format(_T("%Y")),
215 wxDefaultPosition
, wxDefaultSize
,
218 m_comboMonth
= new wxMonthComboBox(this);
219 m_staticMonth
= new wxStaticText(GetParent(), -1, m_date
.Format(_T("%B")),
220 wxDefaultPosition
, wxDefaultSize
,
224 ShowCurrentControls();
227 if ( size
.x
== -1 || size
.y
== -1 )
229 sizeReal
= DoGetBestSize();
240 // we need to set the position as well because the main control position
241 // is not the same as the one specified in pos if we have the controls
243 SetSize(pos
.x
, pos
.y
, sizeReal
.x
, sizeReal
.y
);
245 SetBackgroundColour(*wxWHITE
);
246 SetFont(*wxSWISS_FONT
);
253 wxCalendarCtrl::~wxCalendarCtrl()
255 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
261 // ----------------------------------------------------------------------------
262 // forward wxWin functions to subcontrols
263 // ----------------------------------------------------------------------------
265 bool wxCalendarCtrl::Destroy()
268 m_staticYear
->Destroy();
270 m_spinYear
->Destroy();
272 m_comboMonth
->Destroy();
274 m_staticMonth
->Destroy();
279 m_staticMonth
= NULL
;
281 return wxControl::Destroy();
284 bool wxCalendarCtrl::Show(bool show
)
286 if ( !wxControl::Show(show
) )
291 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
293 if ( GetMonthControl() )
295 GetMonthControl()->Show(show
);
296 GetYearControl()->Show(show
);
303 bool wxCalendarCtrl::Enable(bool enable
)
305 if ( !wxControl::Enable(enable
) )
310 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
312 GetMonthControl()->Enable(enable
);
313 GetYearControl()->Enable(enable
);
319 // ----------------------------------------------------------------------------
320 // enable/disable month/year controls
321 // ----------------------------------------------------------------------------
323 void wxCalendarCtrl::ShowCurrentControls()
325 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
327 if ( AllowMonthChange() )
329 m_comboMonth
->Show();
330 m_staticMonth
->Hide();
332 if ( AllowYearChange() )
335 m_staticYear
->Hide();
343 m_comboMonth
->Hide();
344 m_staticMonth
->Show();
347 // year change not allowed here
349 m_staticYear
->Show();
353 wxControl
*wxCalendarCtrl::GetMonthControl() const
355 return AllowMonthChange() ? (wxControl
*)m_comboMonth
: (wxControl
*)m_staticMonth
;
358 wxControl
*wxCalendarCtrl::GetYearControl() const
360 return AllowYearChange() ? (wxControl
*)m_spinYear
: (wxControl
*)m_staticYear
;
363 void wxCalendarCtrl::EnableYearChange(bool enable
)
365 if ( enable
!= AllowYearChange() )
367 long style
= GetWindowStyle();
369 style
&= ~wxCAL_NO_YEAR_CHANGE
;
371 style
|= wxCAL_NO_YEAR_CHANGE
;
372 SetWindowStyle(style
);
374 ShowCurrentControls();
375 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
382 void wxCalendarCtrl::EnableMonthChange(bool enable
)
384 if ( enable
!= AllowMonthChange() )
386 long style
= GetWindowStyle();
388 style
&= ~wxCAL_NO_MONTH_CHANGE
;
390 style
|= wxCAL_NO_MONTH_CHANGE
;
391 SetWindowStyle(style
);
393 ShowCurrentControls();
394 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
401 // ----------------------------------------------------------------------------
403 // ----------------------------------------------------------------------------
405 bool wxCalendarCtrl::SetDate(const wxDateTime
& date
)
409 bool sameMonth
= m_date
.GetMonth() == date
.GetMonth(),
410 sameYear
= m_date
.GetYear() == date
.GetYear();
412 if ( IsDateInRange(date
) )
414 if ( sameMonth
&& sameYear
)
416 // just change the day
421 if ( AllowMonthChange() && (AllowYearChange() || sameYear
) )
426 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
428 // update the controls
429 m_comboMonth
->SetSelection(m_date
.GetMonth());
431 if ( AllowYearChange() )
433 if ( !m_userChangedYear
)
434 m_spinYear
->SetValue(m_date
.Format(_T("%Y")));
435 else // don't overwrite what the user typed in
436 m_userChangedYear
= FALSE
;
440 // as the month changed, holidays did too
443 // update the calendar
457 void wxCalendarCtrl::ChangeDay(const wxDateTime
& date
)
459 if ( m_date
!= date
)
461 // we need to refresh the row containing the old date and the one
462 // containing the new one
463 wxDateTime dateOld
= m_date
;
466 RefreshDate(dateOld
);
468 // if the date is in the same row, it was already drawn correctly
469 if ( GetWeek(m_date
) != GetWeek(dateOld
) )
476 void wxCalendarCtrl::SetDateAndNotify(const wxDateTime
& date
)
478 wxDateTime::Tm tm1
= m_date
.GetTm(),
482 if ( tm1
.year
!= tm2
.year
)
483 type
= wxEVT_CALENDAR_YEAR_CHANGED
;
484 else if ( tm1
.mon
!= tm2
.mon
)
485 type
= wxEVT_CALENDAR_MONTH_CHANGED
;
486 else if ( tm1
.mday
!= tm2
.mday
)
487 type
= wxEVT_CALENDAR_DAY_CHANGED
;
493 GenerateEvents(type
, wxEVT_CALENDAR_SEL_CHANGED
);
497 // ----------------------------------------------------------------------------
499 // ----------------------------------------------------------------------------
501 bool wxCalendarCtrl::SetLowerDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
505 if ( !(date
.IsValid()) || ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : TRUE
) )
517 bool wxCalendarCtrl::SetUpperDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
521 if ( !(date
.IsValid()) || ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : TRUE
) )
533 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& lowerdate
/* = wxDefaultDateTime */, const wxDateTime
& upperdate
/* = wxDefaultDateTime */)
538 ( !( lowerdate
.IsValid() ) || ( ( upperdate
.IsValid() ) ? ( lowerdate
<= upperdate
) : TRUE
) ) &&
539 ( !( upperdate
.IsValid() ) || ( ( lowerdate
.IsValid() ) ? ( upperdate
>= lowerdate
) : TRUE
) ) )
541 m_lowdate
= lowerdate
;
542 m_highdate
= upperdate
;
552 // ----------------------------------------------------------------------------
554 // ----------------------------------------------------------------------------
556 wxDateTime
wxCalendarCtrl::GetStartDate() const
558 wxDateTime::Tm tm
= m_date
.GetTm();
560 wxDateTime date
= wxDateTime(1, tm
.mon
, tm
.year
);
563 date
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
564 ? wxDateTime::Mon
: wxDateTime::Sun
);
566 if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
)
568 // We want to offset the calendar if we start on the first..
569 if ( date
.GetDay() == 1 )
571 date
-= wxDateSpan::Week();
578 bool wxCalendarCtrl::IsDateShown(const wxDateTime
& date
) const
580 if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
582 return date
.GetMonth() == m_date
.GetMonth();
590 bool wxCalendarCtrl::IsDateInRange(const wxDateTime
& date
) const
593 // Check if the given date is in the range specified
594 retval
= ( ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : TRUE
)
595 && ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : TRUE
) );
599 bool wxCalendarCtrl::ChangeYear(wxDateTime
* target
) const
603 if ( !(IsDateInRange(*target
)) )
605 if ( target
->GetYear() < m_date
.GetYear() )
607 if ( target
->GetYear() >= GetLowerDateLimit().GetYear() )
609 *target
= GetLowerDateLimit();
619 if ( target
->GetYear() <= GetUpperDateLimit().GetYear() )
621 *target
= GetUpperDateLimit();
638 bool wxCalendarCtrl::ChangeMonth(wxDateTime
* target
) const
642 if ( !(IsDateInRange(*target
)) )
646 if ( target
->GetMonth() < m_date
.GetMonth() )
648 *target
= GetLowerDateLimit();
652 *target
= GetUpperDateLimit();
659 size_t wxCalendarCtrl::GetWeek(const wxDateTime
& date
) const
661 size_t retval
= date
.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
662 ? wxDateTime::Monday_First
663 : wxDateTime::Sunday_First
);
665 if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
667 // we need to offset an extra week if we "start" on the 1st of the month
668 wxDateTime::Tm tm
= date
.GetTm();
670 wxDateTime datetest
= wxDateTime(1, tm
.mon
, tm
.year
);
673 datetest
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
674 ? wxDateTime::Mon
: wxDateTime::Sun
);
676 if ( datetest
.GetDay() == 1 )
685 // ----------------------------------------------------------------------------
687 // ----------------------------------------------------------------------------
689 // this is a composite control and it must arrange its parts each time its
690 // size or position changes: the combobox and spinctrl are along the top of
691 // the available area and the calendar takes up therest of the space
693 // the static controls are supposed to be always smaller than combo/spin so we
694 // always use the latter for size calculations and position the static to take
697 // the constants used for the layout
698 #define VERT_MARGIN 5 // distance between combo and calendar
700 #define HORZ_MARGIN 5 // spin
702 #define HORZ_MARGIN 15 // spin
704 wxSize
wxCalendarCtrl::DoGetBestSize() const
706 // calc the size of the calendar
707 ((wxCalendarCtrl
*)this)->RecalcGeometry(); // const_cast
709 wxCoord width
= 7*m_widthCol
,
710 height
= 7*m_heightRow
+ m_rowOffset
+ VERT_MARGIN
;
712 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
714 // the combobox doesn't report its height correctly (it returns the
715 // height including the drop down list) so don't use it
716 height
+= m_spinYear
->GetBestSize().y
;
719 if ( !HasFlag(wxBORDER_NONE
) )
721 // the border would clip the last line otherwise
726 return wxSize(width
, height
);
729 void wxCalendarCtrl::DoSetSize(int x
, int y
,
730 int width
, int height
,
733 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
736 void wxCalendarCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
740 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
742 wxSize sizeCombo
= m_comboMonth
->GetSize();
743 wxSize sizeStatic
= m_staticMonth
->GetSize();
745 int dy
= (sizeCombo
.y
- sizeStatic
.y
) / 2;
747 m_comboMonth
->Move(x
, y
);
748 m_staticMonth
->SetSize(x
, y
+ dy
, sizeCombo
.x
, sizeStatic
.y
);
750 int xDiff
= sizeCombo
.x
+ HORZ_MARGIN
;
752 m_spinYear
->SetSize(x
+ xDiff
, y
, width
- xDiff
, sizeCombo
.y
);
753 m_staticYear
->SetSize(x
+ xDiff
, y
+ dy
, width
- xDiff
, sizeStatic
.y
);
755 wxSize sizeSpin
= m_spinYear
->GetSize();
756 yDiff
= wxMax(sizeSpin
.y
, sizeCombo
.y
) + VERT_MARGIN
;
758 else // no controls on the top
763 wxControl::DoMoveWindow(x
, y
+ yDiff
, width
, height
- yDiff
);
766 void wxCalendarCtrl::DoGetPosition(int *x
, int *y
) const
768 wxControl::DoGetPosition(x
, y
);
770 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
772 // our real top corner is not in this position
775 *y
-= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
780 void wxCalendarCtrl::DoGetSize(int *width
, int *height
) const
782 wxControl::DoGetSize(width
, height
);
784 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
786 // our real height is bigger
787 if ( height
&& GetMonthControl())
789 *height
+= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
794 void wxCalendarCtrl::RecalcGeometry()
796 if ( m_widthCol
!= 0 )
803 // determine the column width (we assume that the weekday names are always
804 // wider (in any language) than the numbers)
806 wxDateTime::WeekDay wd
;
807 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
810 dc
.GetTextExtent(m_weekdays
[wd
], &width
, &m_heightRow
);
811 if ( width
> m_widthCol
)
817 // leave some margins
821 m_rowOffset
= (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) ? m_heightRow
: 0; // conditional in relation to style
824 // ----------------------------------------------------------------------------
826 // ----------------------------------------------------------------------------
828 void wxCalendarCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
837 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
838 m_date
.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
844 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
846 // draw the sequential month-selector
848 dc
.SetBackgroundMode(wxTRANSPARENT
);
849 dc
.SetTextForeground(*wxBLACK
);
850 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
851 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
852 dc
.DrawRectangle(0, y
, 7*m_widthCol
, m_heightRow
);
854 // Get extent of month-name + year
855 wxCoord monthw
, monthh
;
856 wxString headertext
= m_date
.Format(wxT("%B %Y"));
857 dc
.GetTextExtent(headertext
, &monthw
, &monthh
);
859 // draw month-name centered above weekdays
860 wxCoord monthx
= ((m_widthCol
* 7) - monthw
) / 2;
861 wxCoord monthy
= ((m_heightRow
- monthh
) / 2) + y
;
862 dc
.DrawText(headertext
, monthx
, monthy
);
864 // calculate the "month-arrows"
865 wxPoint leftarrow
[3];
866 wxPoint rightarrow
[3];
868 int arrowheight
= monthh
/ 2;
870 leftarrow
[0] = wxPoint(0, arrowheight
/ 2);
871 leftarrow
[1] = wxPoint(arrowheight
/ 2, 0);
872 leftarrow
[2] = wxPoint(arrowheight
/ 2, arrowheight
- 1);
874 rightarrow
[0] = wxPoint(0, 0);
875 rightarrow
[1] = wxPoint(arrowheight
/ 2, arrowheight
/ 2);
876 rightarrow
[2] = wxPoint(0, arrowheight
- 1);
878 // draw the "month-arrows"
880 wxCoord arrowy
= (m_heightRow
- arrowheight
) / 2;
881 wxCoord larrowx
= (m_widthCol
- (arrowheight
/ 2)) / 2;
882 wxCoord rarrowx
= ((m_widthCol
- (arrowheight
/ 2)) / 2) + m_widthCol
*6;
883 m_leftArrowRect
= wxRect(0, 0, 0, 0);
884 m_rightArrowRect
= wxRect(0, 0, 0, 0);
886 if ( AllowMonthChange() )
888 wxDateTime ldpm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) - wxDateSpan::Day(); // last day prev month
889 // Check if range permits change
890 if ( IsDateInRange(ldpm
) && ( ( ldpm
.GetYear() == m_date
.GetYear() ) ? TRUE
: AllowYearChange() ) )
892 m_leftArrowRect
= wxRect(larrowx
- 3, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
893 dc
.SetBrush(wxBrush(*wxBLACK
, wxSOLID
));
894 dc
.SetPen(wxPen(*wxBLACK
, 1, wxSOLID
));
895 dc
.DrawPolygon(3, leftarrow
, larrowx
, arrowy
, wxWINDING_RULE
);
896 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
897 dc
.DrawRectangle(m_leftArrowRect
);
899 wxDateTime fdnm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) + wxDateSpan::Month(); // first day next month
900 if ( IsDateInRange(fdnm
) && ( ( fdnm
.GetYear() == m_date
.GetYear() ) ? TRUE
: AllowYearChange() ) )
902 m_rightArrowRect
= wxRect(rarrowx
- 4, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
903 dc
.SetBrush(wxBrush(*wxBLACK
, wxSOLID
));
904 dc
.SetPen(wxPen(*wxBLACK
, 1, wxSOLID
));
905 dc
.DrawPolygon(3, rightarrow
, rarrowx
, arrowy
, wxWINDING_RULE
);
906 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
907 dc
.DrawRectangle(m_rightArrowRect
);
914 // first draw the week days
915 if ( IsExposed(0, y
, 7*m_widthCol
, m_heightRow
) )
918 wxLogDebug("painting the header");
921 dc
.SetBackgroundMode(wxTRANSPARENT
);
922 dc
.SetTextForeground(m_colHeaderFg
);
923 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
924 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
925 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
927 bool startOnMonday
= (GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
928 for ( size_t wd
= 0; wd
< 7; wd
++ )
932 n
= wd
== 6 ? 0 : wd
+ 1;
936 dc
.GetTextExtent(m_weekdays
[n
], &dayw
, &dayh
);
937 dc
.DrawText(m_weekdays
[n
], (wd
*m_widthCol
) + ((m_widthCol
- dayw
) / 2), y
); // center the day-name
941 // then the calendar itself
942 dc
.SetTextForeground(*wxBLACK
);
943 //dc.SetFont(*wxNORMAL_FONT);
946 wxDateTime date
= GetStartDate();
949 wxLogDebug("starting calendar from %s\n",
950 date
.Format("%a %d-%m-%Y %H:%M:%S").c_str());
953 dc
.SetBackgroundMode(wxSOLID
);
954 for ( size_t nWeek
= 1; nWeek
<= 6; nWeek
++, y
+= m_heightRow
)
956 // if the update region doesn't intersect this row, don't paint it
957 if ( !IsExposed(0, y
, 7*m_widthCol
, m_heightRow
- 1) )
959 date
+= wxDateSpan::Week();
965 wxLogDebug("painting week %d at y = %d\n", nWeek
, y
);
968 for ( size_t wd
= 0; wd
< 7; wd
++ )
970 if ( IsDateShown(date
) )
972 // don't use wxDate::Format() which prepends 0s
973 unsigned int day
= date
.GetDay();
974 wxString dayStr
= wxString::Format(_T("%u"), day
);
976 dc
.GetTextExtent(dayStr
, &width
, (wxCoord
*)NULL
);
978 bool changedColours
= FALSE
,
982 wxCalendarDateAttr
*attr
= NULL
;
984 if ( date
.GetMonth() != m_date
.GetMonth() || !IsDateInRange(date
) )
986 // surrounding week or out-of-range
988 dc
.SetTextForeground(*wxLIGHT_GREY
);
989 changedColours
= TRUE
;
993 isSel
= date
.IsSameDate(m_date
);
994 attr
= m_attrs
[day
- 1];
998 dc
.SetTextForeground(m_colHighlightFg
);
999 dc
.SetTextBackground(m_colHighlightBg
);
1001 changedColours
= TRUE
;
1005 wxColour colFg
, colBg
;
1007 if ( attr
->IsHoliday() )
1009 colFg
= m_colHolidayFg
;
1010 colBg
= m_colHolidayBg
;
1014 colFg
= attr
->GetTextColour();
1015 colBg
= attr
->GetBackgroundColour();
1020 dc
.SetTextForeground(colFg
);
1021 changedColours
= TRUE
;
1026 dc
.SetTextBackground(colBg
);
1027 changedColours
= TRUE
;
1030 if ( attr
->HasFont() )
1032 dc
.SetFont(attr
->GetFont());
1038 wxCoord x
= wd
*m_widthCol
+ (m_widthCol
- width
) / 2;
1039 dc
.DrawText(dayStr
, x
, y
+ 1);
1041 if ( !isSel
&& attr
&& attr
->HasBorder() )
1044 if ( attr
->HasBorderColour() )
1046 colBorder
= attr
->GetBorderColour();
1050 colBorder
= m_foregroundColour
;
1053 wxPen
pen(colBorder
, 1, wxSOLID
);
1055 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1057 switch ( attr
->GetBorder() )
1059 case wxCAL_BORDER_SQUARE
:
1060 dc
.DrawRectangle(x
- 2, y
,
1061 width
+ 4, m_heightRow
);
1064 case wxCAL_BORDER_ROUND
:
1065 dc
.DrawEllipse(x
- 2, y
,
1066 width
+ 4, m_heightRow
);
1070 wxFAIL_MSG(_T("unknown border type"));
1074 if ( changedColours
)
1076 dc
.SetTextForeground(m_foregroundColour
);
1077 dc
.SetTextBackground(m_backgroundColour
);
1085 //else: just don't draw it
1087 date
+= wxDateSpan::Day();
1091 // Greying out out-of-range background
1092 bool showSurrounding
= (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) != 0;
1094 date
= ( showSurrounding
) ? GetStartDate() : wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear());
1095 if ( !IsDateInRange(date
) )
1097 wxDateTime firstOOR
= GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1099 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1100 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1102 HighlightRange(&dc
, date
, firstOOR
, wxTRANSPARENT_PEN
, &oorbrush
);
1105 date
= ( showSurrounding
) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear());
1106 if ( !IsDateInRange(date
) )
1108 wxDateTime firstOOR
= GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1110 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1111 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1113 HighlightRange(&dc
, firstOOR
, date
, wxTRANSPARENT_PEN
, &oorbrush
);
1117 wxLogDebug("+++ finished painting");
1121 void wxCalendarCtrl::RefreshDate(const wxDateTime
& date
)
1127 // always refresh the whole row at once because our OnPaint() will draw
1128 // the whole row anyhow - and this allows the small optimisation in
1129 // OnClick() below to work
1132 rect
.y
= (m_heightRow
* GetWeek(date
)) + m_rowOffset
;
1134 rect
.width
= 7*m_widthCol
;
1135 rect
.height
= m_heightRow
;
1138 // VZ: for some reason, the selected date seems to occupy more space under
1139 // MSW - this is probably some bug in the font size calculations, but I
1140 // don't know where exactly. This fix is ugly and leads to more
1141 // refreshes than really needed, but without it the selected days
1142 // leaves even more ugly underscores on screen.
1147 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
1150 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1153 Refresh(TRUE
, &rect
);
1156 void wxCalendarCtrl::HighlightRange(wxPaintDC
* pDC
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, wxPen
* pPen
, wxBrush
* pBrush
)
1158 // Highlights the given range using pen and brush
1159 // Does nothing if todate < fromdate
1163 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate
.Format("%d %m %Y"), todate
.Format("%d %m %Y"));
1166 if ( todate
>= fromdate
)
1173 // implicit: both dates must be currently shown - checked by GetDateCoord
1174 if ( GetDateCoord(fromdate
, &fd
, &fw
) && GetDateCoord(todate
, &td
, &tw
) )
1177 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd
, fw
, td
, tw
);
1179 if ( ( (tw
- fw
) == 1 ) && ( td
< fd
) )
1181 // special case: interval 7 days or less not in same week
1182 // split in two seperate intervals
1183 wxDateTime tfd
= fromdate
+ wxDateSpan::Days(7-fd
);
1184 wxDateTime ftd
= tfd
+ wxDateSpan::Day();
1186 wxLogDebug("Highlight: Seperate segments");
1189 HighlightRange(pDC
, fromdate
, tfd
, pPen
, pBrush
);
1190 HighlightRange(pDC
, ftd
, todate
, pPen
, pBrush
);
1195 wxPoint corners
[8]; // potentially 8 corners in polygon
1199 // simple case: same week
1201 corners
[0] = wxPoint((fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
);
1202 corners
[1] = wxPoint((fd
- 1) * m_widthCol
, ((fw
+ 1 ) * m_heightRow
) + m_rowOffset
);
1203 corners
[2] = wxPoint(td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
);
1204 corners
[3] = wxPoint(td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
);
1209 // "complex" polygon
1210 corners
[cidx
] = wxPoint((fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1214 corners
[cidx
] = wxPoint((fd
- 1) * m_widthCol
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1215 corners
[cidx
] = wxPoint(0, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1218 corners
[cidx
] = wxPoint(0, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1219 corners
[cidx
] = wxPoint(td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1223 corners
[cidx
] = wxPoint(td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1224 corners
[cidx
] = wxPoint(7 * m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1227 corners
[cidx
] = wxPoint(7 * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1233 pDC
->SetBrush(*pBrush
);
1235 pDC
->DrawPolygon(numpoints
, corners
);
1241 wxLogDebug("--- HighlightRange ---");
1245 bool wxCalendarCtrl::GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const
1250 wxLogDebug("+++ GetDateCoord: (%s) +++", date
.Format("%d %m %Y"));
1253 if ( IsDateShown(date
) )
1255 bool startOnMonday
= ( GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1258 *day
= date
.GetWeekDay();
1260 if ( *day
== 0 ) // sunday
1262 *day
= ( startOnMonday
) ? 7 : 1;
1266 day
+= ( startOnMonday
) ? 0 : 1;
1269 int targetmonth
= date
.GetMonth() + (12 * date
.GetYear());
1270 int thismonth
= m_date
.GetMonth() + (12 * m_date
.GetYear());
1273 if ( targetmonth
== thismonth
)
1275 *week
= GetWeek(date
);
1279 if ( targetmonth
< thismonth
)
1281 *week
= 1; // trivial
1283 else // targetmonth > thismonth
1289 // get the datecoord of the last day in the month currently shown
1291 wxLogDebug(" +++ LDOM +++");
1293 GetDateCoord(ldcm
.SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear()), &lastday
, &lastweek
);
1295 wxLogDebug(" --- LDOM ---");
1298 wxTimeSpan span
= date
- ldcm
;
1300 int daysfromlast
= span
.GetDays();
1302 wxLogDebug("daysfromlast: %i", daysfromlast
);
1304 if ( daysfromlast
+ lastday
> 7 ) // past week boundary
1306 int wholeweeks
= (daysfromlast
/ 7);
1307 *week
= wholeweeks
+ lastweek
;
1308 if ( (daysfromlast
- (7 * wholeweeks
) + lastday
) > 7 )
1328 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date
.Format("%d %m %Y"), *day
, *week
);
1334 // ----------------------------------------------------------------------------
1336 // ----------------------------------------------------------------------------
1338 void wxCalendarCtrl::OnDClick(wxMouseEvent
& event
)
1340 if ( HitTest(event
.GetPosition()) != wxCAL_HITTEST_DAY
)
1346 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1350 void wxCalendarCtrl::OnClick(wxMouseEvent
& event
)
1353 wxDateTime::WeekDay wday
;
1354 switch ( HitTest(event
.GetPosition(), &date
, &wday
) )
1356 case wxCAL_HITTEST_DAY
:
1357 if ( IsDateInRange(date
) )
1361 GenerateEvents(wxEVT_CALENDAR_DAY_CHANGED
,
1362 wxEVT_CALENDAR_SEL_CHANGED
);
1366 case wxCAL_HITTEST_HEADER
:
1368 wxCalendarEvent
event(this, wxEVT_CALENDAR_WEEKDAY_CLICKED
);
1369 event
.m_wday
= wday
;
1370 (void)GetEventHandler()->ProcessEvent(event
);
1374 case wxCAL_HITTEST_DECMONTH
:
1375 case wxCAL_HITTEST_INCMONTH
:
1376 case wxCAL_HITTEST_SURROUNDING_WEEK
:
1377 SetDateAndNotify(date
); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1381 wxFAIL_MSG(_T("unknown hittest code"));
1384 case wxCAL_HITTEST_NOWHERE
:
1390 wxCalendarHitTestResult
wxCalendarCtrl::HitTest(const wxPoint
& pos
,
1392 wxDateTime::WeekDay
*wd
)
1398 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1399 if ( (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
1403 // we need to find out if the hit is on left arrow, on month or on right arrow
1405 if ( wxRegion(m_leftArrowRect
).Contains(pos
) == wxInRegion
)
1409 if ( IsDateInRange(m_date
- wxDateSpan::Month()) )
1411 *date
= m_date
- wxDateSpan::Month();
1415 *date
= GetLowerDateLimit();
1419 return wxCAL_HITTEST_DECMONTH
;
1422 if ( wxRegion(m_rightArrowRect
).Contains(pos
) == wxInRegion
)
1426 if ( IsDateInRange(m_date
+ wxDateSpan::Month()) )
1428 *date
= m_date
+ wxDateSpan::Month();
1432 *date
= GetUpperDateLimit();
1436 return wxCAL_HITTEST_INCMONTH
;
1441 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1443 int wday
= pos
.x
/ m_widthCol
;
1444 // if ( y < m_heightRow )
1445 if ( y
< (m_heightRow
+ m_rowOffset
) )
1447 if ( y
> m_rowOffset
)
1451 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST
)
1453 wday
= wday
== 6 ? 0 : wday
+ 1;
1456 *wd
= (wxDateTime::WeekDay
)wday
;
1459 return wxCAL_HITTEST_HEADER
;
1463 return wxCAL_HITTEST_NOWHERE
;
1467 // int week = (y - m_heightRow) / m_heightRow;
1468 int week
= (y
- (m_heightRow
+ m_rowOffset
)) / m_heightRow
;
1469 if ( week
>= 6 || wday
>= 7 )
1471 return wxCAL_HITTEST_NOWHERE
;
1474 wxDateTime dt
= GetStartDate() + wxDateSpan::Days(7*week
+ wday
);
1476 if ( IsDateShown(dt
) )
1481 if ( dt
.GetMonth() == m_date
.GetMonth() )
1484 return wxCAL_HITTEST_DAY
;
1488 return wxCAL_HITTEST_SURROUNDING_WEEK
;
1493 return wxCAL_HITTEST_NOWHERE
;
1497 // ----------------------------------------------------------------------------
1498 // subcontrols events handling
1499 // ----------------------------------------------------------------------------
1501 void wxCalendarCtrl::OnMonthChange(wxCommandEvent
& event
)
1503 wxDateTime::Tm tm
= m_date
.GetTm();
1505 wxDateTime::Month mon
= (wxDateTime::Month
)event
.GetInt();
1506 if ( tm
.mday
> wxDateTime::GetNumberOfDays(mon
, tm
.year
) )
1508 tm
.mday
= wxDateTime::GetNumberOfDays(mon
, tm
.year
);
1511 wxDateTime target
= wxDateTime(tm
.mday
, mon
, tm
.year
);
1513 ChangeMonth(&target
);
1514 SetDateAndNotify(target
);
1517 void wxCalendarCtrl::OnYearChange(wxCommandEvent
& event
)
1519 int year
= (int)event
.GetInt();
1520 if ( year
== INT_MIN
)
1522 // invalid year in the spin control, ignore it
1526 // set the flag for SetDate(): otherwise it would overwrite the year
1527 // typed in by the user
1528 m_userChangedYear
= TRUE
;
1530 wxDateTime::Tm tm
= m_date
.GetTm();
1532 if ( tm
.mday
> wxDateTime::GetNumberOfDays(tm
.mon
, year
) )
1534 tm
.mday
= wxDateTime::GetNumberOfDays(tm
.mon
, year
);
1537 wxDateTime target
= wxDateTime(tm
.mday
, tm
.mon
, year
);
1539 if ( ChangeYear(&target
) )
1541 SetDateAndNotify(target
);
1545 // In this case we don't want to change the date. That would put us
1546 // inside the same year but a strange number of months forward/back..
1547 m_spinYear
->SetValue(target
.GetYear());
1551 // ----------------------------------------------------------------------------
1552 // keyboard interface
1553 // ----------------------------------------------------------------------------
1555 void wxCalendarCtrl::OnChar(wxKeyEvent
& event
)
1558 switch ( event
.KeyCode() )
1562 target
= m_date
+ wxDateSpan::Year();
1563 if ( ChangeYear(&target
) )
1565 SetDateAndNotify(target
);
1571 target
= m_date
- wxDateSpan::Year();
1572 if ( ChangeYear(&target
) )
1574 SetDateAndNotify(target
);
1579 target
= m_date
- wxDateSpan::Month();
1580 ChangeMonth(&target
);
1581 SetDateAndNotify(target
); // always
1585 target
= m_date
+ wxDateSpan::Month();
1586 ChangeMonth(&target
);
1587 SetDateAndNotify(target
); // always
1591 if ( event
.ControlDown() )
1593 target
= wxDateTime(m_date
).SetToNextWeekDay(
1594 GetWindowStyle() & wxCAL_MONDAY_FIRST
1595 ? wxDateTime::Sun
: wxDateTime::Sat
);
1596 if ( !IsDateInRange(target
) )
1598 target
= GetUpperDateLimit();
1600 SetDateAndNotify(target
);
1603 SetDateAndNotify(m_date
+ wxDateSpan::Day());
1607 if ( event
.ControlDown() )
1609 target
= wxDateTime(m_date
).SetToPrevWeekDay(
1610 GetWindowStyle() & wxCAL_MONDAY_FIRST
1611 ? wxDateTime::Mon
: wxDateTime::Sun
);
1612 if ( !IsDateInRange(target
) )
1614 target
= GetLowerDateLimit();
1616 SetDateAndNotify(target
);
1619 SetDateAndNotify(m_date
- wxDateSpan::Day());
1623 SetDateAndNotify(m_date
- wxDateSpan::Week());
1627 SetDateAndNotify(m_date
+ wxDateSpan::Week());
1631 if ( event
.ControlDown() )
1632 SetDateAndNotify(wxDateTime::Today());
1634 SetDateAndNotify(wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear()));
1638 SetDateAndNotify(wxDateTime(m_date
).SetToLastMonthDay());
1642 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1650 // ----------------------------------------------------------------------------
1651 // holidays handling
1652 // ----------------------------------------------------------------------------
1654 void wxCalendarCtrl::EnableHolidayDisplay(bool display
)
1656 long style
= GetWindowStyle();
1658 style
|= wxCAL_SHOW_HOLIDAYS
;
1660 style
&= ~wxCAL_SHOW_HOLIDAYS
;
1662 SetWindowStyle(style
);
1667 ResetHolidayAttrs();
1672 void wxCalendarCtrl::SetHolidayAttrs()
1674 if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS
)
1676 ResetHolidayAttrs();
1678 wxDateTime::Tm tm
= m_date
.GetTm();
1679 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
1680 dtEnd
= dtStart
.GetLastMonthDay();
1682 wxDateTimeArray hol
;
1683 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
1685 size_t count
= hol
.GetCount();
1686 for ( size_t n
= 0; n
< count
; n
++ )
1688 SetHoliday(hol
[n
].GetDay());
1693 void wxCalendarCtrl::SetHoliday(size_t day
)
1695 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in SetHoliday") );
1697 wxCalendarDateAttr
*attr
= GetAttr(day
);
1700 attr
= new wxCalendarDateAttr
;
1703 attr
->SetHoliday(TRUE
);
1705 // can't use SetAttr() because it would delete this pointer
1706 m_attrs
[day
- 1] = attr
;
1709 void wxCalendarCtrl::ResetHolidayAttrs()
1711 for ( size_t day
= 0; day
< 31; day
++ )
1715 m_attrs
[day
]->SetHoliday(FALSE
);
1720 // ----------------------------------------------------------------------------
1722 // ----------------------------------------------------------------------------
1724 void wxCalendarEvent::Init()
1726 m_wday
= wxDateTime::Inv_WeekDay
;
1729 wxCalendarEvent::wxCalendarEvent(wxCalendarCtrl
*cal
, wxEventType type
)
1730 : wxCommandEvent(type
, cal
->GetId())
1732 m_date
= cal
->GetDate();
1733 SetEventObject(cal
);
1736 #endif // wxUSE_CALENDARCTRL