1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/calctrl.cpp
3 // Purpose: implementation fo the generic wxCalendarCtrl
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "calctrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/dcclient.h"
33 #include "wx/settings.h"
35 #include "wx/combobox.h"
36 #include "wx/stattext.h"
37 #include "wx/textctrl.h"
40 #if wxUSE_CALENDARCTRL
42 #include "wx/spinctrl.h"
44 #include "wx/calctrl.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 class wxMonthComboBox : public wxComboBox
55 wxMonthComboBox(wxCalendarCtrl *cal);
57 void OnMonthChange(wxCommandEvent& event) { m_cal->OnMonthChange(event); }
60 wxCalendarCtrl *m_cal;
63 DECLARE_NO_COPY_CLASS(wxMonthComboBox)
66 class wxYearSpinCtrl : public wxSpinCtrl
69 wxYearSpinCtrl(wxCalendarCtrl *cal);
71 void OnYearTextChange(wxCommandEvent& event) { m_cal->OnYearChange(event); }
72 void OnYearChange(wxSpinEvent& event) { m_cal->OnYearChange(event); }
75 wxCalendarCtrl *m_cal;
78 DECLARE_NO_COPY_CLASS(wxYearSpinCtrl)
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 BEGIN_EVENT_TABLE(wxCalendarCtrl, wxControl)
86 EVT_PAINT(wxCalendarCtrl::OnPaint)
88 EVT_CHAR(wxCalendarCtrl::OnChar)
90 EVT_LEFT_DOWN(wxCalendarCtrl::OnClick)
91 EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick)
94 BEGIN_EVENT_TABLE(wxMonthComboBox, wxComboBox)
95 EVT_COMBOBOX(-1, wxMonthComboBox::OnMonthChange)
98 BEGIN_EVENT_TABLE(wxYearSpinCtrl, wxSpinCtrl)
99 EVT_TEXT(-1, wxYearSpinCtrl::OnYearTextChange)
100 EVT_SPINCTRL(-1, wxYearSpinCtrl::OnYearChange)
103 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl, wxControl)
104 IMPLEMENT_DYNAMIC_CLASS(wxCalendarEvent, wxCommandEvent)
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_SEL_CHANGED)
111 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DAY_CHANGED)
112 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_MONTH_CHANGED)
113 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_YEAR_CHANGED)
114 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DOUBLECLICKED)
115 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_WEEKDAY_CLICKED)
117 // ============================================================================
119 // ============================================================================
121 // ----------------------------------------------------------------------------
122 // wxMonthComboBox and wxYearSpinCtrl
123 // ----------------------------------------------------------------------------
125 wxMonthComboBox::wxMonthComboBox(wxCalendarCtrl *cal)
126 : wxComboBox(cal->GetParent(), -1,
131 wxCB_READONLY | wxCLIP_SIBLINGS)
136 for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
138 Append(wxDateTime::GetMonthName(m));
141 SetSelection(m_cal->GetDate().GetMonth());
142 SetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH|wxSIZE_AUTO_HEIGHT);
145 wxYearSpinCtrl::wxYearSpinCtrl(wxCalendarCtrl *cal)
146 : wxSpinCtrl(cal->GetParent(), -1,
147 cal->GetDate().Format(_T("%Y")),
150 wxSP_ARROW_KEYS | wxCLIP_SIBLINGS,
151 -4300, 10000, cal->GetDate().GetYear())
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 wxCalendarCtrl::wxCalendarCtrl(wxWindow *parent,
162 const wxDateTime& date,
166 const wxString& name)
170 (void)Create(parent, id, date, pos, size, style, name);
173 void wxCalendarCtrl::Init()
178 m_userChangedYear = FALSE;
183 wxDateTime::WeekDay wd;
184 for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
186 m_weekdays[wd] = wxDateTime::GetWeekDayName(wd, wxDateTime::Name_Abbr);
189 for ( size_t n = 0; n < WXSIZEOF(m_attrs); n++ )
194 m_colHighlightFg = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
195 m_colHighlightBg = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
197 m_colHolidayFg = *wxRED;
198 // don't set m_colHolidayBg - by default, same as our bg colour
200 m_colHeaderFg = *wxBLUE;
201 m_colHeaderBg = *wxLIGHT_GREY;
204 bool wxCalendarCtrl::Create(wxWindow *parent,
206 const wxDateTime& date,
210 const wxString& name)
212 if ( !wxControl::Create(parent, id, pos, size,
213 style | wxCLIP_CHILDREN | wxWANTS_CHARS,
214 wxDefaultValidator, name) )
219 // needed to get the arrow keys normally used for the dialog navigation
220 SetWindowStyle(style | wxWANTS_CHARS);
222 m_date = date.IsValid() ? date : wxDateTime::Today();
224 m_lowdate = wxDefaultDateTime;
225 m_highdate = wxDefaultDateTime;
227 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
229 m_spinYear = new wxYearSpinCtrl(this);
230 m_staticYear = new wxStaticText(GetParent(), -1, m_date.Format(_T("%Y")),
231 wxDefaultPosition, wxDefaultSize,
234 m_comboMonth = new wxMonthComboBox(this);
235 m_staticMonth = new wxStaticText(GetParent(), -1, m_date.Format(_T("%B")),
236 wxDefaultPosition, wxDefaultSize,
240 ShowCurrentControls();
243 if ( size.x == -1 || size.y == -1 )
245 sizeReal = DoGetBestSize();
256 // we need to set the position as well because the main control position
257 // is not the same as the one specified in pos if we have the controls
259 SetSize(pos.x, pos.y, sizeReal.x, sizeReal.y);
261 SetBackgroundColour(*wxWHITE);
262 SetFont(*wxSWISS_FONT);
269 wxCalendarCtrl::~wxCalendarCtrl()
271 for ( size_t n = 0; n < WXSIZEOF(m_attrs); n++ )
277 // ----------------------------------------------------------------------------
278 // forward wxWin functions to subcontrols
279 // ----------------------------------------------------------------------------
281 bool wxCalendarCtrl::Destroy()
284 m_staticYear->Destroy();
286 m_spinYear->Destroy();
288 m_comboMonth->Destroy();
290 m_staticMonth->Destroy();
295 m_staticMonth = NULL;
297 return wxControl::Destroy();
300 bool wxCalendarCtrl::Show(bool show)
302 if ( !wxControl::Show(show) )
307 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) )
309 if ( GetMonthControl() )
311 GetMonthControl()->Show(show);
312 GetYearControl()->Show(show);
319 bool wxCalendarCtrl::Enable(bool enable)
321 if ( !wxControl::Enable(enable) )
326 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) )
328 GetMonthControl()->Enable(enable);
329 GetYearControl()->Enable(enable);
335 // ----------------------------------------------------------------------------
336 // enable/disable month/year controls
337 // ----------------------------------------------------------------------------
339 void wxCalendarCtrl::ShowCurrentControls()
341 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
343 if ( AllowMonthChange() )
345 m_comboMonth->Show();
346 m_staticMonth->Hide();
348 if ( AllowYearChange() )
351 m_staticYear->Hide();
359 m_comboMonth->Hide();
360 m_staticMonth->Show();
363 // year change not allowed here
365 m_staticYear->Show();
369 wxControl *wxCalendarCtrl::GetMonthControl() const
371 return AllowMonthChange() ? (wxControl *)m_comboMonth : (wxControl *)m_staticMonth;
374 wxControl *wxCalendarCtrl::GetYearControl() const
376 return AllowYearChange() ? (wxControl *)m_spinYear : (wxControl *)m_staticYear;
379 void wxCalendarCtrl::EnableYearChange(bool enable)
381 if ( enable != AllowYearChange() )
383 long style = GetWindowStyle();
385 style &= ~wxCAL_NO_YEAR_CHANGE;
387 style |= wxCAL_NO_YEAR_CHANGE;
388 SetWindowStyle(style);
390 ShowCurrentControls();
391 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION )
398 void wxCalendarCtrl::EnableMonthChange(bool enable)
400 if ( enable != AllowMonthChange() )
402 long style = GetWindowStyle();
404 style &= ~wxCAL_NO_MONTH_CHANGE;
406 style |= wxCAL_NO_MONTH_CHANGE;
407 SetWindowStyle(style);
409 ShowCurrentControls();
410 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION )
417 // ----------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
421 bool wxCalendarCtrl::SetDate(const wxDateTime& date)
425 bool sameMonth = m_date.GetMonth() == date.GetMonth(),
426 sameYear = m_date.GetYear() == date.GetYear();
428 if ( IsDateInRange(date) )
430 if ( sameMonth && sameYear )
432 // just change the day
437 if ( AllowMonthChange() && (AllowYearChange() || sameYear) )
442 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) )
444 // update the controls
445 m_comboMonth->SetSelection(m_date.GetMonth());
447 if ( AllowYearChange() )
449 if ( !m_userChangedYear )
450 m_spinYear->SetValue(m_date.Format(_T("%Y")));
451 else // don't overwrite what the user typed in
452 m_userChangedYear = FALSE;
456 // as the month changed, holidays did too
459 // update the calendar
473 void wxCalendarCtrl::ChangeDay(const wxDateTime& date)
475 if ( m_date != date )
477 // we need to refresh the row containing the old date and the one
478 // containing the new one
479 wxDateTime dateOld = m_date;
482 RefreshDate(dateOld);
484 // if the date is in the same row, it was already drawn correctly
485 if ( GetWeek(m_date) != GetWeek(dateOld) )
492 void wxCalendarCtrl::SetDateAndNotify(const wxDateTime& date)
494 wxDateTime::Tm tm1 = m_date.GetTm(),
498 if ( tm1.year != tm2.year )
499 type = wxEVT_CALENDAR_YEAR_CHANGED;
500 else if ( tm1.mon != tm2.mon )
501 type = wxEVT_CALENDAR_MONTH_CHANGED;
502 else if ( tm1.mday != tm2.mday )
503 type = wxEVT_CALENDAR_DAY_CHANGED;
509 GenerateEvents(type, wxEVT_CALENDAR_SEL_CHANGED);
513 // ----------------------------------------------------------------------------
515 // ----------------------------------------------------------------------------
517 bool wxCalendarCtrl::SetLowerDateLimit(const wxDateTime& date /* = wxDefaultDateTime */)
521 if ( !(date.IsValid()) || ( ( m_highdate.IsValid() ) ? ( date <= m_highdate ) : TRUE ) )
533 bool wxCalendarCtrl::SetUpperDateLimit(const wxDateTime& date /* = wxDefaultDateTime */)
537 if ( !(date.IsValid()) || ( ( m_lowdate.IsValid() ) ? ( date >= m_lowdate ) : TRUE ) )
549 bool wxCalendarCtrl::SetDateRange(const wxDateTime& lowerdate /* = wxDefaultDateTime */, const wxDateTime& upperdate /* = wxDefaultDateTime */)
554 ( !( lowerdate.IsValid() ) || ( ( upperdate.IsValid() ) ? ( lowerdate <= upperdate ) : TRUE ) ) &&
555 ( !( upperdate.IsValid() ) || ( ( lowerdate.IsValid() ) ? ( upperdate >= lowerdate ) : TRUE ) ) )
557 m_lowdate = lowerdate;
558 m_highdate = upperdate;
568 // ----------------------------------------------------------------------------
570 // ----------------------------------------------------------------------------
572 wxDateTime wxCalendarCtrl::GetStartDate() const
574 wxDateTime::Tm tm = m_date.GetTm();
576 wxDateTime date = wxDateTime(1, tm.mon, tm.year);
579 date.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
580 ? wxDateTime::Mon : wxDateTime::Sun);
582 if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS )
584 // We want to offset the calendar if we start on the first..
585 if ( date.GetDay() == 1 )
587 date -= wxDateSpan::Week();
594 bool wxCalendarCtrl::IsDateShown(const wxDateTime& date) const
596 if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS) )
598 return date.GetMonth() == m_date.GetMonth();
606 bool wxCalendarCtrl::IsDateInRange(const wxDateTime& date) const
609 // Check if the given date is in the range specified
610 retval = ( ( ( m_lowdate.IsValid() ) ? ( date >= m_lowdate ) : TRUE )
611 && ( ( m_highdate.IsValid() ) ? ( date <= m_highdate ) : TRUE ) );
615 bool wxCalendarCtrl::ChangeYear(wxDateTime* target) const
619 if ( !(IsDateInRange(*target)) )
621 if ( target->GetYear() < m_date.GetYear() )
623 if ( target->GetYear() >= GetLowerDateLimit().GetYear() )
625 *target = GetLowerDateLimit();
635 if ( target->GetYear() <= GetUpperDateLimit().GetYear() )
637 *target = GetUpperDateLimit();
654 bool wxCalendarCtrl::ChangeMonth(wxDateTime* target) const
658 if ( !(IsDateInRange(*target)) )
662 if ( target->GetMonth() < m_date.GetMonth() )
664 *target = GetLowerDateLimit();
668 *target = GetUpperDateLimit();
675 size_t wxCalendarCtrl::GetWeek(const wxDateTime& date) const
677 size_t retval = date.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
678 ? wxDateTime::Monday_First
679 : wxDateTime::Sunday_First);
681 if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS) )
683 // we need to offset an extra week if we "start" on the 1st of the month
684 wxDateTime::Tm tm = date.GetTm();
686 wxDateTime datetest = wxDateTime(1, tm.mon, tm.year);
689 datetest.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
690 ? wxDateTime::Mon : wxDateTime::Sun);
692 if ( datetest.GetDay() == 1 )
701 // ----------------------------------------------------------------------------
703 // ----------------------------------------------------------------------------
705 // this is a composite control and it must arrange its parts each time its
706 // size or position changes: the combobox and spinctrl are along the top of
707 // the available area and the calendar takes up therest of the space
709 // the static controls are supposed to be always smaller than combo/spin so we
710 // always use the latter for size calculations and position the static to take
713 // the constants used for the layout
714 #define VERT_MARGIN 5 // distance between combo and calendar
716 #define HORZ_MARGIN 5 // spin
718 #define HORZ_MARGIN 15 // spin
720 wxSize wxCalendarCtrl::DoGetBestSize() const
722 // calc the size of the calendar
723 ((wxCalendarCtrl *)this)->RecalcGeometry(); // const_cast
725 wxCoord width = 7*m_widthCol,
726 height = 7*m_heightRow + m_rowOffset + VERT_MARGIN;
728 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
730 // the combobox doesn't report its height correctly (it returns the
731 // height including the drop down list) so don't use it
732 height += m_spinYear->GetBestSize().y;
735 if ( !HasFlag(wxBORDER_NONE) )
737 // the border would clip the last line otherwise
742 return wxSize(width, height);
745 void wxCalendarCtrl::DoSetSize(int x, int y,
746 int width, int height,
749 wxControl::DoSetSize(x, y, width, height, sizeFlags);
752 void wxCalendarCtrl::DoMoveWindow(int x, int y, int width, int height)
756 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
758 wxSize sizeCombo = m_comboMonth->GetSize();
759 wxSize sizeStatic = m_staticMonth->GetSize();
761 int dy = (sizeCombo.y - sizeStatic.y) / 2;
763 m_comboMonth->Move(x, y);
764 m_staticMonth->SetSize(x, y + dy, sizeCombo.x, sizeStatic.y);
766 int xDiff = sizeCombo.x + HORZ_MARGIN;
768 m_spinYear->SetSize(x + xDiff, y, width - xDiff, sizeCombo.y);
769 m_staticYear->SetSize(x + xDiff, y + dy, width - xDiff, sizeStatic.y);
771 wxSize sizeSpin = m_spinYear->GetSize();
772 yDiff = wxMax(sizeSpin.y, sizeCombo.y) + VERT_MARGIN;
774 else // no controls on the top
779 wxControl::DoMoveWindow(x, y + yDiff, width, height - yDiff);
782 void wxCalendarCtrl::DoGetPosition(int *x, int *y) const
784 wxControl::DoGetPosition(x, y);
786 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) )
788 // our real top corner is not in this position
791 *y -= GetMonthControl()->GetSize().y + VERT_MARGIN;
796 void wxCalendarCtrl::DoGetSize(int *width, int *height) const
798 wxControl::DoGetSize(width, height);
800 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) )
802 // our real height is bigger
803 if ( height && GetMonthControl())
805 *height += GetMonthControl()->GetSize().y + VERT_MARGIN;
810 void wxCalendarCtrl::RecalcGeometry()
812 if ( m_widthCol != 0 )
819 // determine the column width (we assume that the weekday names are always
820 // wider (in any language) than the numbers)
822 wxDateTime::WeekDay wd;
823 for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
826 dc.GetTextExtent(m_weekdays[wd], &width, &m_heightRow);
827 if ( width > m_widthCol )
833 // leave some margins
837 m_rowOffset = (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) ? m_heightRow : 0; // conditional in relation to style
840 // ----------------------------------------------------------------------------
842 // ----------------------------------------------------------------------------
844 void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
853 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
854 m_date.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
860 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
862 // draw the sequential month-selector
864 dc.SetBackgroundMode(wxTRANSPARENT);
865 dc.SetTextForeground(*wxBLACK);
866 dc.SetBrush(wxBrush(m_colHeaderBg, wxSOLID));
867 dc.SetPen(wxPen(m_colHeaderBg, 1, wxSOLID));
868 dc.DrawRectangle(0, y, 7*m_widthCol, m_heightRow);
870 // Get extent of month-name + year
871 wxCoord monthw, monthh;
872 wxString headertext = m_date.Format(wxT("%B %Y"));
873 dc.GetTextExtent(headertext, &monthw, &monthh);
875 // draw month-name centered above weekdays
876 wxCoord monthx = ((m_widthCol * 7) - monthw) / 2;
877 wxCoord monthy = ((m_heightRow - monthh) / 2) + y;
878 dc.DrawText(headertext, monthx, monthy);
880 // calculate the "month-arrows"
881 wxPoint leftarrow[3];
882 wxPoint rightarrow[3];
884 int arrowheight = monthh / 2;
886 leftarrow[0] = wxPoint(0, arrowheight / 2);
887 leftarrow[1] = wxPoint(arrowheight / 2, 0);
888 leftarrow[2] = wxPoint(arrowheight / 2, arrowheight - 1);
890 rightarrow[0] = wxPoint(0, 0);
891 rightarrow[1] = wxPoint(arrowheight / 2, arrowheight / 2);
892 rightarrow[2] = wxPoint(0, arrowheight - 1);
894 // draw the "month-arrows"
896 wxCoord arrowy = (m_heightRow - arrowheight) / 2;
897 wxCoord larrowx = (m_widthCol - (arrowheight / 2)) / 2;
898 wxCoord rarrowx = ((m_widthCol - (arrowheight / 2)) / 2) + m_widthCol*6;
899 m_leftArrowRect = wxRect(0, 0, 0, 0);
900 m_rightArrowRect = wxRect(0, 0, 0, 0);
902 if ( AllowMonthChange() )
904 wxDateTime ldpm = wxDateTime(1,m_date.GetMonth(), m_date.GetYear()) - wxDateSpan::Day(); // last day prev month
905 // Check if range permits change
906 if ( IsDateInRange(ldpm) && ( ( ldpm.GetYear() == m_date.GetYear() ) ? TRUE : AllowYearChange() ) )
908 m_leftArrowRect = wxRect(larrowx - 3, arrowy - 3, (arrowheight / 2) + 8, (arrowheight + 6));
909 dc.SetBrush(wxBrush(*wxBLACK, wxSOLID));
910 dc.SetPen(wxPen(*wxBLACK, 1, wxSOLID));
911 dc.DrawPolygon(3, leftarrow, larrowx , arrowy, wxWINDING_RULE);
912 dc.SetBrush(*wxTRANSPARENT_BRUSH);
913 dc.DrawRectangle(m_leftArrowRect);
915 wxDateTime fdnm = wxDateTime(1,m_date.GetMonth(), m_date.GetYear()) + wxDateSpan::Month(); // first day next month
916 if ( IsDateInRange(fdnm) && ( ( fdnm.GetYear() == m_date.GetYear() ) ? TRUE : AllowYearChange() ) )
918 m_rightArrowRect = wxRect(rarrowx - 4, arrowy - 3, (arrowheight / 2) + 8, (arrowheight + 6));
919 dc.SetBrush(wxBrush(*wxBLACK, wxSOLID));
920 dc.SetPen(wxPen(*wxBLACK, 1, wxSOLID));
921 dc.DrawPolygon(3, rightarrow, rarrowx , arrowy, wxWINDING_RULE);
922 dc.SetBrush(*wxTRANSPARENT_BRUSH);
923 dc.DrawRectangle(m_rightArrowRect);
930 // first draw the week days
931 if ( IsExposed(0, y, 7*m_widthCol, m_heightRow) )
934 wxLogDebug("painting the header");
937 dc.SetBackgroundMode(wxTRANSPARENT);
938 dc.SetTextForeground(m_colHeaderFg);
939 dc.SetBrush(wxBrush(m_colHeaderBg, wxSOLID));
940 dc.SetPen(wxPen(m_colHeaderBg, 1, wxSOLID));
941 dc.DrawRectangle(0, y, GetClientSize().x, m_heightRow);
943 bool startOnMonday = (GetWindowStyle() & wxCAL_MONDAY_FIRST) != 0;
944 for ( size_t wd = 0; wd < 7; wd++ )
948 n = wd == 6 ? 0 : wd + 1;
952 dc.GetTextExtent(m_weekdays[n], &dayw, &dayh);
953 dc.DrawText(m_weekdays[n], (wd*m_widthCol) + ((m_widthCol- dayw) / 2), y); // center the day-name
957 // then the calendar itself
958 dc.SetTextForeground(*wxBLACK);
959 //dc.SetFont(*wxNORMAL_FONT);
962 wxDateTime date = GetStartDate();
965 wxLogDebug("starting calendar from %s\n",
966 date.Format("%a %d-%m-%Y %H:%M:%S").c_str());
969 dc.SetBackgroundMode(wxSOLID);
970 for ( size_t nWeek = 1; nWeek <= 6; nWeek++, y += m_heightRow )
972 // if the update region doesn't intersect this row, don't paint it
973 if ( !IsExposed(0, y, 7*m_widthCol, m_heightRow - 1) )
975 date += wxDateSpan::Week();
981 wxLogDebug("painting week %d at y = %d\n", nWeek, y);
984 for ( size_t wd = 0; wd < 7; wd++ )
986 if ( IsDateShown(date) )
988 // don't use wxDate::Format() which prepends 0s
989 unsigned int day = date.GetDay();
990 wxString dayStr = wxString::Format(_T("%u"), day);
992 dc.GetTextExtent(dayStr, &width, (wxCoord *)NULL);
994 bool changedColours = FALSE,
998 wxCalendarDateAttr *attr = NULL;
1000 if ( date.GetMonth() != m_date.GetMonth() || !IsDateInRange(date) )
1002 // surrounding week or out-of-range
1004 dc.SetTextForeground(*wxLIGHT_GREY);
1005 changedColours = TRUE;
1009 isSel = date.IsSameDate(m_date);
1010 attr = m_attrs[day - 1];
1014 dc.SetTextForeground(m_colHighlightFg);
1015 dc.SetTextBackground(m_colHighlightBg);
1017 changedColours = TRUE;
1021 wxColour colFg, colBg;
1023 if ( attr->IsHoliday() )
1025 colFg = m_colHolidayFg;
1026 colBg = m_colHolidayBg;
1030 colFg = attr->GetTextColour();
1031 colBg = attr->GetBackgroundColour();
1036 dc.SetTextForeground(colFg);
1037 changedColours = TRUE;
1042 dc.SetTextBackground(colBg);
1043 changedColours = TRUE;
1046 if ( attr->HasFont() )
1048 dc.SetFont(attr->GetFont());
1054 wxCoord x = wd*m_widthCol + (m_widthCol - width) / 2;
1055 dc.DrawText(dayStr, x, y + 1);
1057 if ( !isSel && attr && attr->HasBorder() )
1060 if ( attr->HasBorderColour() )
1062 colBorder = attr->GetBorderColour();
1066 colBorder = m_foregroundColour;
1069 wxPen pen(colBorder, 1, wxSOLID);
1071 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1073 switch ( attr->GetBorder() )
1075 case wxCAL_BORDER_SQUARE:
1076 dc.DrawRectangle(x - 2, y,
1077 width + 4, m_heightRow);
1080 case wxCAL_BORDER_ROUND:
1081 dc.DrawEllipse(x - 2, y,
1082 width + 4, m_heightRow);
1086 wxFAIL_MSG(_T("unknown border type"));
1090 if ( changedColours )
1092 dc.SetTextForeground(m_foregroundColour);
1093 dc.SetTextBackground(m_backgroundColour);
1101 //else: just don't draw it
1103 date += wxDateSpan::Day();
1107 // Greying out out-of-range background
1108 bool showSurrounding = (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS) != 0;
1110 date = ( showSurrounding ) ? GetStartDate() : wxDateTime(1, m_date.GetMonth(), m_date.GetYear());
1111 if ( !IsDateInRange(date) )
1113 wxDateTime firstOOR = GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1115 wxBrush oorbrush = *wxLIGHT_GREY_BRUSH;
1116 oorbrush.SetStyle(wxFDIAGONAL_HATCH);
1118 HighlightRange(&dc, date, firstOOR, wxTRANSPARENT_PEN, &oorbrush);
1121 date = ( showSurrounding ) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date.GetMonth(), m_date.GetYear());
1122 if ( !IsDateInRange(date) )
1124 wxDateTime firstOOR = GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1126 wxBrush oorbrush = *wxLIGHT_GREY_BRUSH;
1127 oorbrush.SetStyle(wxFDIAGONAL_HATCH);
1129 HighlightRange(&dc, firstOOR, date, wxTRANSPARENT_PEN, &oorbrush);
1133 wxLogDebug("+++ finished painting");
1137 void wxCalendarCtrl::RefreshDate(const wxDateTime& date)
1143 // always refresh the whole row at once because our OnPaint() will draw
1144 // the whole row anyhow - and this allows the small optimisation in
1145 // OnClick() below to work
1148 rect.y = (m_heightRow * GetWeek(date)) + m_rowOffset;
1150 rect.width = 7*m_widthCol;
1151 rect.height = m_heightRow;
1154 // VZ: for some reason, the selected date seems to occupy more space under
1155 // MSW - this is probably some bug in the font size calculations, but I
1156 // don't know where exactly. This fix is ugly and leads to more
1157 // refreshes than really needed, but without it the selected days
1158 // leaves even more ugly underscores on screen.
1163 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
1166 rect.x + rect.width, rect.y + rect.height);
1169 Refresh(TRUE, &rect);
1172 void wxCalendarCtrl::HighlightRange(wxPaintDC* pDC, const wxDateTime& fromdate, const wxDateTime& todate, wxPen* pPen, wxBrush* pBrush)
1174 // Highlights the given range using pen and brush
1175 // Does nothing if todate < fromdate
1179 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate.Format("%d %m %Y"), todate.Format("%d %m %Y"));
1182 if ( todate >= fromdate )
1189 // implicit: both dates must be currently shown - checked by GetDateCoord
1190 if ( GetDateCoord(fromdate, &fd, &fw) && GetDateCoord(todate, &td, &tw) )
1193 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd, fw, td, tw);
1195 if ( ( (tw - fw) == 1 ) && ( td < fd ) )
1197 // special case: interval 7 days or less not in same week
1198 // split in two seperate intervals
1199 wxDateTime tfd = fromdate + wxDateSpan::Days(7-fd);
1200 wxDateTime ftd = tfd + wxDateSpan::Day();
1202 wxLogDebug("Highlight: Seperate segments");
1205 HighlightRange(pDC, fromdate, tfd, pPen, pBrush);
1206 HighlightRange(pDC, ftd, todate, pPen, pBrush);
1211 wxPoint corners[8]; // potentially 8 corners in polygon
1215 // simple case: same week
1217 corners[0] = wxPoint((fd - 1) * m_widthCol, (fw * m_heightRow) + m_rowOffset);
1218 corners[1] = wxPoint((fd - 1) * m_widthCol, ((fw + 1 ) * m_heightRow) + m_rowOffset);
1219 corners[2] = wxPoint(td * m_widthCol, ((tw + 1) * m_heightRow) + m_rowOffset);
1220 corners[3] = wxPoint(td * m_widthCol, (tw * m_heightRow) + m_rowOffset);
1225 // "complex" polygon
1226 corners[cidx] = wxPoint((fd - 1) * m_widthCol, (fw * m_heightRow) + m_rowOffset); cidx++;
1230 corners[cidx] = wxPoint((fd - 1) * m_widthCol, ((fw + 1) * m_heightRow) + m_rowOffset); cidx++;
1231 corners[cidx] = wxPoint(0, ((fw + 1) * m_heightRow) + m_rowOffset); cidx++;
1234 corners[cidx] = wxPoint(0, ((tw + 1) * m_heightRow) + m_rowOffset); cidx++;
1235 corners[cidx] = wxPoint(td * m_widthCol, ((tw + 1) * m_heightRow) + m_rowOffset); cidx++;
1239 corners[cidx] = wxPoint(td * m_widthCol, (tw * m_heightRow) + m_rowOffset); cidx++;
1240 corners[cidx] = wxPoint(7 * m_widthCol, (tw * m_heightRow) + m_rowOffset); cidx++;
1243 corners[cidx] = wxPoint(7 * m_widthCol, (fw * m_heightRow) + m_rowOffset); cidx++;
1249 pDC->SetBrush(*pBrush);
1251 pDC->DrawPolygon(numpoints, corners);
1257 wxLogDebug("--- HighlightRange ---");
1261 bool wxCalendarCtrl::GetDateCoord(const wxDateTime& date, int *day, int *week) const
1266 wxLogDebug("+++ GetDateCoord: (%s) +++", date.Format("%d %m %Y"));
1269 if ( IsDateShown(date) )
1271 bool startOnMonday = ( GetWindowStyle() & wxCAL_MONDAY_FIRST ) != 0;
1274 *day = date.GetWeekDay();
1276 if ( *day == 0 ) // sunday
1278 *day = ( startOnMonday ) ? 7 : 1;
1282 day += ( startOnMonday ) ? 0 : 1;
1285 int targetmonth = date.GetMonth() + (12 * date.GetYear());
1286 int thismonth = m_date.GetMonth() + (12 * m_date.GetYear());
1289 if ( targetmonth == thismonth )
1291 *week = GetWeek(date);
1295 if ( targetmonth < thismonth )
1297 *week = 1; // trivial
1299 else // targetmonth > thismonth
1305 // get the datecoord of the last day in the month currently shown
1307 wxLogDebug(" +++ LDOM +++");
1309 GetDateCoord(ldcm.SetToLastMonthDay(m_date.GetMonth(), m_date.GetYear()), &lastday, &lastweek);
1311 wxLogDebug(" --- LDOM ---");
1314 wxTimeSpan span = date - ldcm;
1316 int daysfromlast = span.GetDays();
1318 wxLogDebug("daysfromlast: %i", daysfromlast);
1320 if ( daysfromlast + lastday > 7 ) // past week boundary
1322 int wholeweeks = (daysfromlast / 7);
1323 *week = wholeweeks + lastweek;
1324 if ( (daysfromlast - (7 * wholeweeks) + lastday) > 7 )
1344 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date.Format("%d %m %Y"), *day, *week);
1350 // ----------------------------------------------------------------------------
1352 // ----------------------------------------------------------------------------
1354 void wxCalendarCtrl::OnDClick(wxMouseEvent& event)
1356 if ( HitTest(event.GetPosition()) != wxCAL_HITTEST_DAY )
1362 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED);
1366 void wxCalendarCtrl::OnClick(wxMouseEvent& event)
1369 wxDateTime::WeekDay wday;
1370 switch ( HitTest(event.GetPosition(), &date, &wday) )
1372 case wxCAL_HITTEST_DAY:
1373 if ( IsDateInRange(date) )
1377 GenerateEvents(wxEVT_CALENDAR_DAY_CHANGED,
1378 wxEVT_CALENDAR_SEL_CHANGED);
1382 case wxCAL_HITTEST_HEADER:
1384 wxCalendarEvent event(this, wxEVT_CALENDAR_WEEKDAY_CLICKED);
1385 event.m_wday = wday;
1386 (void)GetEventHandler()->ProcessEvent(event);
1390 case wxCAL_HITTEST_DECMONTH:
1391 case wxCAL_HITTEST_INCMONTH:
1392 case wxCAL_HITTEST_SURROUNDING_WEEK:
1393 SetDateAndNotify(date); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1397 wxFAIL_MSG(_T("unknown hittest code"));
1400 case wxCAL_HITTEST_NOWHERE:
1406 wxCalendarHitTestResult wxCalendarCtrl::HitTest(const wxPoint& pos,
1408 wxDateTime::WeekDay *wd)
1414 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1415 if ( (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION) )
1419 // we need to find out if the hit is on left arrow, on month or on right arrow
1421 if ( wxRegion(m_leftArrowRect).Contains(pos) == wxInRegion )
1425 if ( IsDateInRange(m_date - wxDateSpan::Month()) )
1427 *date = m_date - wxDateSpan::Month();
1431 *date = GetLowerDateLimit();
1435 return wxCAL_HITTEST_DECMONTH;
1438 if ( wxRegion(m_rightArrowRect).Contains(pos) == wxInRegion )
1442 if ( IsDateInRange(m_date + wxDateSpan::Month()) )
1444 *date = m_date + wxDateSpan::Month();
1448 *date = GetUpperDateLimit();
1452 return wxCAL_HITTEST_INCMONTH;
1457 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1459 int wday = pos.x / m_widthCol;
1460 // if ( y < m_heightRow )
1461 if ( y < (m_heightRow + m_rowOffset) )
1463 if ( y > m_rowOffset )
1467 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST )
1469 wday = wday == 6 ? 0 : wday + 1;
1472 *wd = (wxDateTime::WeekDay)wday;
1475 return wxCAL_HITTEST_HEADER;
1479 return wxCAL_HITTEST_NOWHERE;
1483 // int week = (y - m_heightRow) / m_heightRow;
1484 int week = (y - (m_heightRow + m_rowOffset)) / m_heightRow;
1485 if ( week >= 6 || wday >= 7 )
1487 return wxCAL_HITTEST_NOWHERE;
1490 wxDateTime dt = GetStartDate() + wxDateSpan::Days(7*week + wday);
1492 if ( IsDateShown(dt) )
1497 if ( dt.GetMonth() == m_date.GetMonth() )
1500 return wxCAL_HITTEST_DAY;
1504 return wxCAL_HITTEST_SURROUNDING_WEEK;
1509 return wxCAL_HITTEST_NOWHERE;
1513 // ----------------------------------------------------------------------------
1514 // subcontrols events handling
1515 // ----------------------------------------------------------------------------
1517 void wxCalendarCtrl::OnMonthChange(wxCommandEvent& event)
1519 wxDateTime::Tm tm = m_date.GetTm();
1521 wxDateTime::Month mon = (wxDateTime::Month)event.GetInt();
1522 if ( tm.mday > wxDateTime::GetNumberOfDays(mon, tm.year) )
1524 tm.mday = wxDateTime::GetNumberOfDays(mon, tm.year);
1527 wxDateTime target = wxDateTime(tm.mday, mon, tm.year);
1529 ChangeMonth(&target);
1530 SetDateAndNotify(target);
1533 void wxCalendarCtrl::OnYearChange(wxCommandEvent& event)
1535 int year = (int)event.GetInt();
1536 if ( year == INT_MIN )
1538 // invalid year in the spin control, ignore it
1542 // set the flag for SetDate(): otherwise it would overwrite the year
1543 // typed in by the user
1544 m_userChangedYear = TRUE;
1546 wxDateTime::Tm tm = m_date.GetTm();
1548 if ( tm.mday > wxDateTime::GetNumberOfDays(tm.mon, year) )
1550 tm.mday = wxDateTime::GetNumberOfDays(tm.mon, year);
1553 wxDateTime target = wxDateTime(tm.mday, tm.mon, year);
1555 if ( ChangeYear(&target) )
1557 SetDateAndNotify(target);
1561 // In this case we don't want to change the date. That would put us
1562 // inside the same year but a strange number of months forward/back..
1563 m_spinYear->SetValue(target.GetYear());
1567 // ----------------------------------------------------------------------------
1568 // keyboard interface
1569 // ----------------------------------------------------------------------------
1571 void wxCalendarCtrl::OnChar(wxKeyEvent& event)
1574 switch ( event.GetKeyCode() )
1578 target = m_date + wxDateSpan::Year();
1579 if ( ChangeYear(&target) )
1581 SetDateAndNotify(target);
1587 target = m_date - wxDateSpan::Year();
1588 if ( ChangeYear(&target) )
1590 SetDateAndNotify(target);
1595 target = m_date - wxDateSpan::Month();
1596 ChangeMonth(&target);
1597 SetDateAndNotify(target); // always
1601 target = m_date + wxDateSpan::Month();
1602 ChangeMonth(&target);
1603 SetDateAndNotify(target); // always
1607 if ( event.ControlDown() )
1609 target = wxDateTime(m_date).SetToNextWeekDay(
1610 GetWindowStyle() & wxCAL_MONDAY_FIRST
1611 ? wxDateTime::Sun : wxDateTime::Sat);
1612 if ( !IsDateInRange(target) )
1614 target = GetUpperDateLimit();
1616 SetDateAndNotify(target);
1619 SetDateAndNotify(m_date + wxDateSpan::Day());
1623 if ( event.ControlDown() )
1625 target = wxDateTime(m_date).SetToPrevWeekDay(
1626 GetWindowStyle() & wxCAL_MONDAY_FIRST
1627 ? wxDateTime::Mon : wxDateTime::Sun);
1628 if ( !IsDateInRange(target) )
1630 target = GetLowerDateLimit();
1632 SetDateAndNotify(target);
1635 SetDateAndNotify(m_date - wxDateSpan::Day());
1639 SetDateAndNotify(m_date - wxDateSpan::Week());
1643 SetDateAndNotify(m_date + wxDateSpan::Week());
1647 if ( event.ControlDown() )
1648 SetDateAndNotify(wxDateTime::Today());
1650 SetDateAndNotify(wxDateTime(1, m_date.GetMonth(), m_date.GetYear()));
1654 SetDateAndNotify(wxDateTime(m_date).SetToLastMonthDay());
1658 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED);
1666 // ----------------------------------------------------------------------------
1667 // holidays handling
1668 // ----------------------------------------------------------------------------
1670 void wxCalendarCtrl::EnableHolidayDisplay(bool display)
1672 long style = GetWindowStyle();
1674 style |= wxCAL_SHOW_HOLIDAYS;
1676 style &= ~wxCAL_SHOW_HOLIDAYS;
1678 SetWindowStyle(style);
1683 ResetHolidayAttrs();
1688 void wxCalendarCtrl::SetHolidayAttrs()
1690 if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS )
1692 ResetHolidayAttrs();
1694 wxDateTime::Tm tm = m_date.GetTm();
1695 wxDateTime dtStart(1, tm.mon, tm.year),
1696 dtEnd = dtStart.GetLastMonthDay();
1698 wxDateTimeArray hol;
1699 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol);
1701 size_t count = hol.GetCount();
1702 for ( size_t n = 0; n < count; n++ )
1704 SetHoliday(hol[n].GetDay());
1709 void wxCalendarCtrl::SetHoliday(size_t day)
1711 wxCHECK_RET( day > 0 && day < 32, _T("invalid day in SetHoliday") );
1713 wxCalendarDateAttr *attr = GetAttr(day);
1716 attr = new wxCalendarDateAttr;
1719 attr->SetHoliday(TRUE);
1721 // can't use SetAttr() because it would delete this pointer
1722 m_attrs[day - 1] = attr;
1725 void wxCalendarCtrl::ResetHolidayAttrs()
1727 for ( size_t day = 0; day < 31; day++ )
1731 m_attrs[day]->SetHoliday(FALSE);
1736 // ----------------------------------------------------------------------------
1738 // ----------------------------------------------------------------------------
1740 void wxCalendarEvent::Init()
1742 m_wday = wxDateTime::Inv_WeekDay;
1745 wxCalendarEvent::wxCalendarEvent(wxCalendarCtrl *cal, wxEventType type)
1746 : wxCommandEvent(type, cal->GetId())
1748 m_date = cal->GetDate();
1749 SetEventObject(cal);
1752 #endif // wxUSE_CALENDARCTRL