1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/calctrl.cpp
3 // Purpose: implementation fo the generic wxCalendarCtrl
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "calctrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/dcclient.h"
33 #include "wx/settings.h"
35 #include "wx/combobox.h"
36 #include "wx/stattext.h"
37 #include "wx/textctrl.h"
40 #if wxUSE_CALENDARCTRL
42 #include "wx/spinctrl.h"
44 #include "wx/calctrl.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 class wxMonthComboBox
: public wxComboBox
55 wxMonthComboBox(wxCalendarCtrl
*cal
);
57 void OnMonthChange(wxCommandEvent
& event
) { m_cal
->OnMonthChange(event
); }
60 wxCalendarCtrl
*m_cal
;
65 class wxYearSpinCtrl
: public wxSpinCtrl
68 wxYearSpinCtrl(wxCalendarCtrl
*cal
);
70 void OnYearTextChange(wxCommandEvent
& event
) { m_cal
->OnYearChange(event
); }
71 void OnYearChange(wxSpinEvent
& event
) { m_cal
->OnYearChange(event
); }
74 wxCalendarCtrl
*m_cal
;
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 BEGIN_EVENT_TABLE(wxCalendarCtrl
, wxControl
)
84 EVT_PAINT(wxCalendarCtrl::OnPaint
)
86 EVT_CHAR(wxCalendarCtrl::OnChar
)
88 EVT_LEFT_DOWN(wxCalendarCtrl::OnClick
)
89 EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick
)
92 BEGIN_EVENT_TABLE(wxMonthComboBox
, wxComboBox
)
93 EVT_COMBOBOX(-1, wxMonthComboBox::OnMonthChange
)
96 BEGIN_EVENT_TABLE(wxYearSpinCtrl
, wxSpinCtrl
)
97 EVT_TEXT(-1, wxYearSpinCtrl::OnYearTextChange
)
98 EVT_SPINCTRL(-1, wxYearSpinCtrl::OnYearChange
)
101 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl
, wxControl
)
102 IMPLEMENT_DYNAMIC_CLASS(wxCalendarEvent
, wxCommandEvent
)
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_SEL_CHANGED
)
109 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DAY_CHANGED
)
110 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_MONTH_CHANGED
)
111 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_YEAR_CHANGED
)
112 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DOUBLECLICKED
)
113 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_WEEKDAY_CLICKED
)
115 // ============================================================================
117 // ============================================================================
119 // ----------------------------------------------------------------------------
120 // wxMonthComboBox and wxYearSpinCtrl
121 // ----------------------------------------------------------------------------
123 wxMonthComboBox::wxMonthComboBox(wxCalendarCtrl
*cal
)
124 : wxComboBox(cal
->GetParent(), -1,
129 wxCB_READONLY
| wxCLIP_SIBLINGS
)
134 for ( m
= wxDateTime::Jan
; m
< wxDateTime::Inv_Month
; wxNextMonth(m
) )
136 Append(wxDateTime::GetMonthName(m
));
139 SetSelection(m_cal
->GetDate().GetMonth());
140 SetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
|wxSIZE_AUTO_HEIGHT
);
143 wxYearSpinCtrl::wxYearSpinCtrl(wxCalendarCtrl
*cal
)
144 : wxSpinCtrl(cal
->GetParent(), -1,
145 cal
->GetDate().Format(_T("%Y")),
148 wxSP_ARROW_KEYS
| wxCLIP_SIBLINGS
,
149 -4300, 10000, cal
->GetDate().GetYear())
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 void wxCalendarCtrl::Init()
163 m_userChangedYear
= FALSE
;
168 wxDateTime::WeekDay wd
;
169 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
171 m_weekdays
[wd
] = wxDateTime::GetWeekDayName(wd
, wxDateTime::Name_Abbr
);
174 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
179 m_colHighlightFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
180 m_colHighlightBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
182 m_colHolidayFg
= *wxRED
;
183 // don't set m_colHolidayBg - by default, same as our bg colour
185 m_colHeaderFg
= *wxBLUE
;
186 m_colHeaderBg
= *wxLIGHT_GREY
;
189 bool wxCalendarCtrl::Create(wxWindow
*parent
,
191 const wxDateTime
& date
,
195 const wxString
& name
)
197 if ( !wxControl::Create(parent
, id
, pos
, size
,
198 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
199 wxDefaultValidator
, name
) )
204 // needed to get the arrow keys normally used for the dialog navigation
205 SetWindowStyle(style
| wxWANTS_CHARS
);
207 m_date
= date
.IsValid() ? date
: wxDateTime::Today();
209 m_lowdate
= wxDefaultDateTime
;
210 m_highdate
= wxDefaultDateTime
;
212 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
214 m_spinYear
= new wxYearSpinCtrl(this);
215 m_staticYear
= new wxStaticText(GetParent(), -1, m_date
.Format(_T("%Y")),
216 wxDefaultPosition
, wxDefaultSize
,
219 m_comboMonth
= new wxMonthComboBox(this);
220 m_staticMonth
= new wxStaticText(GetParent(), -1, m_date
.Format(_T("%B")),
221 wxDefaultPosition
, wxDefaultSize
,
225 ShowCurrentControls();
228 if ( size
.x
== -1 || size
.y
== -1 )
230 sizeReal
= DoGetBestSize();
241 // we need to set the position as well because the main control position
242 // is not the same as the one specified in pos if we have the controls
244 SetSize(pos
.x
, pos
.y
, sizeReal
.x
, sizeReal
.y
);
246 SetBackgroundColour(*wxWHITE
);
247 SetFont(*wxSWISS_FONT
);
254 wxCalendarCtrl::~wxCalendarCtrl()
256 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
262 // ----------------------------------------------------------------------------
263 // forward wxWin functions to subcontrols
264 // ----------------------------------------------------------------------------
266 bool wxCalendarCtrl::Destroy()
269 m_staticYear
->Destroy();
271 m_spinYear
->Destroy();
273 m_comboMonth
->Destroy();
275 m_staticMonth
->Destroy();
280 m_staticMonth
= NULL
;
282 return wxControl::Destroy();
285 bool wxCalendarCtrl::Show(bool show
)
287 if ( !wxControl::Show(show
) )
292 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
294 if ( GetMonthControl() )
296 GetMonthControl()->Show(show
);
297 GetYearControl()->Show(show
);
304 bool wxCalendarCtrl::Enable(bool enable
)
306 if ( !wxControl::Enable(enable
) )
311 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
313 GetMonthControl()->Enable(enable
);
314 GetYearControl()->Enable(enable
);
320 // ----------------------------------------------------------------------------
321 // enable/disable month/year controls
322 // ----------------------------------------------------------------------------
324 void wxCalendarCtrl::ShowCurrentControls()
326 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
328 if ( AllowMonthChange() )
330 m_comboMonth
->Show();
331 m_staticMonth
->Hide();
333 if ( AllowYearChange() )
336 m_staticYear
->Hide();
344 m_comboMonth
->Hide();
345 m_staticMonth
->Show();
348 // year change not allowed here
350 m_staticYear
->Show();
354 wxControl
*wxCalendarCtrl::GetMonthControl() const
356 return AllowMonthChange() ? (wxControl
*)m_comboMonth
: (wxControl
*)m_staticMonth
;
359 wxControl
*wxCalendarCtrl::GetYearControl() const
361 return AllowYearChange() ? (wxControl
*)m_spinYear
: (wxControl
*)m_staticYear
;
364 void wxCalendarCtrl::EnableYearChange(bool enable
)
366 if ( enable
!= AllowYearChange() )
368 long style
= GetWindowStyle();
370 style
&= ~wxCAL_NO_YEAR_CHANGE
;
372 style
|= wxCAL_NO_YEAR_CHANGE
;
373 SetWindowStyle(style
);
375 ShowCurrentControls();
376 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
383 void wxCalendarCtrl::EnableMonthChange(bool enable
)
385 if ( enable
!= AllowMonthChange() )
387 long style
= GetWindowStyle();
389 style
&= ~wxCAL_NO_MONTH_CHANGE
;
391 style
|= wxCAL_NO_MONTH_CHANGE
;
392 SetWindowStyle(style
);
394 ShowCurrentControls();
395 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 bool wxCalendarCtrl::SetDate(const wxDateTime
& date
)
410 bool sameMonth
= m_date
.GetMonth() == date
.GetMonth(),
411 sameYear
= m_date
.GetYear() == date
.GetYear();
413 if ( IsDateInRange(date
) )
415 if ( sameMonth
&& sameYear
)
417 // just change the day
422 if ( AllowMonthChange() && (AllowYearChange() || sameYear
) )
427 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
429 // update the controls
430 m_comboMonth
->SetSelection(m_date
.GetMonth());
432 if ( AllowYearChange() )
434 if ( !m_userChangedYear
)
435 m_spinYear
->SetValue(m_date
.Format(_T("%Y")));
436 else // don't overwrite what the user typed in
437 m_userChangedYear
= FALSE
;
441 // as the month changed, holidays did too
444 // update the calendar
458 void wxCalendarCtrl::ChangeDay(const wxDateTime
& date
)
460 if ( m_date
!= date
)
462 // we need to refresh the row containing the old date and the one
463 // containing the new one
464 wxDateTime dateOld
= m_date
;
467 RefreshDate(dateOld
);
469 // if the date is in the same row, it was already drawn correctly
470 if ( GetWeek(m_date
) != GetWeek(dateOld
) )
477 void wxCalendarCtrl::SetDateAndNotify(const wxDateTime
& date
)
479 wxDateTime::Tm tm1
= m_date
.GetTm(),
483 if ( tm1
.year
!= tm2
.year
)
484 type
= wxEVT_CALENDAR_YEAR_CHANGED
;
485 else if ( tm1
.mon
!= tm2
.mon
)
486 type
= wxEVT_CALENDAR_MONTH_CHANGED
;
487 else if ( tm1
.mday
!= tm2
.mday
)
488 type
= wxEVT_CALENDAR_DAY_CHANGED
;
494 GenerateEvents(type
, wxEVT_CALENDAR_SEL_CHANGED
);
498 // ----------------------------------------------------------------------------
500 // ----------------------------------------------------------------------------
502 bool wxCalendarCtrl::SetLowerDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
506 if ( !(date
.IsValid()) || ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : TRUE
) )
518 bool wxCalendarCtrl::SetUpperDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
522 if ( !(date
.IsValid()) || ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : TRUE
) )
534 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& lowerdate
/* = wxDefaultDateTime */, const wxDateTime
& upperdate
/* = wxDefaultDateTime */)
539 ( !( lowerdate
.IsValid() ) || ( ( upperdate
.IsValid() ) ? ( lowerdate
<= upperdate
) : TRUE
) ) &&
540 ( !( upperdate
.IsValid() ) || ( ( lowerdate
.IsValid() ) ? ( upperdate
>= lowerdate
) : TRUE
) ) )
542 m_lowdate
= lowerdate
;
543 m_highdate
= upperdate
;
553 // ----------------------------------------------------------------------------
555 // ----------------------------------------------------------------------------
557 wxDateTime
wxCalendarCtrl::GetStartDate() const
559 wxDateTime::Tm tm
= m_date
.GetTm();
561 wxDateTime date
= wxDateTime(1, tm
.mon
, tm
.year
);
564 date
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
565 ? wxDateTime::Mon
: wxDateTime::Sun
);
567 if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
)
569 // We want to offset the calendar if we start on the first..
570 if ( date
.GetDay() == 1 )
572 date
-= wxDateSpan::Week();
579 bool wxCalendarCtrl::IsDateShown(const wxDateTime
& date
) const
581 if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
583 return date
.GetMonth() == m_date
.GetMonth();
591 bool wxCalendarCtrl::IsDateInRange(const wxDateTime
& date
) const
594 // Check if the given date is in the range specified
595 retval
= ( ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : TRUE
)
596 && ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : TRUE
) );
600 bool wxCalendarCtrl::ChangeYear(wxDateTime
* target
) const
604 if ( !(IsDateInRange(*target
)) )
606 if ( target
->GetYear() < m_date
.GetYear() )
608 if ( target
->GetYear() >= GetLowerDateLimit().GetYear() )
610 *target
= GetLowerDateLimit();
620 if ( target
->GetYear() <= GetUpperDateLimit().GetYear() )
622 *target
= GetUpperDateLimit();
639 bool wxCalendarCtrl::ChangeMonth(wxDateTime
* target
) const
643 if ( !(IsDateInRange(*target
)) )
647 if ( target
->GetMonth() < m_date
.GetMonth() )
649 *target
= GetLowerDateLimit();
653 *target
= GetUpperDateLimit();
660 size_t wxCalendarCtrl::GetWeek(const wxDateTime
& date
) const
662 size_t retval
= date
.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
663 ? wxDateTime::Monday_First
664 : wxDateTime::Sunday_First
);
666 if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
668 // we need to offset an extra week if we "start" on the 1st of the month
669 wxDateTime::Tm tm
= date
.GetTm();
671 wxDateTime datetest
= wxDateTime(1, tm
.mon
, tm
.year
);
674 datetest
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
675 ? wxDateTime::Mon
: wxDateTime::Sun
);
677 if ( datetest
.GetDay() == 1 )
686 // ----------------------------------------------------------------------------
688 // ----------------------------------------------------------------------------
690 // this is a composite control and it must arrange its parts each time its
691 // size or position changes: the combobox and spinctrl are along the top of
692 // the available area and the calendar takes up therest of the space
694 // the static controls are supposed to be always smaller than combo/spin so we
695 // always use the latter for size calculations and position the static to take
698 // the constants used for the layout
699 #define VERT_MARGIN 5 // distance between combo and calendar
701 #define HORZ_MARGIN 5 // spin
703 #define HORZ_MARGIN 15 // spin
705 wxSize
wxCalendarCtrl::DoGetBestSize() const
707 // calc the size of the calendar
708 ((wxCalendarCtrl
*)this)->RecalcGeometry(); // const_cast
710 wxCoord width
= 7*m_widthCol
,
711 height
= 7*m_heightRow
+ m_rowOffset
+ VERT_MARGIN
;
713 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
715 // the combobox doesn't report its height correctly (it returns the
716 // height including the drop down list) so don't use it
717 height
+= m_spinYear
->GetBestSize().y
;
720 if ( !HasFlag(wxBORDER_NONE
) )
722 // the border would clip the last line otherwise
727 return wxSize(width
, height
);
730 void wxCalendarCtrl::DoSetSize(int x
, int y
,
731 int width
, int height
,
734 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
737 void wxCalendarCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
741 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
743 wxSize sizeCombo
= m_comboMonth
->GetSize();
744 wxSize sizeStatic
= m_staticMonth
->GetSize();
746 int dy
= (sizeCombo
.y
- sizeStatic
.y
) / 2;
748 m_comboMonth
->Move(x
, y
);
749 m_staticMonth
->SetSize(x
, y
+ dy
, sizeCombo
.x
, sizeStatic
.y
);
751 int xDiff
= sizeCombo
.x
+ HORZ_MARGIN
;
753 m_spinYear
->SetSize(x
+ xDiff
, y
, width
- xDiff
, sizeCombo
.y
);
754 m_staticYear
->SetSize(x
+ xDiff
, y
+ dy
, width
- xDiff
, sizeStatic
.y
);
756 wxSize sizeSpin
= m_spinYear
->GetSize();
757 yDiff
= wxMax(sizeSpin
.y
, sizeCombo
.y
) + VERT_MARGIN
;
759 else // no controls on the top
764 wxControl::DoMoveWindow(x
, y
+ yDiff
, width
, height
- yDiff
);
767 void wxCalendarCtrl::DoGetPosition(int *x
, int *y
) const
769 wxControl::DoGetPosition(x
, y
);
771 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
773 // our real top corner is not in this position
776 *y
-= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
781 void wxCalendarCtrl::DoGetSize(int *width
, int *height
) const
783 wxControl::DoGetSize(width
, height
);
785 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
787 // our real height is bigger
788 if ( height
&& GetMonthControl())
790 *height
+= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
795 void wxCalendarCtrl::RecalcGeometry()
797 if ( m_widthCol
!= 0 )
804 // determine the column width (we assume that the weekday names are always
805 // wider (in any language) than the numbers)
807 wxDateTime::WeekDay wd
;
808 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
811 dc
.GetTextExtent(m_weekdays
[wd
], &width
, &m_heightRow
);
812 if ( width
> m_widthCol
)
818 // leave some margins
822 m_rowOffset
= (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) ? m_heightRow
: 0; // conditional in relation to style
825 // ----------------------------------------------------------------------------
827 // ----------------------------------------------------------------------------
829 void wxCalendarCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
838 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
839 m_date
.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
845 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
847 // draw the sequential month-selector
849 dc
.SetBackgroundMode(wxTRANSPARENT
);
850 dc
.SetTextForeground(*wxBLACK
);
851 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
852 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
853 dc
.DrawRectangle(0, y
, 7*m_widthCol
, m_heightRow
);
855 // Get extent of month-name + year
856 wxCoord monthw
, monthh
;
857 wxString headertext
= m_date
.Format(wxT("%B %Y"));
858 dc
.GetTextExtent(headertext
, &monthw
, &monthh
);
860 // draw month-name centered above weekdays
861 wxCoord monthx
= ((m_widthCol
* 7) - monthw
) / 2;
862 wxCoord monthy
= ((m_heightRow
- monthh
) / 2) + y
;
863 dc
.DrawText(headertext
, monthx
, monthy
);
865 // calculate the "month-arrows"
866 wxPoint leftarrow
[3];
867 wxPoint rightarrow
[3];
869 int arrowheight
= monthh
/ 2;
871 leftarrow
[0] = wxPoint(0, arrowheight
/ 2);
872 leftarrow
[1] = wxPoint(arrowheight
/ 2, 0);
873 leftarrow
[2] = wxPoint(arrowheight
/ 2, arrowheight
- 1);
875 rightarrow
[0] = wxPoint(0, 0);
876 rightarrow
[1] = wxPoint(arrowheight
/ 2, arrowheight
/ 2);
877 rightarrow
[2] = wxPoint(0, arrowheight
- 1);
879 // draw the "month-arrows"
881 wxCoord arrowy
= (m_heightRow
- arrowheight
) / 2;
882 wxCoord larrowx
= (m_widthCol
- (arrowheight
/ 2)) / 2;
883 wxCoord rarrowx
= ((m_widthCol
- (arrowheight
/ 2)) / 2) + m_widthCol
*6;
884 m_leftArrowRect
= wxRect(0, 0, 0, 0);
885 m_rightArrowRect
= wxRect(0, 0, 0, 0);
887 if ( AllowMonthChange() )
889 wxDateTime ldpm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) - wxDateSpan::Day(); // last day prev month
890 // Check if range permits change
891 if ( IsDateInRange(ldpm
) && ( ( ldpm
.GetYear() == m_date
.GetYear() ) ? TRUE
: AllowYearChange() ) )
893 m_leftArrowRect
= wxRect(larrowx
- 3, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
894 dc
.SetBrush(wxBrush(*wxBLACK
, wxSOLID
));
895 dc
.SetPen(wxPen(*wxBLACK
, 1, wxSOLID
));
896 dc
.DrawPolygon(3, leftarrow
, larrowx
, arrowy
, wxWINDING_RULE
);
897 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
898 dc
.DrawRectangle(m_leftArrowRect
);
900 wxDateTime fdnm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) + wxDateSpan::Month(); // first day next month
901 if ( IsDateInRange(fdnm
) && ( ( fdnm
.GetYear() == m_date
.GetYear() ) ? TRUE
: AllowYearChange() ) )
903 m_rightArrowRect
= wxRect(rarrowx
- 4, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
904 dc
.SetBrush(wxBrush(*wxBLACK
, wxSOLID
));
905 dc
.SetPen(wxPen(*wxBLACK
, 1, wxSOLID
));
906 dc
.DrawPolygon(3, rightarrow
, rarrowx
, arrowy
, wxWINDING_RULE
);
907 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
908 dc
.DrawRectangle(m_rightArrowRect
);
915 // first draw the week days
916 if ( IsExposed(0, y
, 7*m_widthCol
, m_heightRow
) )
919 wxLogDebug("painting the header");
922 dc
.SetBackgroundMode(wxTRANSPARENT
);
923 dc
.SetTextForeground(m_colHeaderFg
);
924 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
925 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
926 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
928 bool startOnMonday
= (GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
929 for ( size_t wd
= 0; wd
< 7; wd
++ )
933 n
= wd
== 6 ? 0 : wd
+ 1;
937 dc
.GetTextExtent(m_weekdays
[n
], &dayw
, &dayh
);
938 dc
.DrawText(m_weekdays
[n
], (wd
*m_widthCol
) + ((m_widthCol
- dayw
) / 2), y
); // center the day-name
942 // then the calendar itself
943 dc
.SetTextForeground(*wxBLACK
);
944 //dc.SetFont(*wxNORMAL_FONT);
947 wxDateTime date
= GetStartDate();
950 wxLogDebug("starting calendar from %s\n",
951 date
.Format("%a %d-%m-%Y %H:%M:%S").c_str());
954 dc
.SetBackgroundMode(wxSOLID
);
955 for ( size_t nWeek
= 1; nWeek
<= 6; nWeek
++, y
+= m_heightRow
)
957 // if the update region doesn't intersect this row, don't paint it
958 if ( !IsExposed(0, y
, 7*m_widthCol
, m_heightRow
- 1) )
960 date
+= wxDateSpan::Week();
966 wxLogDebug("painting week %d at y = %d\n", nWeek
, y
);
969 for ( size_t wd
= 0; wd
< 7; wd
++ )
971 if ( IsDateShown(date
) )
973 // don't use wxDate::Format() which prepends 0s
974 unsigned int day
= date
.GetDay();
975 wxString dayStr
= wxString::Format(_T("%u"), day
);
977 dc
.GetTextExtent(dayStr
, &width
, (wxCoord
*)NULL
);
979 bool changedColours
= FALSE
,
983 wxCalendarDateAttr
*attr
= NULL
;
985 if ( date
.GetMonth() != m_date
.GetMonth() || !IsDateInRange(date
) )
987 // surrounding week or out-of-range
989 dc
.SetTextForeground(*wxLIGHT_GREY
);
990 changedColours
= TRUE
;
994 isSel
= date
.IsSameDate(m_date
);
995 attr
= m_attrs
[day
- 1];
999 dc
.SetTextForeground(m_colHighlightFg
);
1000 dc
.SetTextBackground(m_colHighlightBg
);
1002 changedColours
= TRUE
;
1006 wxColour colFg
, colBg
;
1008 if ( attr
->IsHoliday() )
1010 colFg
= m_colHolidayFg
;
1011 colBg
= m_colHolidayBg
;
1015 colFg
= attr
->GetTextColour();
1016 colBg
= attr
->GetBackgroundColour();
1021 dc
.SetTextForeground(colFg
);
1022 changedColours
= TRUE
;
1027 dc
.SetTextBackground(colBg
);
1028 changedColours
= TRUE
;
1031 if ( attr
->HasFont() )
1033 dc
.SetFont(attr
->GetFont());
1039 wxCoord x
= wd
*m_widthCol
+ (m_widthCol
- width
) / 2;
1040 dc
.DrawText(dayStr
, x
, y
+ 1);
1042 if ( !isSel
&& attr
&& attr
->HasBorder() )
1045 if ( attr
->HasBorderColour() )
1047 colBorder
= attr
->GetBorderColour();
1051 colBorder
= m_foregroundColour
;
1054 wxPen
pen(colBorder
, 1, wxSOLID
);
1056 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1058 switch ( attr
->GetBorder() )
1060 case wxCAL_BORDER_SQUARE
:
1061 dc
.DrawRectangle(x
- 2, y
,
1062 width
+ 4, m_heightRow
);
1065 case wxCAL_BORDER_ROUND
:
1066 dc
.DrawEllipse(x
- 2, y
,
1067 width
+ 4, m_heightRow
);
1071 wxFAIL_MSG(_T("unknown border type"));
1075 if ( changedColours
)
1077 dc
.SetTextForeground(m_foregroundColour
);
1078 dc
.SetTextBackground(m_backgroundColour
);
1086 //else: just don't draw it
1088 date
+= wxDateSpan::Day();
1092 // Greying out out-of-range background
1093 bool showSurrounding
= (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) != 0;
1095 date
= ( showSurrounding
) ? GetStartDate() : wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear());
1096 if ( !IsDateInRange(date
) )
1098 wxDateTime firstOOR
= GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1100 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1101 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1103 HighlightRange(&dc
, date
, firstOOR
, wxTRANSPARENT_PEN
, &oorbrush
);
1106 date
= ( showSurrounding
) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear());
1107 if ( !IsDateInRange(date
) )
1109 wxDateTime firstOOR
= GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1111 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1112 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1114 HighlightRange(&dc
, firstOOR
, date
, wxTRANSPARENT_PEN
, &oorbrush
);
1118 wxLogDebug("+++ finished painting");
1122 void wxCalendarCtrl::RefreshDate(const wxDateTime
& date
)
1128 // always refresh the whole row at once because our OnPaint() will draw
1129 // the whole row anyhow - and this allows the small optimisation in
1130 // OnClick() below to work
1133 rect
.y
= (m_heightRow
* GetWeek(date
)) + m_rowOffset
;
1135 rect
.width
= 7*m_widthCol
;
1136 rect
.height
= m_heightRow
;
1139 // VZ: for some reason, the selected date seems to occupy more space under
1140 // MSW - this is probably some bug in the font size calculations, but I
1141 // don't know where exactly. This fix is ugly and leads to more
1142 // refreshes than really needed, but without it the selected days
1143 // leaves even more ugly underscores on screen.
1148 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
1151 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1154 Refresh(TRUE
, &rect
);
1157 void wxCalendarCtrl::HighlightRange(wxPaintDC
* pDC
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, wxPen
* pPen
, wxBrush
* pBrush
)
1159 // Highlights the given range using pen and brush
1160 // Does nothing if todate < fromdate
1164 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate
.Format("%d %m %Y"), todate
.Format("%d %m %Y"));
1167 if ( todate
>= fromdate
)
1174 // implicit: both dates must be currently shown - checked by GetDateCoord
1175 if ( GetDateCoord(fromdate
, &fd
, &fw
) && GetDateCoord(todate
, &td
, &tw
) )
1178 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd
, fw
, td
, tw
);
1180 if ( ( (tw
- fw
) == 1 ) && ( td
< fd
) )
1182 // special case: interval 7 days or less not in same week
1183 // split in two seperate intervals
1184 wxDateTime tfd
= fromdate
+ wxDateSpan::Days(7-fd
);
1185 wxDateTime ftd
= tfd
+ wxDateSpan::Day();
1187 wxLogDebug("Highlight: Seperate segments");
1190 HighlightRange(pDC
, fromdate
, tfd
, pPen
, pBrush
);
1191 HighlightRange(pDC
, ftd
, todate
, pPen
, pBrush
);
1196 wxPoint corners
[8]; // potentially 8 corners in polygon
1200 // simple case: same week
1202 corners
[0] = wxPoint((fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
);
1203 corners
[1] = wxPoint((fd
- 1) * m_widthCol
, ((fw
+ 1 ) * m_heightRow
) + m_rowOffset
);
1204 corners
[2] = wxPoint(td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
);
1205 corners
[3] = wxPoint(td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
);
1210 // "complex" polygon
1211 corners
[cidx
] = wxPoint((fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1215 corners
[cidx
] = wxPoint((fd
- 1) * m_widthCol
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1216 corners
[cidx
] = wxPoint(0, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1219 corners
[cidx
] = wxPoint(0, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1220 corners
[cidx
] = wxPoint(td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1224 corners
[cidx
] = wxPoint(td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1225 corners
[cidx
] = wxPoint(7 * m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1228 corners
[cidx
] = wxPoint(7 * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1234 pDC
->SetBrush(*pBrush
);
1236 pDC
->DrawPolygon(numpoints
, corners
);
1242 wxLogDebug("--- HighlightRange ---");
1246 bool wxCalendarCtrl::GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const
1251 wxLogDebug("+++ GetDateCoord: (%s) +++", date
.Format("%d %m %Y"));
1254 if ( IsDateShown(date
) )
1256 bool startOnMonday
= ( GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1259 *day
= date
.GetWeekDay();
1261 if ( *day
== 0 ) // sunday
1263 *day
= ( startOnMonday
) ? 7 : 1;
1267 day
+= ( startOnMonday
) ? 0 : 1;
1270 int targetmonth
= date
.GetMonth() + (12 * date
.GetYear());
1271 int thismonth
= m_date
.GetMonth() + (12 * m_date
.GetYear());
1274 if ( targetmonth
== thismonth
)
1276 *week
= GetWeek(date
);
1280 if ( targetmonth
< thismonth
)
1282 *week
= 1; // trivial
1284 else // targetmonth > thismonth
1290 // get the datecoord of the last day in the month currently shown
1292 wxLogDebug(" +++ LDOM +++");
1294 GetDateCoord(ldcm
.SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear()), &lastday
, &lastweek
);
1296 wxLogDebug(" --- LDOM ---");
1299 wxTimeSpan span
= date
- ldcm
;
1301 int daysfromlast
= span
.GetDays();
1303 wxLogDebug("daysfromlast: %i", daysfromlast
);
1305 if ( daysfromlast
+ lastday
> 7 ) // past week boundary
1307 int wholeweeks
= (daysfromlast
/ 7);
1308 *week
= wholeweeks
+ lastweek
;
1309 if ( (daysfromlast
- (7 * wholeweeks
) + lastday
) > 7 )
1329 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date
.Format("%d %m %Y"), *day
, *week
);
1335 // ----------------------------------------------------------------------------
1337 // ----------------------------------------------------------------------------
1339 void wxCalendarCtrl::OnDClick(wxMouseEvent
& event
)
1341 if ( HitTest(event
.GetPosition()) != wxCAL_HITTEST_DAY
)
1347 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1351 void wxCalendarCtrl::OnClick(wxMouseEvent
& event
)
1354 wxDateTime::WeekDay wday
;
1355 switch ( HitTest(event
.GetPosition(), &date
, &wday
) )
1357 case wxCAL_HITTEST_DAY
:
1358 if ( IsDateInRange(date
) )
1362 GenerateEvents(wxEVT_CALENDAR_DAY_CHANGED
,
1363 wxEVT_CALENDAR_SEL_CHANGED
);
1367 case wxCAL_HITTEST_HEADER
:
1369 wxCalendarEvent
event(this, wxEVT_CALENDAR_WEEKDAY_CLICKED
);
1370 event
.m_wday
= wday
;
1371 (void)GetEventHandler()->ProcessEvent(event
);
1375 case wxCAL_HITTEST_DECMONTH
:
1376 case wxCAL_HITTEST_INCMONTH
:
1377 case wxCAL_HITTEST_SURROUNDING_WEEK
:
1378 SetDateAndNotify(date
); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1382 wxFAIL_MSG(_T("unknown hittest code"));
1385 case wxCAL_HITTEST_NOWHERE
:
1391 wxCalendarHitTestResult
wxCalendarCtrl::HitTest(const wxPoint
& pos
,
1393 wxDateTime::WeekDay
*wd
)
1399 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1400 if ( (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
1404 // we need to find out if the hit is on left arrow, on month or on right arrow
1406 if ( wxRegion(m_leftArrowRect
).Contains(pos
) == wxInRegion
)
1410 if ( IsDateInRange(m_date
- wxDateSpan::Month()) )
1412 *date
= m_date
- wxDateSpan::Month();
1416 *date
= GetLowerDateLimit();
1420 return wxCAL_HITTEST_DECMONTH
;
1423 if ( wxRegion(m_rightArrowRect
).Contains(pos
) == wxInRegion
)
1427 if ( IsDateInRange(m_date
+ wxDateSpan::Month()) )
1429 *date
= m_date
+ wxDateSpan::Month();
1433 *date
= GetUpperDateLimit();
1437 return wxCAL_HITTEST_INCMONTH
;
1442 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1444 int wday
= pos
.x
/ m_widthCol
;
1445 // if ( y < m_heightRow )
1446 if ( y
< (m_heightRow
+ m_rowOffset
) )
1448 if ( y
> m_rowOffset
)
1452 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST
)
1454 wday
= wday
== 6 ? 0 : wday
+ 1;
1457 *wd
= (wxDateTime::WeekDay
)wday
;
1460 return wxCAL_HITTEST_HEADER
;
1464 return wxCAL_HITTEST_NOWHERE
;
1468 // int week = (y - m_heightRow) / m_heightRow;
1469 int week
= (y
- (m_heightRow
+ m_rowOffset
)) / m_heightRow
;
1470 if ( week
>= 6 || wday
>= 7 )
1472 return wxCAL_HITTEST_NOWHERE
;
1475 wxDateTime dt
= GetStartDate() + wxDateSpan::Days(7*week
+ wday
);
1477 if ( IsDateShown(dt
) )
1482 if ( dt
.GetMonth() == m_date
.GetMonth() )
1485 return wxCAL_HITTEST_DAY
;
1489 return wxCAL_HITTEST_SURROUNDING_WEEK
;
1494 return wxCAL_HITTEST_NOWHERE
;
1498 // ----------------------------------------------------------------------------
1499 // subcontrols events handling
1500 // ----------------------------------------------------------------------------
1502 void wxCalendarCtrl::OnMonthChange(wxCommandEvent
& event
)
1504 wxDateTime::Tm tm
= m_date
.GetTm();
1506 wxDateTime::Month mon
= (wxDateTime::Month
)event
.GetInt();
1507 if ( tm
.mday
> wxDateTime::GetNumberOfDays(mon
, tm
.year
) )
1509 tm
.mday
= wxDateTime::GetNumberOfDays(mon
, tm
.year
);
1512 wxDateTime target
= wxDateTime(tm
.mday
, mon
, tm
.year
);
1514 ChangeMonth(&target
);
1515 SetDateAndNotify(target
);
1518 void wxCalendarCtrl::OnYearChange(wxCommandEvent
& event
)
1520 int year
= (int)event
.GetInt();
1521 if ( year
== INT_MIN
)
1523 // invalid year in the spin control, ignore it
1527 // set the flag for SetDate(): otherwise it would overwrite the year
1528 // typed in by the user
1529 m_userChangedYear
= TRUE
;
1531 wxDateTime::Tm tm
= m_date
.GetTm();
1533 if ( tm
.mday
> wxDateTime::GetNumberOfDays(tm
.mon
, year
) )
1535 tm
.mday
= wxDateTime::GetNumberOfDays(tm
.mon
, year
);
1538 wxDateTime target
= wxDateTime(tm
.mday
, tm
.mon
, year
);
1540 if ( ChangeYear(&target
) )
1542 SetDateAndNotify(target
);
1546 // In this case we don't want to change the date. That would put us
1547 // inside the same year but a strange number of months forward/back..
1548 m_spinYear
->SetValue(target
.GetYear());
1552 // ----------------------------------------------------------------------------
1553 // keyboard interface
1554 // ----------------------------------------------------------------------------
1556 void wxCalendarCtrl::OnChar(wxKeyEvent
& event
)
1559 switch ( event
.KeyCode() )
1563 target
= m_date
+ wxDateSpan::Year();
1564 if ( ChangeYear(&target
) )
1566 SetDateAndNotify(target
);
1572 target
= m_date
- wxDateSpan::Year();
1573 if ( ChangeYear(&target
) )
1575 SetDateAndNotify(target
);
1580 target
= m_date
- wxDateSpan::Month();
1581 ChangeMonth(&target
);
1582 SetDateAndNotify(target
); // always
1586 target
= m_date
+ wxDateSpan::Month();
1587 ChangeMonth(&target
);
1588 SetDateAndNotify(target
); // always
1592 if ( event
.ControlDown() )
1594 target
= wxDateTime(m_date
).SetToNextWeekDay(
1595 GetWindowStyle() & wxCAL_MONDAY_FIRST
1596 ? wxDateTime::Sun
: wxDateTime::Sat
);
1597 if ( !IsDateInRange(target
) )
1599 target
= GetUpperDateLimit();
1601 SetDateAndNotify(target
);
1604 SetDateAndNotify(m_date
+ wxDateSpan::Day());
1608 if ( event
.ControlDown() )
1610 target
= wxDateTime(m_date
).SetToPrevWeekDay(
1611 GetWindowStyle() & wxCAL_MONDAY_FIRST
1612 ? wxDateTime::Mon
: wxDateTime::Sun
);
1613 if ( !IsDateInRange(target
) )
1615 target
= GetLowerDateLimit();
1617 SetDateAndNotify(target
);
1620 SetDateAndNotify(m_date
- wxDateSpan::Day());
1624 SetDateAndNotify(m_date
- wxDateSpan::Week());
1628 SetDateAndNotify(m_date
+ wxDateSpan::Week());
1632 if ( event
.ControlDown() )
1633 SetDateAndNotify(wxDateTime::Today());
1635 SetDateAndNotify(wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear()));
1639 SetDateAndNotify(wxDateTime(m_date
).SetToLastMonthDay());
1643 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1651 // ----------------------------------------------------------------------------
1652 // holidays handling
1653 // ----------------------------------------------------------------------------
1655 void wxCalendarCtrl::EnableHolidayDisplay(bool display
)
1657 long style
= GetWindowStyle();
1659 style
|= wxCAL_SHOW_HOLIDAYS
;
1661 style
&= ~wxCAL_SHOW_HOLIDAYS
;
1663 SetWindowStyle(style
);
1668 ResetHolidayAttrs();
1673 void wxCalendarCtrl::SetHolidayAttrs()
1675 if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS
)
1677 ResetHolidayAttrs();
1679 wxDateTime::Tm tm
= m_date
.GetTm();
1680 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
1681 dtEnd
= dtStart
.GetLastMonthDay();
1683 wxDateTimeArray hol
;
1684 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
1686 size_t count
= hol
.GetCount();
1687 for ( size_t n
= 0; n
< count
; n
++ )
1689 SetHoliday(hol
[n
].GetDay());
1694 void wxCalendarCtrl::SetHoliday(size_t day
)
1696 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in SetHoliday") );
1698 wxCalendarDateAttr
*attr
= GetAttr(day
);
1701 attr
= new wxCalendarDateAttr
;
1704 attr
->SetHoliday(TRUE
);
1706 // can't use SetAttr() because it would delete this pointer
1707 m_attrs
[day
- 1] = attr
;
1710 void wxCalendarCtrl::ResetHolidayAttrs()
1712 for ( size_t day
= 0; day
< 31; day
++ )
1716 m_attrs
[day
]->SetHoliday(FALSE
);
1721 // ----------------------------------------------------------------------------
1723 // ----------------------------------------------------------------------------
1725 void wxCalendarEvent::Init()
1727 m_wday
= wxDateTime::Inv_WeekDay
;
1730 wxCalendarEvent::wxCalendarEvent(wxCalendarCtrl
*cal
, wxEventType type
)
1731 : wxCommandEvent(type
, cal
->GetId())
1733 m_date
= cal
->GetDate();
1734 SetEventObject(cal
);
1737 #endif // wxUSE_CALENDARCTRL