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,
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")),
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
++ )
173 m_colHighlightFg
= ss
.GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
174 m_colHighlightBg
= ss
.GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
176 m_colHolidayFg
= *wxRED
;
177 // don't set m_colHolidayBg - by default, same as our bg colour
179 m_colHeaderFg
= *wxBLUE
;
180 m_colHeaderBg
= *wxLIGHT_GREY
;
183 bool wxCalendarCtrl::Create(wxWindow
*parent
,
185 const wxDateTime
& date
,
189 const wxString
& name
)
191 if ( !wxControl::Create(parent
, id
, pos
, size
,
192 style
| wxWANTS_CHARS
, 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 ( !(GetWindowStyle() & 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();
236 SetBackgroundColour(*wxWHITE
);
237 SetFont(*wxSWISS_FONT
);
244 wxCalendarCtrl::~wxCalendarCtrl()
246 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
252 // ----------------------------------------------------------------------------
253 // forward wxWin functions to subcontrols
254 // ----------------------------------------------------------------------------
256 bool wxCalendarCtrl::Show(bool show
)
258 if ( !wxControl::Show(show
) )
263 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
265 if ( GetMonthControl() )
267 GetMonthControl()->Show(show
);
268 GetYearControl()->Show(show
);
275 bool wxCalendarCtrl::Enable(bool enable
)
277 if ( !wxControl::Enable(enable
) )
282 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
284 GetMonthControl()->Enable(enable
);
285 GetYearControl()->Enable(enable
);
291 // ----------------------------------------------------------------------------
292 // enable/disable month/year controls
293 // ----------------------------------------------------------------------------
295 void wxCalendarCtrl::ShowCurrentControls()
297 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
299 if ( AllowMonthChange() )
301 m_comboMonth
->Show();
302 m_staticMonth
->Hide();
304 if ( AllowYearChange() )
307 m_staticYear
->Hide();
315 m_comboMonth
->Hide();
316 m_staticMonth
->Show();
319 // year change not allowed here
321 m_staticYear
->Show();
325 wxControl
*wxCalendarCtrl::GetMonthControl() const
327 return AllowMonthChange() ? (wxControl
*)m_comboMonth
: (wxControl
*)m_staticMonth
;
330 wxControl
*wxCalendarCtrl::GetYearControl() const
332 return AllowYearChange() ? (wxControl
*)m_spinYear
: (wxControl
*)m_staticYear
;
335 void wxCalendarCtrl::EnableYearChange(bool enable
)
337 if ( enable
!= AllowYearChange() )
339 long style
= GetWindowStyle();
341 style
&= ~wxCAL_NO_YEAR_CHANGE
;
343 style
|= wxCAL_NO_YEAR_CHANGE
;
344 SetWindowStyle(style
);
346 ShowCurrentControls();
347 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
354 void wxCalendarCtrl::EnableMonthChange(bool enable
)
356 if ( enable
!= AllowMonthChange() )
358 long style
= GetWindowStyle();
360 style
&= ~wxCAL_NO_MONTH_CHANGE
;
362 style
|= wxCAL_NO_MONTH_CHANGE
;
363 SetWindowStyle(style
);
365 ShowCurrentControls();
366 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
373 // ----------------------------------------------------------------------------
375 // ----------------------------------------------------------------------------
377 bool wxCalendarCtrl::SetDate(const wxDateTime
& date
)
381 bool sameMonth
= m_date
.GetMonth() == date
.GetMonth(),
382 sameYear
= m_date
.GetYear() == date
.GetYear();
384 if ( IsDateInRange(date
) )
386 if ( sameMonth
&& sameYear
)
388 // just change the day
394 if ( !AllowMonthChange() || (!AllowYearChange() && !sameYear
) )
404 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
406 // update the controls
407 m_comboMonth
->SetSelection(m_date
.GetMonth());
409 if ( AllowYearChange() )
411 m_spinYear
->SetValue(m_date
.Format(_T("%Y")));
415 // as the month changed, holidays did too
418 // update the calendar
426 void wxCalendarCtrl::ChangeDay(const wxDateTime
& date
)
428 if ( m_date
!= date
)
430 // we need to refresh the row containing the old date and the one
431 // containing the new one
432 wxDateTime dateOld
= m_date
;
435 RefreshDate(dateOld
);
437 // if the date is in the same row, it was already drawn correctly
438 if ( GetWeek(m_date
) != GetWeek(dateOld
) )
445 void wxCalendarCtrl::SetDateAndNotify(const wxDateTime
& date
)
447 wxDateTime::Tm tm1
= m_date
.GetTm(),
451 if ( tm1
.year
!= tm2
.year
)
452 type
= wxEVT_CALENDAR_YEAR_CHANGED
;
453 else if ( tm1
.mon
!= tm2
.mon
)
454 type
= wxEVT_CALENDAR_MONTH_CHANGED
;
455 else if ( tm1
.mday
!= tm2
.mday
)
456 type
= wxEVT_CALENDAR_DAY_CHANGED
;
462 GenerateEvents(type
, wxEVT_CALENDAR_SEL_CHANGED
);
466 // ----------------------------------------------------------------------------
468 // ----------------------------------------------------------------------------
470 bool wxCalendarCtrl::SetLowerDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
474 if ( !(date
.IsValid()) || ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : TRUE
) )
486 bool wxCalendarCtrl::SetUpperDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
490 if ( !(date
.IsValid()) || ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : TRUE
) )
502 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& lowerdate
/* = wxDefaultDateTime */, const wxDateTime
& upperdate
/* = wxDefaultDateTime */)
507 ( !( lowerdate
.IsValid() ) || ( ( upperdate
.IsValid() ) ? ( lowerdate
<= upperdate
) : TRUE
) ) &&
508 ( !( upperdate
.IsValid() ) || ( ( lowerdate
.IsValid() ) ? ( upperdate
>= lowerdate
) : TRUE
) ) )
510 m_lowdate
= lowerdate
;
511 m_highdate
= upperdate
;
521 // ----------------------------------------------------------------------------
523 // ----------------------------------------------------------------------------
525 wxDateTime
wxCalendarCtrl::GetStartDate() const
527 wxDateTime::Tm tm
= m_date
.GetTm();
529 wxDateTime date
= wxDateTime(1, tm
.mon
, tm
.year
);
532 date
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
533 ? wxDateTime::Mon
: wxDateTime::Sun
);
535 if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
)
537 // We want to offset the calendar if we start on the first..
538 if ( date
.GetDay() == 1 )
540 date
-= wxDateSpan::Week();
547 bool wxCalendarCtrl::IsDateShown(const wxDateTime
& date
) const
549 if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
551 return date
.GetMonth() == m_date
.GetMonth();
559 bool wxCalendarCtrl::IsDateInRange(const wxDateTime
& date
) const
562 // Check if the given date is in the range specified
563 retval
= ( ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : TRUE
)
564 && ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : TRUE
) );
568 bool wxCalendarCtrl::ChangeYear(wxDateTime
* target
) const
572 if ( !(IsDateInRange(*target
)) )
574 if ( target
->GetYear() < m_date
.GetYear() )
576 if ( target
->GetYear() >= GetLowerDateLimit().GetYear() )
578 *target
= GetLowerDateLimit();
588 if ( target
->GetYear() <= GetUpperDateLimit().GetYear() )
590 *target
= GetUpperDateLimit();
607 bool wxCalendarCtrl::ChangeMonth(wxDateTime
* target
) const
611 if ( !(IsDateInRange(*target
)) )
615 if ( target
->GetMonth() < m_date
.GetMonth() )
617 *target
= GetLowerDateLimit();
621 *target
= GetUpperDateLimit();
628 size_t wxCalendarCtrl::GetWeek(const wxDateTime
& date
) const
630 size_t retval
= date
.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
631 ? wxDateTime::Monday_First
632 : wxDateTime::Sunday_First
);
634 if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
636 // we need to offset an extra week if we "start" on the 1st of the month
637 wxDateTime::Tm tm
= date
.GetTm();
639 wxDateTime datetest
= wxDateTime(1, tm
.mon
, tm
.year
);
642 datetest
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
643 ? wxDateTime::Mon
: wxDateTime::Sun
);
645 if ( datetest
.GetDay() == 1 )
654 // ----------------------------------------------------------------------------
656 // ----------------------------------------------------------------------------
658 // this is a composite control and it must arrange its parts each time its
659 // size or position changes: the combobox and spinctrl are along the top of
660 // the available area and the calendar takes up therest of the space
662 // the static controls are supposed to be always smaller than combo/spin so we
663 // always use the latter for size calculations and position the static to take
666 // the constants used for the layout
667 #define VERT_MARGIN 5 // distance between combo and calendar
668 #define HORZ_MARGIN 15 // spin
670 wxSize
wxCalendarCtrl::DoGetBestSize() const
672 // calc the size of the calendar
673 ((wxCalendarCtrl
*)this)->RecalcGeometry(); // const_cast
675 wxCoord width
= 7*m_widthCol
,
676 height
= 7*m_heightRow
+ m_rowOffset
;
678 // the combobox doesn't report its height correctly (it returns the
679 // height including the drop down list) so don't use it
681 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
683 height
+= VERT_MARGIN
+ m_spinYear
->GetBestSize().y
;
687 height
+= VERT_MARGIN
;
690 // if ( GetWindowStyle() & (wxRAISED_BORDER | wxSUNKEN_BORDER) ) // This doesn't work. Default is wxBORDER_DEFAULT (0)
691 if ( !(GetWindowStyle() & wxBORDER_NONE
) )
693 // the border would clip the last line otherwise
698 return wxSize(width
, height
);
701 void wxCalendarCtrl::DoSetSize(int x
, int y
,
702 int width
, int height
,
705 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
708 void wxCalendarCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
713 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
715 wxSize sizeCombo
= m_comboMonth
->GetSize();
716 wxSize sizeStatic
= m_staticMonth
->GetSize();
718 int dy
= (sizeCombo
.y
- sizeStatic
.y
) / 2;
720 m_comboMonth
->Move(x
, y
);
721 m_staticMonth
->SetSize(x
, y
+ dy
, sizeCombo
.x
, sizeStatic
.y
);
723 xDiff
= sizeCombo
.x
+ HORZ_MARGIN
;
725 m_spinYear
->SetSize(x
+ xDiff
, y
, width
- xDiff
, sizeCombo
.y
);
726 m_staticYear
->SetSize(x
+ xDiff
, y
+ dy
, width
- xDiff
, sizeStatic
.y
);
728 wxSize sizeSpin
= m_spinYear
->GetSize();
729 yDiff
= wxMax(sizeSpin
.y
, sizeCombo
.y
) + VERT_MARGIN
;
732 wxControl::DoMoveWindow(x
, y
+ yDiff
, width
, height
- yDiff
);
735 void wxCalendarCtrl::DoGetPosition(int *x
, int *y
) const
737 wxControl::DoGetPosition(x
, y
);
739 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
741 // our real top corner is not in this position
744 *y
-= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
749 void wxCalendarCtrl::DoGetSize(int *width
, int *height
) const
751 wxControl::DoGetSize(width
, height
);
753 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
755 // our real height is bigger
758 *height
+= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
763 void wxCalendarCtrl::RecalcGeometry()
765 if ( m_widthCol
!= 0 )
772 // determine the column width (we assume that the weekday names are always
773 // wider (in any language) than the numbers)
775 wxDateTime::WeekDay wd
;
776 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
779 dc
.GetTextExtent(m_weekdays
[wd
], &width
, &m_heightRow
);
780 if ( width
> m_widthCol
)
786 // leave some margins
790 m_rowOffset
= (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) ? m_heightRow
: 0; // conditional in relation to style
793 // ----------------------------------------------------------------------------
795 // ----------------------------------------------------------------------------
797 void wxCalendarCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
806 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
807 m_date
.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
813 /////////////////////////////////////////////////////////////////////////////////////////
815 if ( (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
817 // draw the sequential month-selector
819 dc
.SetBackgroundMode(wxTRANSPARENT
);
820 dc
.SetTextForeground(*wxBLACK
);
821 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
822 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
823 dc
.DrawRectangle(0, y
, 7*m_widthCol
, m_heightRow
);
825 // Get extent of month-name + year
826 wxCoord monthw
, monthh
;
827 wxString headertext
= m_date
.Format("%B %Y");
828 dc
.GetTextExtent(headertext
, &monthw
, &monthh
);
830 // draw month-name centered above weekdays
831 wxCoord monthx
= ((m_widthCol
* 7) - monthw
) / 2;
832 wxCoord monthy
= ((m_heightRow
- monthh
) / 2) + y
;
833 dc
.DrawText(headertext
, monthx
, monthy
);
835 // calculate the "month-arrows"
836 wxPoint leftarrow
[3];
837 wxPoint rightarrow
[3];
839 int arrowheight
= monthh
/ 2;
841 leftarrow
[0] = wxPoint(0, arrowheight
/ 2);
842 leftarrow
[1] = wxPoint(arrowheight
/ 2, 0);
843 leftarrow
[2] = wxPoint(arrowheight
/ 2, arrowheight
- 1);
845 rightarrow
[0] = wxPoint(0, 0);
846 rightarrow
[1] = wxPoint(arrowheight
/ 2, arrowheight
/ 2);
847 rightarrow
[2] = wxPoint(0, arrowheight
- 1);
849 // draw the "month-arrows"
851 wxCoord arrowy
= (m_heightRow
- arrowheight
) / 2;
852 wxCoord larrowx
= (m_widthCol
- (arrowheight
/ 2)) / 2;
853 wxCoord rarrowx
= ((m_widthCol
- (arrowheight
/ 2)) / 2) + m_widthCol
*6;
854 m_leftArrowRect
= wxRect(0, 0, 0, 0);
855 m_rightArrowRect
= wxRect(0, 0, 0, 0);
857 if ( AllowMonthChange() )
859 wxDateTime ldpm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) - wxDateSpan::Day(); // last day prev month
860 // Check if range permits change
861 if ( IsDateInRange(ldpm
) && ( ( ldpm
.GetYear() == m_date
.GetYear() ) ? TRUE
: AllowYearChange() ) )
863 m_leftArrowRect
= wxRect(larrowx
- 3, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
864 dc
.SetBrush(wxBrush(*wxBLACK
, wxSOLID
));
865 dc
.SetPen(wxPen(*wxBLACK
, 1, wxSOLID
));
866 dc
.DrawPolygon(3, leftarrow
, larrowx
, arrowy
, wxWINDING_RULE
);
867 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
868 dc
.DrawRectangle(m_leftArrowRect
);
870 wxDateTime fdnm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) + wxDateSpan::Month(); // first day next month
871 if ( IsDateInRange(fdnm
) && ( ( fdnm
.GetYear() == m_date
.GetYear() ) ? TRUE
: AllowYearChange() ) )
873 m_rightArrowRect
= wxRect(rarrowx
- 4, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
874 dc
.SetBrush(wxBrush(*wxBLACK
, wxSOLID
));
875 dc
.SetPen(wxPen(*wxBLACK
, 1, wxSOLID
));
876 dc
.DrawPolygon(3, rightarrow
, rarrowx
, arrowy
, wxWINDING_RULE
);
877 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
878 dc
.DrawRectangle(m_rightArrowRect
);
885 /////////////////////////////////////////////////////////////////////////////////////////
887 // first draw the week days
888 if ( IsExposed(0, y
, 7*m_widthCol
, m_heightRow
) )
891 wxLogDebug("painting the header");
894 dc
.SetBackgroundMode(wxTRANSPARENT
);
895 dc
.SetTextForeground(m_colHeaderFg
);
896 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
897 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
898 dc
.DrawRectangle(0, y
, 7*m_widthCol
, m_heightRow
);
900 bool startOnMonday
= (GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
901 for ( size_t wd
= 0; wd
< 7; wd
++ )
905 n
= wd
== 6 ? 0 : wd
+ 1;
909 dc
.GetTextExtent(m_weekdays
[n
], &dayw
, &dayh
);
910 // dc.DrawText(m_weekdays[n], wd*m_widthCol + 1, y);
911 dc
.DrawText(m_weekdays
[n
], (wd
*m_widthCol
) + ((m_widthCol
- dayw
) / 2), y
); // center the day-name
915 // then the calendar itself
916 dc
.SetTextForeground(*wxBLACK
);
917 //dc.SetFont(*wxNORMAL_FONT);
920 wxDateTime date
= GetStartDate();
923 wxLogDebug("starting calendar from %s\n",
924 date
.Format("%a %d-%m-%Y %H:%M:%S").c_str());
927 dc
.SetBackgroundMode(wxSOLID
);
928 for ( size_t nWeek
= 1; nWeek
<= 6; nWeek
++, y
+= m_heightRow
)
930 // if the update region doesn't intersect this row, don't paint it
931 if ( !IsExposed(0, y
, 7*m_widthCol
, m_heightRow
- 1) )
933 date
+= wxDateSpan::Week();
939 wxLogDebug("painting week %d at y = %d\n", nWeek
, y
);
942 for ( size_t wd
= 0; wd
< 7; wd
++ )
944 if ( IsDateShown(date
) )
946 // don't use wxDate::Format() which prepends 0s
947 unsigned int day
= date
.GetDay();
948 wxString dayStr
= wxString::Format(_T("%u"), day
);
950 dc
.GetTextExtent(dayStr
, &width
, (wxCoord
*)NULL
);
952 bool changedColours
= FALSE
,
956 wxCalendarDateAttr
*attr
= NULL
;
958 if ( date
.GetMonth() != m_date
.GetMonth() || !IsDateInRange(date
) )
960 // surrounding week or out-of-range
962 dc
.SetTextForeground(*wxLIGHT_GREY
);
963 changedColours
= TRUE
;
967 isSel
= date
.IsSameDate(m_date
);
968 attr
= m_attrs
[day
- 1];
972 dc
.SetTextForeground(m_colHighlightFg
);
973 dc
.SetTextBackground(m_colHighlightBg
);
975 changedColours
= TRUE
;
979 wxColour colFg
, colBg
;
981 if ( attr
->IsHoliday() )
983 colFg
= m_colHolidayFg
;
984 colBg
= m_colHolidayBg
;
988 colFg
= attr
->GetTextColour();
989 colBg
= attr
->GetBackgroundColour();
994 dc
.SetTextForeground(colFg
);
995 changedColours
= TRUE
;
1000 dc
.SetTextBackground(colBg
);
1001 changedColours
= TRUE
;
1004 if ( attr
->HasFont() )
1006 dc
.SetFont(attr
->GetFont());
1012 wxCoord x
= wd
*m_widthCol
+ (m_widthCol
- width
) / 2;
1013 dc
.DrawText(dayStr
, x
, y
+ 1);
1015 if ( !isSel
&& attr
&& attr
->HasBorder() )
1018 if ( attr
->HasBorderColour() )
1020 colBorder
= attr
->GetBorderColour();
1024 colBorder
= m_foregroundColour
;
1027 wxPen
pen(colBorder
, 1, wxSOLID
);
1029 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1031 switch ( attr
->GetBorder() )
1033 case wxCAL_BORDER_SQUARE
:
1034 dc
.DrawRectangle(x
- 2, y
,
1035 width
+ 4, m_heightRow
);
1038 case wxCAL_BORDER_ROUND
:
1039 dc
.DrawEllipse(x
- 2, y
,
1040 width
+ 4, m_heightRow
);
1044 wxFAIL_MSG(_T("unknown border type"));
1048 if ( changedColours
)
1050 dc
.SetTextForeground(m_foregroundColour
);
1051 dc
.SetTextBackground(m_backgroundColour
);
1059 //else: just don't draw it
1061 date
+= wxDateSpan::Day();
1065 // Greying out out-of-range background
1066 bool showSurrounding
= (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) != 0;
1068 date
= ( showSurrounding
) ? GetStartDate() : wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear());
1069 if ( !IsDateInRange(date
) )
1071 wxDateTime firstOOR
= GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1073 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1074 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1076 HighlightRange(&dc
, date
, firstOOR
, wxTRANSPARENT_PEN
, &oorbrush
);
1079 date
= ( showSurrounding
) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear());
1080 if ( !IsDateInRange(date
) )
1082 wxDateTime firstOOR
= GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1084 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1085 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1087 HighlightRange(&dc
, firstOOR
, date
, wxTRANSPARENT_PEN
, &oorbrush
);
1091 wxLogDebug("+++ finished painting");
1095 void wxCalendarCtrl::RefreshDate(const wxDateTime
& date
)
1101 // always refresh the whole row at once because our OnPaint() will draw
1102 // the whole row anyhow - and this allows the small optimisation in
1103 // OnClick() below to work
1106 rect
.y
= (m_heightRow
* GetWeek(date
)) + m_rowOffset
;
1108 rect
.width
= 7*m_widthCol
;
1109 rect
.height
= m_heightRow
;
1112 // VZ: for some reason, the selected date seems to occupy more space under
1113 // MSW - this is probably some bug in the font size calculations, but I
1114 // don't know where exactly. This fix is ugly and leads to more
1115 // refreshes than really needed, but without it the selected days
1116 // leaves even more ugly underscores on screen.
1121 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
1124 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1127 Refresh(TRUE
, &rect
);
1130 void wxCalendarCtrl::HighlightRange(wxPaintDC
* pDC
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, wxPen
* pPen
, wxBrush
* pBrush
)
1132 // Highlights the given range using pen and brush
1133 // Does nothing if todate < fromdate
1137 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate
.Format("%d %m %Y"), todate
.Format("%d %m %Y"));
1140 if ( todate
>= fromdate
)
1147 // implicit: both dates must be currently shown - checked by GetDateCoord
1148 if ( GetDateCoord(fromdate
, &fd
, &fw
) && GetDateCoord(todate
, &td
, &tw
) )
1151 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd
, fw
, td
, tw
);
1153 if ( ( (tw
- fw
) == 1 ) && ( td
< fd
) )
1155 // special case: interval 7 days or less not in same week
1156 // split in two seperate intervals
1157 wxDateTime tfd
= fromdate
+ wxDateSpan::Days(7-fd
);
1158 wxDateTime ftd
= tfd
+ wxDateSpan::Day();
1160 wxLogDebug("Highlight: Seperate segments");
1163 HighlightRange(pDC
, fromdate
, tfd
, pPen
, pBrush
);
1164 HighlightRange(pDC
, ftd
, todate
, pPen
, pBrush
);
1169 wxPoint corners
[8]; // potentially 8 corners in polygon
1173 // simple case: same week
1175 corners
[0] = wxPoint((fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
);
1176 corners
[1] = wxPoint((fd
- 1) * m_widthCol
, ((fw
+ 1 ) * m_heightRow
) + m_rowOffset
);
1177 corners
[2] = wxPoint(td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
);
1178 corners
[3] = wxPoint(td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
);
1183 // "complex" polygon
1184 corners
[cidx
] = wxPoint((fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1188 corners
[cidx
] = wxPoint((fd
- 1) * m_widthCol
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1189 corners
[cidx
] = wxPoint(0, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1192 corners
[cidx
] = wxPoint(0, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1193 corners
[cidx
] = wxPoint(td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1197 corners
[cidx
] = wxPoint(td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1198 corners
[cidx
] = wxPoint(7 * m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1201 corners
[cidx
] = wxPoint(7 * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1207 pDC
->SetBrush(*pBrush
);
1209 pDC
->DrawPolygon(numpoints
, corners
);
1215 wxLogDebug("--- HighlightRange ---");
1219 bool wxCalendarCtrl::GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const
1224 wxLogDebug("+++ GetDateCoord: (%s) +++", date
.Format("%d %m %Y"));
1227 if ( IsDateShown(date
) )
1229 bool startOnMonday
= ( GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1232 *day
= date
.GetWeekDay();
1234 if ( *day
== 0 ) // sunday
1236 *day
= ( startOnMonday
) ? 7 : 1;
1240 day
+= ( startOnMonday
) ? 0 : 1;
1243 int targetmonth
= date
.GetMonth() + (12 * date
.GetYear());
1244 int thismonth
= m_date
.GetMonth() + (12 * m_date
.GetYear());
1247 if ( targetmonth
== thismonth
)
1249 *week
= GetWeek(date
);
1253 if ( targetmonth
< thismonth
)
1255 *week
= 1; // trivial
1257 else // targetmonth > thismonth
1263 // get the datecoord of the last day in the month currently shown
1265 wxLogDebug(" +++ LDOM +++");
1267 GetDateCoord(ldcm
.SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear()), &lastday
, &lastweek
);
1269 wxLogDebug(" --- LDOM ---");
1272 wxTimeSpan span
= date
- ldcm
;
1274 int daysfromlast
= span
.GetDays();
1276 wxLogDebug("daysfromlast: %i", daysfromlast
);
1278 if ( daysfromlast
+ lastday
> 7 ) // past week boundary
1280 int wholeweeks
= (daysfromlast
/ 7);
1281 *week
= wholeweeks
+ lastweek
;
1282 if ( (daysfromlast
- (7 * wholeweeks
) + lastday
) > 7 )
1302 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date
.Format("%d %m %Y"), *day
, *week
);
1308 // ----------------------------------------------------------------------------
1310 // ----------------------------------------------------------------------------
1312 void wxCalendarCtrl::OnDClick(wxMouseEvent
& event
)
1314 if ( HitTest(event
.GetPosition()) != wxCAL_HITTEST_DAY
)
1320 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1324 void wxCalendarCtrl::OnClick(wxMouseEvent
& event
)
1327 wxDateTime::WeekDay wday
;
1328 switch ( HitTest(event
.GetPosition(), &date
, &wday
) )
1330 case wxCAL_HITTEST_DAY
:
1331 if ( IsDateInRange(date
) )
1335 GenerateEvents(wxEVT_CALENDAR_DAY_CHANGED
,
1336 wxEVT_CALENDAR_SEL_CHANGED
);
1340 case wxCAL_HITTEST_HEADER
:
1342 wxCalendarEvent
event(this, wxEVT_CALENDAR_WEEKDAY_CLICKED
);
1343 event
.m_wday
= wday
;
1344 (void)GetEventHandler()->ProcessEvent(event
);
1348 case wxCAL_HITTEST_DECMONTH
:
1349 case wxCAL_HITTEST_INCMONTH
:
1350 case wxCAL_HITTEST_SURROUNDING_WEEK
:
1351 SetDateAndNotify(date
); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1355 wxFAIL_MSG(_T("unknown hittest code"));
1358 case wxCAL_HITTEST_NOWHERE
:
1364 wxCalendarHitTestResult
wxCalendarCtrl::HitTest(const wxPoint
& pos
,
1366 wxDateTime::WeekDay
*wd
)
1372 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1373 if ( (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
1377 // we need to find out if the hit is on left arrow, on month or on right arrow
1379 if ( wxRegion(m_leftArrowRect
).Contains(pos
) == wxInRegion
)
1383 if ( IsDateInRange(m_date
- wxDateSpan::Month()) )
1385 *date
= m_date
- wxDateSpan::Month();
1389 *date
= GetLowerDateLimit();
1393 return wxCAL_HITTEST_DECMONTH
;
1396 if ( wxRegion(m_rightArrowRect
).Contains(pos
) == wxInRegion
)
1400 if ( IsDateInRange(m_date
+ wxDateSpan::Month()) )
1402 *date
= m_date
+ wxDateSpan::Month();
1406 *date
= GetUpperDateLimit();
1410 return wxCAL_HITTEST_INCMONTH
;
1415 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1417 int wday
= pos
.x
/ m_widthCol
;
1418 // if ( y < m_heightRow )
1419 if ( y
< (m_heightRow
+ m_rowOffset
) )
1421 if ( y
> m_rowOffset
)
1425 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST
)
1427 wday
= wday
== 6 ? 0 : wday
+ 1;
1430 *wd
= (wxDateTime::WeekDay
)wday
;
1433 return wxCAL_HITTEST_HEADER
;
1437 return wxCAL_HITTEST_NOWHERE
;
1441 // int week = (y - m_heightRow) / m_heightRow;
1442 int week
= (y
- (m_heightRow
+ m_rowOffset
)) / m_heightRow
;
1443 if ( week
>= 6 || wday
>= 7 )
1445 return wxCAL_HITTEST_NOWHERE
;
1448 wxDateTime dt
= GetStartDate() + wxDateSpan::Days(7*week
+ wday
);
1450 if ( IsDateShown(dt
) )
1455 if ( dt
.GetMonth() == m_date
.GetMonth() )
1458 return wxCAL_HITTEST_DAY
;
1462 return wxCAL_HITTEST_SURROUNDING_WEEK
;
1467 return wxCAL_HITTEST_NOWHERE
;
1471 // ----------------------------------------------------------------------------
1472 // subcontrols events handling
1473 // ----------------------------------------------------------------------------
1475 void wxCalendarCtrl::OnMonthChange(wxCommandEvent
& event
)
1477 wxDateTime::Tm tm
= m_date
.GetTm();
1479 wxDateTime::Month mon
= (wxDateTime::Month
)event
.GetInt();
1480 if ( tm
.mday
> wxDateTime::GetNumberOfDays(mon
, tm
.year
) )
1482 tm
.mday
= wxDateTime::GetNumberOfDays(mon
, tm
.year
);
1485 wxDateTime target
= wxDateTime(tm
.mday
, mon
, tm
.year
);
1487 ChangeMonth(&target
);
1488 SetDateAndNotify(target
);
1491 void wxCalendarCtrl::OnYearChange(wxSpinEvent
& event
)
1493 wxDateTime::Tm tm
= m_date
.GetTm();
1495 int year
= (int)event
.GetInt();
1496 if ( tm
.mday
> wxDateTime::GetNumberOfDays(tm
.mon
, year
) )
1498 tm
.mday
= wxDateTime::GetNumberOfDays(tm
.mon
, year
);
1501 wxDateTime target
= wxDateTime(tm
.mday
, tm
.mon
, year
);
1503 if ( ChangeYear(&target
) )
1505 SetDateAndNotify(target
);
1509 // In this case we don't want to change the date. That would put us
1510 // inside the same year but a strange number of months forward/back..
1511 m_spinYear
->SetValue(target
.GetYear());
1516 // ----------------------------------------------------------------------------
1517 // keyboard interface
1518 // ----------------------------------------------------------------------------
1520 void wxCalendarCtrl::OnChar(wxKeyEvent
& event
)
1523 switch ( event
.KeyCode() )
1527 target
= m_date
+ wxDateSpan::Year();
1528 if ( ChangeYear(&target
) )
1530 SetDateAndNotify(target
);
1536 target
= m_date
- wxDateSpan::Year();
1537 if ( ChangeYear(&target
) )
1539 SetDateAndNotify(target
);
1544 target
= m_date
- wxDateSpan::Month();
1545 ChangeMonth(&target
);
1546 SetDateAndNotify(target
); // always
1550 target
= m_date
+ wxDateSpan::Month();
1551 ChangeMonth(&target
);
1552 SetDateAndNotify(target
); // always
1556 if ( event
.ControlDown() )
1558 target
= wxDateTime(m_date
).SetToNextWeekDay(
1559 GetWindowStyle() & wxCAL_MONDAY_FIRST
1560 ? wxDateTime::Sun
: wxDateTime::Sat
);
1561 if ( !IsDateInRange(target
) )
1563 target
= GetUpperDateLimit();
1565 SetDateAndNotify(target
);
1568 SetDateAndNotify(m_date
+ wxDateSpan::Day());
1572 if ( event
.ControlDown() )
1574 target
= wxDateTime(m_date
).SetToPrevWeekDay(
1575 GetWindowStyle() & wxCAL_MONDAY_FIRST
1576 ? wxDateTime::Mon
: wxDateTime::Sun
);
1577 if ( !IsDateInRange(target
) )
1579 target
= GetLowerDateLimit();
1581 SetDateAndNotify(target
);
1584 SetDateAndNotify(m_date
- wxDateSpan::Day());
1588 SetDateAndNotify(m_date
- wxDateSpan::Week());
1592 SetDateAndNotify(m_date
+ wxDateSpan::Week());
1596 if ( event
.ControlDown() )
1597 SetDateAndNotify(wxDateTime::Today());
1599 SetDateAndNotify(wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear()));
1603 SetDateAndNotify(wxDateTime(m_date
).SetToLastMonthDay());
1607 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1615 // ----------------------------------------------------------------------------
1616 // holidays handling
1617 // ----------------------------------------------------------------------------
1619 void wxCalendarCtrl::EnableHolidayDisplay(bool display
)
1621 long style
= GetWindowStyle();
1623 style
|= wxCAL_SHOW_HOLIDAYS
;
1625 style
&= ~wxCAL_SHOW_HOLIDAYS
;
1627 SetWindowStyle(style
);
1632 ResetHolidayAttrs();
1637 void wxCalendarCtrl::SetHolidayAttrs()
1639 if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS
)
1641 ResetHolidayAttrs();
1643 wxDateTime::Tm tm
= m_date
.GetTm();
1644 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
1645 dtEnd
= dtStart
.GetLastMonthDay();
1647 wxDateTimeArray hol
;
1648 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
1650 size_t count
= hol
.GetCount();
1651 for ( size_t n
= 0; n
< count
; n
++ )
1653 SetHoliday(hol
[n
].GetDay());
1658 void wxCalendarCtrl::SetHoliday(size_t day
)
1660 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in SetHoliday") );
1662 wxCalendarDateAttr
*attr
= GetAttr(day
);
1665 attr
= new wxCalendarDateAttr
;
1668 attr
->SetHoliday(TRUE
);
1670 // can't use SetAttr() because it would delete this pointer
1671 m_attrs
[day
- 1] = attr
;
1674 void wxCalendarCtrl::ResetHolidayAttrs()
1676 for ( size_t day
= 0; day
< 31; day
++ )
1680 m_attrs
[day
]->SetHoliday(FALSE
);
1685 // ----------------------------------------------------------------------------
1687 // ----------------------------------------------------------------------------
1689 void wxCalendarEvent::Init()
1691 m_wday
= wxDateTime::Inv_WeekDay
;
1694 wxCalendarEvent::wxCalendarEvent(wxCalendarCtrl
*cal
, wxEventType type
)
1695 : wxCommandEvent(type
, cal
->GetId())
1697 m_date
= cal
->GetDate();
1698 SetEventObject(cal
);
1701 #endif // wxUSE_CALENDARCTRL