1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/calctrl.cpp
3 // Purpose: implementation fo the generic wxCalendarCtrl
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "calctrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/dcclient.h"
33 #include "wx/settings.h"
35 #include "wx/combobox.h"
36 #include "wx/stattext.h"
39 #if wxUSE_CALENDARCTRL
41 #include "wx/calctrl.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 class wxMonthComboBox
: public wxComboBox
52 wxMonthComboBox(wxCalendarCtrl
*cal
);
54 void OnMonthChange(wxCommandEvent
& event
) { m_cal
->OnMonthChange(event
); }
57 wxCalendarCtrl
*m_cal
;
62 class wxYearSpinCtrl
: public wxSpinCtrl
65 wxYearSpinCtrl(wxCalendarCtrl
*cal
);
67 void OnYearChange(wxSpinEvent
& event
) { m_cal
->OnYearChange(event
); }
70 wxCalendarCtrl
*m_cal
;
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 BEGIN_EVENT_TABLE(wxCalendarCtrl
, wxControl
)
80 EVT_PAINT(wxCalendarCtrl::OnPaint
)
82 EVT_CHAR(wxCalendarCtrl::OnChar
)
84 EVT_LEFT_DOWN(wxCalendarCtrl::OnClick
)
85 EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick
)
88 BEGIN_EVENT_TABLE(wxMonthComboBox
, wxComboBox
)
89 EVT_COMBOBOX(-1, wxMonthComboBox::OnMonthChange
)
92 BEGIN_EVENT_TABLE(wxYearSpinCtrl
, wxSpinCtrl
)
93 EVT_SPINCTRL(-1, wxYearSpinCtrl::OnYearChange
)
96 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl
, wxControl
)
97 IMPLEMENT_DYNAMIC_CLASS(wxCalendarEvent
, wxCommandEvent
)
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_SEL_CHANGED
)
104 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DAY_CHANGED
)
105 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_MONTH_CHANGED
)
106 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_YEAR_CHANGED
)
107 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DOUBLECLICKED
)
108 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_WEEKDAY_CLICKED
)
110 // ============================================================================
112 // ============================================================================
114 // ----------------------------------------------------------------------------
115 // wxMonthComboBox and wxYearSpinCtrl
116 // ----------------------------------------------------------------------------
118 wxMonthComboBox::wxMonthComboBox(wxCalendarCtrl
*cal
)
119 : wxComboBox(cal
->GetParent(), -1,
124 wxCB_READONLY
| wxCLIP_SIBLINGS
)
129 for ( m
= wxDateTime::Jan
; m
< wxDateTime::Inv_Month
; wxNextMonth(m
) )
131 Append(wxDateTime::GetMonthName(m
));
134 SetSelection(m_cal
->GetDate().GetMonth());
135 SetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
|wxSIZE_AUTO_HEIGHT
);
138 wxYearSpinCtrl::wxYearSpinCtrl(wxCalendarCtrl
*cal
)
139 : wxSpinCtrl(cal
->GetParent(), -1,
140 cal
->GetDate().Format(_T("%Y")),
143 wxSP_ARROW_KEYS
| wxCLIP_SIBLINGS
,
144 -4300, 10000, cal
->GetDate().GetYear())
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 void wxCalendarCtrl::Init()
161 wxDateTime::WeekDay wd
;
162 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
164 m_weekdays
[wd
] = wxDateTime::GetWeekDayName(wd
, wxDateTime::Name_Abbr
);
167 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
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
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
193 wxDefaultValidator
, name
) )
198 // needed to get the arrow keys normally used for the dialog navigation
199 SetWindowStyle(style
| wxWANTS_CHARS
);
201 m_date
= date
.IsValid() ? date
: wxDateTime::Today();
203 m_lowdate
= wxDefaultDateTime
;
204 m_highdate
= wxDefaultDateTime
;
206 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
208 m_spinYear
= new wxYearSpinCtrl(this);
209 m_staticYear
= new wxStaticText(GetParent(), -1, m_date
.Format(_T("%Y")),
210 wxDefaultPosition
, wxDefaultSize
,
213 m_comboMonth
= new wxMonthComboBox(this);
214 m_staticMonth
= new wxStaticText(GetParent(), -1, m_date
.Format(_T("%B")),
215 wxDefaultPosition
, wxDefaultSize
,
219 ShowCurrentControls();
222 if ( size
.x
== -1 || size
.y
== -1 )
224 sizeReal
= DoGetBestSize();
237 SetBackgroundColour(*wxWHITE
);
238 SetFont(*wxSWISS_FONT
);
245 wxCalendarCtrl::~wxCalendarCtrl()
247 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
253 // ----------------------------------------------------------------------------
254 // forward wxWin functions to subcontrols
255 // ----------------------------------------------------------------------------
257 bool wxCalendarCtrl::Show(bool show
)
259 if ( !wxControl::Show(show
) )
264 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
266 if ( GetMonthControl() )
268 GetMonthControl()->Show(show
);
269 GetYearControl()->Show(show
);
276 bool wxCalendarCtrl::Enable(bool enable
)
278 if ( !wxControl::Enable(enable
) )
283 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
285 GetMonthControl()->Enable(enable
);
286 GetYearControl()->Enable(enable
);
292 // ----------------------------------------------------------------------------
293 // enable/disable month/year controls
294 // ----------------------------------------------------------------------------
296 void wxCalendarCtrl::ShowCurrentControls()
298 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
300 if ( AllowMonthChange() )
302 m_comboMonth
->Show();
303 m_staticMonth
->Hide();
305 if ( AllowYearChange() )
308 m_staticYear
->Hide();
316 m_comboMonth
->Hide();
317 m_staticMonth
->Show();
320 // year change not allowed here
322 m_staticYear
->Show();
326 wxControl
*wxCalendarCtrl::GetMonthControl() const
328 return AllowMonthChange() ? (wxControl
*)m_comboMonth
: (wxControl
*)m_staticMonth
;
331 wxControl
*wxCalendarCtrl::GetYearControl() const
333 return AllowYearChange() ? (wxControl
*)m_spinYear
: (wxControl
*)m_staticYear
;
336 void wxCalendarCtrl::EnableYearChange(bool enable
)
338 if ( enable
!= AllowYearChange() )
340 long style
= GetWindowStyle();
342 style
&= ~wxCAL_NO_YEAR_CHANGE
;
344 style
|= wxCAL_NO_YEAR_CHANGE
;
345 SetWindowStyle(style
);
347 ShowCurrentControls();
348 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
355 void wxCalendarCtrl::EnableMonthChange(bool enable
)
357 if ( enable
!= AllowMonthChange() )
359 long style
= GetWindowStyle();
361 style
&= ~wxCAL_NO_MONTH_CHANGE
;
363 style
|= wxCAL_NO_MONTH_CHANGE
;
364 SetWindowStyle(style
);
366 ShowCurrentControls();
367 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 bool wxCalendarCtrl::SetDate(const wxDateTime
& date
)
382 bool sameMonth
= m_date
.GetMonth() == date
.GetMonth(),
383 sameYear
= m_date
.GetYear() == date
.GetYear();
385 if ( IsDateInRange(date
) )
387 if ( sameMonth
&& sameYear
)
389 // just change the day
395 if ( !AllowMonthChange() || (!AllowYearChange() && !sameYear
) )
405 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
407 // update the controls
408 m_comboMonth
->SetSelection(m_date
.GetMonth());
410 if ( AllowYearChange() )
412 m_spinYear
->SetValue(m_date
.Format(_T("%Y")));
416 // as the month changed, holidays did too
419 // update the calendar
427 void wxCalendarCtrl::ChangeDay(const wxDateTime
& date
)
429 if ( m_date
!= date
)
431 // we need to refresh the row containing the old date and the one
432 // containing the new one
433 wxDateTime dateOld
= m_date
;
436 RefreshDate(dateOld
);
438 // if the date is in the same row, it was already drawn correctly
439 if ( GetWeek(m_date
) != GetWeek(dateOld
) )
446 void wxCalendarCtrl::SetDateAndNotify(const wxDateTime
& date
)
448 wxDateTime::Tm tm1
= m_date
.GetTm(),
452 if ( tm1
.year
!= tm2
.year
)
453 type
= wxEVT_CALENDAR_YEAR_CHANGED
;
454 else if ( tm1
.mon
!= tm2
.mon
)
455 type
= wxEVT_CALENDAR_MONTH_CHANGED
;
456 else if ( tm1
.mday
!= tm2
.mday
)
457 type
= wxEVT_CALENDAR_DAY_CHANGED
;
463 GenerateEvents(type
, wxEVT_CALENDAR_SEL_CHANGED
);
467 // ----------------------------------------------------------------------------
469 // ----------------------------------------------------------------------------
471 bool wxCalendarCtrl::SetLowerDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
475 if ( !(date
.IsValid()) || ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : TRUE
) )
487 bool wxCalendarCtrl::SetUpperDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
491 if ( !(date
.IsValid()) || ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : TRUE
) )
503 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& lowerdate
/* = wxDefaultDateTime */, const wxDateTime
& upperdate
/* = wxDefaultDateTime */)
508 ( !( lowerdate
.IsValid() ) || ( ( upperdate
.IsValid() ) ? ( lowerdate
<= upperdate
) : TRUE
) ) &&
509 ( !( upperdate
.IsValid() ) || ( ( lowerdate
.IsValid() ) ? ( upperdate
>= lowerdate
) : TRUE
) ) )
511 m_lowdate
= lowerdate
;
512 m_highdate
= upperdate
;
522 // ----------------------------------------------------------------------------
524 // ----------------------------------------------------------------------------
526 wxDateTime
wxCalendarCtrl::GetStartDate() const
528 wxDateTime::Tm tm
= m_date
.GetTm();
530 wxDateTime date
= wxDateTime(1, tm
.mon
, tm
.year
);
533 date
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
534 ? wxDateTime::Mon
: wxDateTime::Sun
);
536 if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
)
538 // We want to offset the calendar if we start on the first..
539 if ( date
.GetDay() == 1 )
541 date
-= wxDateSpan::Week();
548 bool wxCalendarCtrl::IsDateShown(const wxDateTime
& date
) const
550 if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
552 return date
.GetMonth() == m_date
.GetMonth();
560 bool wxCalendarCtrl::IsDateInRange(const wxDateTime
& date
) const
563 // Check if the given date is in the range specified
564 retval
= ( ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : TRUE
)
565 && ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : TRUE
) );
569 bool wxCalendarCtrl::ChangeYear(wxDateTime
* target
) const
573 if ( !(IsDateInRange(*target
)) )
575 if ( target
->GetYear() < m_date
.GetYear() )
577 if ( target
->GetYear() >= GetLowerDateLimit().GetYear() )
579 *target
= GetLowerDateLimit();
589 if ( target
->GetYear() <= GetUpperDateLimit().GetYear() )
591 *target
= GetUpperDateLimit();
608 bool wxCalendarCtrl::ChangeMonth(wxDateTime
* target
) const
612 if ( !(IsDateInRange(*target
)) )
616 if ( target
->GetMonth() < m_date
.GetMonth() )
618 *target
= GetLowerDateLimit();
622 *target
= GetUpperDateLimit();
629 size_t wxCalendarCtrl::GetWeek(const wxDateTime
& date
) const
631 size_t retval
= date
.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
632 ? wxDateTime::Monday_First
633 : wxDateTime::Sunday_First
);
635 if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
637 // we need to offset an extra week if we "start" on the 1st of the month
638 wxDateTime::Tm tm
= date
.GetTm();
640 wxDateTime datetest
= wxDateTime(1, tm
.mon
, tm
.year
);
643 datetest
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
644 ? wxDateTime::Mon
: wxDateTime::Sun
);
646 if ( datetest
.GetDay() == 1 )
655 // ----------------------------------------------------------------------------
657 // ----------------------------------------------------------------------------
659 // this is a composite control and it must arrange its parts each time its
660 // size or position changes: the combobox and spinctrl are along the top of
661 // the available area and the calendar takes up therest of the space
663 // the static controls are supposed to be always smaller than combo/spin so we
664 // always use the latter for size calculations and position the static to take
667 // the constants used for the layout
668 #define VERT_MARGIN 5 // distance between combo and calendar
669 #define HORZ_MARGIN 15 // spin
671 wxSize
wxCalendarCtrl::DoGetBestSize() const
673 // calc the size of the calendar
674 ((wxCalendarCtrl
*)this)->RecalcGeometry(); // const_cast
676 wxCoord width
= 7*m_widthCol
,
677 height
= 7*m_heightRow
+ m_rowOffset
;
679 // the combobox doesn't report its height correctly (it returns the
680 // height including the drop down list) so don't use it
682 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
684 height
+= VERT_MARGIN
+ m_spinYear
->GetBestSize().y
;
688 height
+= VERT_MARGIN
;
691 // if ( GetWindowStyle() & (wxRAISED_BORDER | wxSUNKEN_BORDER) ) // This doesn't work. Default is wxBORDER_DEFAULT (0)
692 if ( !(GetWindowStyle() & wxBORDER_NONE
) )
694 // the border would clip the last line otherwise
699 return wxSize(width
, height
);
702 void wxCalendarCtrl::DoSetSize(int x
, int y
,
703 int width
, int height
,
706 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
709 void wxCalendarCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
714 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
716 wxSize sizeCombo
= m_comboMonth
->GetSize();
717 wxSize sizeStatic
= m_staticMonth
->GetSize();
719 int dy
= (sizeCombo
.y
- sizeStatic
.y
) / 2;
721 m_comboMonth
->Move(x
, y
);
722 m_staticMonth
->SetSize(x
, y
+ dy
, sizeCombo
.x
, sizeStatic
.y
);
724 xDiff
= sizeCombo
.x
+ HORZ_MARGIN
;
726 m_spinYear
->SetSize(x
+ xDiff
, y
, width
- xDiff
, sizeCombo
.y
);
727 m_staticYear
->SetSize(x
+ xDiff
, y
+ dy
, width
- xDiff
, sizeStatic
.y
);
729 wxSize sizeSpin
= m_spinYear
->GetSize();
730 yDiff
= wxMax(sizeSpin
.y
, sizeCombo
.y
) + VERT_MARGIN
;
733 wxControl::DoMoveWindow(x
, y
+ yDiff
, width
, height
- yDiff
);
736 void wxCalendarCtrl::DoGetPosition(int *x
, int *y
) const
738 wxControl::DoGetPosition(x
, y
);
740 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
742 // our real top corner is not in this position
745 *y
-= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
750 void wxCalendarCtrl::DoGetSize(int *width
, int *height
) const
752 wxControl::DoGetSize(width
, height
);
754 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
756 // our real height is bigger
759 *height
+= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
764 void wxCalendarCtrl::RecalcGeometry()
766 if ( m_widthCol
!= 0 )
773 // determine the column width (we assume that the weekday names are always
774 // wider (in any language) than the numbers)
776 wxDateTime::WeekDay wd
;
777 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
780 dc
.GetTextExtent(m_weekdays
[wd
], &width
, &m_heightRow
);
781 if ( width
> m_widthCol
)
787 // leave some margins
791 m_rowOffset
= (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) ? m_heightRow
: 0; // conditional in relation to style
794 // ----------------------------------------------------------------------------
796 // ----------------------------------------------------------------------------
798 void wxCalendarCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
807 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
808 m_date
.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
814 /////////////////////////////////////////////////////////////////////////////////////////
816 if ( (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
818 // draw the sequential month-selector
820 dc
.SetBackgroundMode(wxTRANSPARENT
);
821 dc
.SetTextForeground(*wxBLACK
);
822 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
823 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
824 dc
.DrawRectangle(0, y
, 7*m_widthCol
, m_heightRow
);
826 // Get extent of month-name + year
827 wxCoord monthw
, monthh
;
828 wxString headertext
= m_date
.Format(wxT("%B %Y"));
829 dc
.GetTextExtent(headertext
, &monthw
, &monthh
);
831 // draw month-name centered above weekdays
832 wxCoord monthx
= ((m_widthCol
* 7) - monthw
) / 2;
833 wxCoord monthy
= ((m_heightRow
- monthh
) / 2) + y
;
834 dc
.DrawText(headertext
, monthx
, monthy
);
836 // calculate the "month-arrows"
837 wxPoint leftarrow
[3];
838 wxPoint rightarrow
[3];
840 int arrowheight
= monthh
/ 2;
842 leftarrow
[0] = wxPoint(0, arrowheight
/ 2);
843 leftarrow
[1] = wxPoint(arrowheight
/ 2, 0);
844 leftarrow
[2] = wxPoint(arrowheight
/ 2, arrowheight
- 1);
846 rightarrow
[0] = wxPoint(0, 0);
847 rightarrow
[1] = wxPoint(arrowheight
/ 2, arrowheight
/ 2);
848 rightarrow
[2] = wxPoint(0, arrowheight
- 1);
850 // draw the "month-arrows"
852 wxCoord arrowy
= (m_heightRow
- arrowheight
) / 2;
853 wxCoord larrowx
= (m_widthCol
- (arrowheight
/ 2)) / 2;
854 wxCoord rarrowx
= ((m_widthCol
- (arrowheight
/ 2)) / 2) + m_widthCol
*6;
855 m_leftArrowRect
= wxRect(0, 0, 0, 0);
856 m_rightArrowRect
= wxRect(0, 0, 0, 0);
858 if ( AllowMonthChange() )
860 wxDateTime ldpm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) - wxDateSpan::Day(); // last day prev month
861 // Check if range permits change
862 if ( IsDateInRange(ldpm
) && ( ( ldpm
.GetYear() == m_date
.GetYear() ) ? TRUE
: AllowYearChange() ) )
864 m_leftArrowRect
= wxRect(larrowx
- 3, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
865 dc
.SetBrush(wxBrush(*wxBLACK
, wxSOLID
));
866 dc
.SetPen(wxPen(*wxBLACK
, 1, wxSOLID
));
867 dc
.DrawPolygon(3, leftarrow
, larrowx
, arrowy
, wxWINDING_RULE
);
868 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
869 dc
.DrawRectangle(m_leftArrowRect
);
871 wxDateTime fdnm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) + wxDateSpan::Month(); // first day next month
872 if ( IsDateInRange(fdnm
) && ( ( fdnm
.GetYear() == m_date
.GetYear() ) ? TRUE
: AllowYearChange() ) )
874 m_rightArrowRect
= wxRect(rarrowx
- 4, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
875 dc
.SetBrush(wxBrush(*wxBLACK
, wxSOLID
));
876 dc
.SetPen(wxPen(*wxBLACK
, 1, wxSOLID
));
877 dc
.DrawPolygon(3, rightarrow
, rarrowx
, arrowy
, wxWINDING_RULE
);
878 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
879 dc
.DrawRectangle(m_rightArrowRect
);
886 /////////////////////////////////////////////////////////////////////////////////////////
888 // first draw the week days
889 if ( IsExposed(0, y
, 7*m_widthCol
, m_heightRow
) )
892 wxLogDebug("painting the header");
895 dc
.SetBackgroundMode(wxTRANSPARENT
);
896 dc
.SetTextForeground(m_colHeaderFg
);
897 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
898 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
899 dc
.DrawRectangle(0, y
, 7*m_widthCol
, m_heightRow
);
901 bool startOnMonday
= (GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
902 for ( size_t wd
= 0; wd
< 7; wd
++ )
906 n
= wd
== 6 ? 0 : wd
+ 1;
910 dc
.GetTextExtent(m_weekdays
[n
], &dayw
, &dayh
);
911 // dc.DrawText(m_weekdays[n], wd*m_widthCol + 1, y);
912 dc
.DrawText(m_weekdays
[n
], (wd
*m_widthCol
) + ((m_widthCol
- dayw
) / 2), y
); // center the day-name
916 // then the calendar itself
917 dc
.SetTextForeground(*wxBLACK
);
918 //dc.SetFont(*wxNORMAL_FONT);
921 wxDateTime date
= GetStartDate();
924 wxLogDebug("starting calendar from %s\n",
925 date
.Format("%a %d-%m-%Y %H:%M:%S").c_str());
928 dc
.SetBackgroundMode(wxSOLID
);
929 for ( size_t nWeek
= 1; nWeek
<= 6; nWeek
++, y
+= m_heightRow
)
931 // if the update region doesn't intersect this row, don't paint it
932 if ( !IsExposed(0, y
, 7*m_widthCol
, m_heightRow
- 1) )
934 date
+= wxDateSpan::Week();
940 wxLogDebug("painting week %d at y = %d\n", nWeek
, y
);
943 for ( size_t wd
= 0; wd
< 7; wd
++ )
945 if ( IsDateShown(date
) )
947 // don't use wxDate::Format() which prepends 0s
948 unsigned int day
= date
.GetDay();
949 wxString dayStr
= wxString::Format(_T("%u"), day
);
951 dc
.GetTextExtent(dayStr
, &width
, (wxCoord
*)NULL
);
953 bool changedColours
= FALSE
,
957 wxCalendarDateAttr
*attr
= NULL
;
959 if ( date
.GetMonth() != m_date
.GetMonth() || !IsDateInRange(date
) )
961 // surrounding week or out-of-range
963 dc
.SetTextForeground(*wxLIGHT_GREY
);
964 changedColours
= TRUE
;
968 isSel
= date
.IsSameDate(m_date
);
969 attr
= m_attrs
[day
- 1];
973 dc
.SetTextForeground(m_colHighlightFg
);
974 dc
.SetTextBackground(m_colHighlightBg
);
976 changedColours
= TRUE
;
980 wxColour colFg
, colBg
;
982 if ( attr
->IsHoliday() )
984 colFg
= m_colHolidayFg
;
985 colBg
= m_colHolidayBg
;
989 colFg
= attr
->GetTextColour();
990 colBg
= attr
->GetBackgroundColour();
995 dc
.SetTextForeground(colFg
);
996 changedColours
= TRUE
;
1001 dc
.SetTextBackground(colBg
);
1002 changedColours
= TRUE
;
1005 if ( attr
->HasFont() )
1007 dc
.SetFont(attr
->GetFont());
1013 wxCoord x
= wd
*m_widthCol
+ (m_widthCol
- width
) / 2;
1014 dc
.DrawText(dayStr
, x
, y
+ 1);
1016 if ( !isSel
&& attr
&& attr
->HasBorder() )
1019 if ( attr
->HasBorderColour() )
1021 colBorder
= attr
->GetBorderColour();
1025 colBorder
= m_foregroundColour
;
1028 wxPen
pen(colBorder
, 1, wxSOLID
);
1030 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1032 switch ( attr
->GetBorder() )
1034 case wxCAL_BORDER_SQUARE
:
1035 dc
.DrawRectangle(x
- 2, y
,
1036 width
+ 4, m_heightRow
);
1039 case wxCAL_BORDER_ROUND
:
1040 dc
.DrawEllipse(x
- 2, y
,
1041 width
+ 4, m_heightRow
);
1045 wxFAIL_MSG(_T("unknown border type"));
1049 if ( changedColours
)
1051 dc
.SetTextForeground(m_foregroundColour
);
1052 dc
.SetTextBackground(m_backgroundColour
);
1060 //else: just don't draw it
1062 date
+= wxDateSpan::Day();
1066 // Greying out out-of-range background
1067 bool showSurrounding
= (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) != 0;
1069 date
= ( showSurrounding
) ? GetStartDate() : wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear());
1070 if ( !IsDateInRange(date
) )
1072 wxDateTime firstOOR
= GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1074 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1075 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1077 HighlightRange(&dc
, date
, firstOOR
, wxTRANSPARENT_PEN
, &oorbrush
);
1080 date
= ( showSurrounding
) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear());
1081 if ( !IsDateInRange(date
) )
1083 wxDateTime firstOOR
= GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1085 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1086 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1088 HighlightRange(&dc
, firstOOR
, date
, wxTRANSPARENT_PEN
, &oorbrush
);
1092 wxLogDebug("+++ finished painting");
1096 void wxCalendarCtrl::RefreshDate(const wxDateTime
& date
)
1102 // always refresh the whole row at once because our OnPaint() will draw
1103 // the whole row anyhow - and this allows the small optimisation in
1104 // OnClick() below to work
1107 rect
.y
= (m_heightRow
* GetWeek(date
)) + m_rowOffset
;
1109 rect
.width
= 7*m_widthCol
;
1110 rect
.height
= m_heightRow
;
1113 // VZ: for some reason, the selected date seems to occupy more space under
1114 // MSW - this is probably some bug in the font size calculations, but I
1115 // don't know where exactly. This fix is ugly and leads to more
1116 // refreshes than really needed, but without it the selected days
1117 // leaves even more ugly underscores on screen.
1122 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
1125 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1128 Refresh(TRUE
, &rect
);
1131 void wxCalendarCtrl::HighlightRange(wxPaintDC
* pDC
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, wxPen
* pPen
, wxBrush
* pBrush
)
1133 // Highlights the given range using pen and brush
1134 // Does nothing if todate < fromdate
1138 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate
.Format("%d %m %Y"), todate
.Format("%d %m %Y"));
1141 if ( todate
>= fromdate
)
1148 // implicit: both dates must be currently shown - checked by GetDateCoord
1149 if ( GetDateCoord(fromdate
, &fd
, &fw
) && GetDateCoord(todate
, &td
, &tw
) )
1152 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd
, fw
, td
, tw
);
1154 if ( ( (tw
- fw
) == 1 ) && ( td
< fd
) )
1156 // special case: interval 7 days or less not in same week
1157 // split in two seperate intervals
1158 wxDateTime tfd
= fromdate
+ wxDateSpan::Days(7-fd
);
1159 wxDateTime ftd
= tfd
+ wxDateSpan::Day();
1161 wxLogDebug("Highlight: Seperate segments");
1164 HighlightRange(pDC
, fromdate
, tfd
, pPen
, pBrush
);
1165 HighlightRange(pDC
, ftd
, todate
, pPen
, pBrush
);
1170 wxPoint corners
[8]; // potentially 8 corners in polygon
1174 // simple case: same week
1176 corners
[0] = wxPoint((fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
);
1177 corners
[1] = wxPoint((fd
- 1) * m_widthCol
, ((fw
+ 1 ) * m_heightRow
) + m_rowOffset
);
1178 corners
[2] = wxPoint(td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
);
1179 corners
[3] = wxPoint(td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
);
1184 // "complex" polygon
1185 corners
[cidx
] = wxPoint((fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1189 corners
[cidx
] = wxPoint((fd
- 1) * m_widthCol
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1190 corners
[cidx
] = wxPoint(0, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1193 corners
[cidx
] = wxPoint(0, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1194 corners
[cidx
] = wxPoint(td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1198 corners
[cidx
] = wxPoint(td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1199 corners
[cidx
] = wxPoint(7 * m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1202 corners
[cidx
] = wxPoint(7 * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1208 pDC
->SetBrush(*pBrush
);
1210 pDC
->DrawPolygon(numpoints
, corners
);
1216 wxLogDebug("--- HighlightRange ---");
1220 bool wxCalendarCtrl::GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const
1225 wxLogDebug("+++ GetDateCoord: (%s) +++", date
.Format("%d %m %Y"));
1228 if ( IsDateShown(date
) )
1230 bool startOnMonday
= ( GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1233 *day
= date
.GetWeekDay();
1235 if ( *day
== 0 ) // sunday
1237 *day
= ( startOnMonday
) ? 7 : 1;
1241 day
+= ( startOnMonday
) ? 0 : 1;
1244 int targetmonth
= date
.GetMonth() + (12 * date
.GetYear());
1245 int thismonth
= m_date
.GetMonth() + (12 * m_date
.GetYear());
1248 if ( targetmonth
== thismonth
)
1250 *week
= GetWeek(date
);
1254 if ( targetmonth
< thismonth
)
1256 *week
= 1; // trivial
1258 else // targetmonth > thismonth
1264 // get the datecoord of the last day in the month currently shown
1266 wxLogDebug(" +++ LDOM +++");
1268 GetDateCoord(ldcm
.SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear()), &lastday
, &lastweek
);
1270 wxLogDebug(" --- LDOM ---");
1273 wxTimeSpan span
= date
- ldcm
;
1275 int daysfromlast
= span
.GetDays();
1277 wxLogDebug("daysfromlast: %i", daysfromlast
);
1279 if ( daysfromlast
+ lastday
> 7 ) // past week boundary
1281 int wholeweeks
= (daysfromlast
/ 7);
1282 *week
= wholeweeks
+ lastweek
;
1283 if ( (daysfromlast
- (7 * wholeweeks
) + lastday
) > 7 )
1303 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date
.Format("%d %m %Y"), *day
, *week
);
1309 // ----------------------------------------------------------------------------
1311 // ----------------------------------------------------------------------------
1313 void wxCalendarCtrl::OnDClick(wxMouseEvent
& event
)
1315 if ( HitTest(event
.GetPosition()) != wxCAL_HITTEST_DAY
)
1321 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1325 void wxCalendarCtrl::OnClick(wxMouseEvent
& event
)
1328 wxDateTime::WeekDay wday
;
1329 switch ( HitTest(event
.GetPosition(), &date
, &wday
) )
1331 case wxCAL_HITTEST_DAY
:
1332 if ( IsDateInRange(date
) )
1336 GenerateEvents(wxEVT_CALENDAR_DAY_CHANGED
,
1337 wxEVT_CALENDAR_SEL_CHANGED
);
1341 case wxCAL_HITTEST_HEADER
:
1343 wxCalendarEvent
event(this, wxEVT_CALENDAR_WEEKDAY_CLICKED
);
1344 event
.m_wday
= wday
;
1345 (void)GetEventHandler()->ProcessEvent(event
);
1349 case wxCAL_HITTEST_DECMONTH
:
1350 case wxCAL_HITTEST_INCMONTH
:
1351 case wxCAL_HITTEST_SURROUNDING_WEEK
:
1352 SetDateAndNotify(date
); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1356 wxFAIL_MSG(_T("unknown hittest code"));
1359 case wxCAL_HITTEST_NOWHERE
:
1365 wxCalendarHitTestResult
wxCalendarCtrl::HitTest(const wxPoint
& pos
,
1367 wxDateTime::WeekDay
*wd
)
1373 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1374 if ( (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
1378 // we need to find out if the hit is on left arrow, on month or on right arrow
1380 if ( wxRegion(m_leftArrowRect
).Contains(pos
) == wxInRegion
)
1384 if ( IsDateInRange(m_date
- wxDateSpan::Month()) )
1386 *date
= m_date
- wxDateSpan::Month();
1390 *date
= GetLowerDateLimit();
1394 return wxCAL_HITTEST_DECMONTH
;
1397 if ( wxRegion(m_rightArrowRect
).Contains(pos
) == wxInRegion
)
1401 if ( IsDateInRange(m_date
+ wxDateSpan::Month()) )
1403 *date
= m_date
+ wxDateSpan::Month();
1407 *date
= GetUpperDateLimit();
1411 return wxCAL_HITTEST_INCMONTH
;
1416 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1418 int wday
= pos
.x
/ m_widthCol
;
1419 // if ( y < m_heightRow )
1420 if ( y
< (m_heightRow
+ m_rowOffset
) )
1422 if ( y
> m_rowOffset
)
1426 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST
)
1428 wday
= wday
== 6 ? 0 : wday
+ 1;
1431 *wd
= (wxDateTime::WeekDay
)wday
;
1434 return wxCAL_HITTEST_HEADER
;
1438 return wxCAL_HITTEST_NOWHERE
;
1442 // int week = (y - m_heightRow) / m_heightRow;
1443 int week
= (y
- (m_heightRow
+ m_rowOffset
)) / m_heightRow
;
1444 if ( week
>= 6 || wday
>= 7 )
1446 return wxCAL_HITTEST_NOWHERE
;
1449 wxDateTime dt
= GetStartDate() + wxDateSpan::Days(7*week
+ wday
);
1451 if ( IsDateShown(dt
) )
1456 if ( dt
.GetMonth() == m_date
.GetMonth() )
1459 return wxCAL_HITTEST_DAY
;
1463 return wxCAL_HITTEST_SURROUNDING_WEEK
;
1468 return wxCAL_HITTEST_NOWHERE
;
1472 // ----------------------------------------------------------------------------
1473 // subcontrols events handling
1474 // ----------------------------------------------------------------------------
1476 void wxCalendarCtrl::OnMonthChange(wxCommandEvent
& event
)
1478 wxDateTime::Tm tm
= m_date
.GetTm();
1480 wxDateTime::Month mon
= (wxDateTime::Month
)event
.GetInt();
1481 if ( tm
.mday
> wxDateTime::GetNumberOfDays(mon
, tm
.year
) )
1483 tm
.mday
= wxDateTime::GetNumberOfDays(mon
, tm
.year
);
1486 wxDateTime target
= wxDateTime(tm
.mday
, mon
, tm
.year
);
1488 ChangeMonth(&target
);
1489 SetDateAndNotify(target
);
1492 void wxCalendarCtrl::OnYearChange(wxSpinEvent
& event
)
1494 wxDateTime::Tm tm
= m_date
.GetTm();
1496 int year
= (int)event
.GetInt();
1497 if ( tm
.mday
> wxDateTime::GetNumberOfDays(tm
.mon
, year
) )
1499 tm
.mday
= wxDateTime::GetNumberOfDays(tm
.mon
, year
);
1502 wxDateTime target
= wxDateTime(tm
.mday
, tm
.mon
, year
);
1504 if ( ChangeYear(&target
) )
1506 SetDateAndNotify(target
);
1510 // In this case we don't want to change the date. That would put us
1511 // inside the same year but a strange number of months forward/back..
1512 m_spinYear
->SetValue(target
.GetYear());
1517 // ----------------------------------------------------------------------------
1518 // keyboard interface
1519 // ----------------------------------------------------------------------------
1521 void wxCalendarCtrl::OnChar(wxKeyEvent
& event
)
1524 switch ( event
.KeyCode() )
1528 target
= m_date
+ wxDateSpan::Year();
1529 if ( ChangeYear(&target
) )
1531 SetDateAndNotify(target
);
1537 target
= m_date
- wxDateSpan::Year();
1538 if ( ChangeYear(&target
) )
1540 SetDateAndNotify(target
);
1545 target
= m_date
- wxDateSpan::Month();
1546 ChangeMonth(&target
);
1547 SetDateAndNotify(target
); // always
1551 target
= m_date
+ wxDateSpan::Month();
1552 ChangeMonth(&target
);
1553 SetDateAndNotify(target
); // always
1557 if ( event
.ControlDown() )
1559 target
= wxDateTime(m_date
).SetToNextWeekDay(
1560 GetWindowStyle() & wxCAL_MONDAY_FIRST
1561 ? wxDateTime::Sun
: wxDateTime::Sat
);
1562 if ( !IsDateInRange(target
) )
1564 target
= GetUpperDateLimit();
1566 SetDateAndNotify(target
);
1569 SetDateAndNotify(m_date
+ wxDateSpan::Day());
1573 if ( event
.ControlDown() )
1575 target
= wxDateTime(m_date
).SetToPrevWeekDay(
1576 GetWindowStyle() & wxCAL_MONDAY_FIRST
1577 ? wxDateTime::Mon
: wxDateTime::Sun
);
1578 if ( !IsDateInRange(target
) )
1580 target
= GetLowerDateLimit();
1582 SetDateAndNotify(target
);
1585 SetDateAndNotify(m_date
- wxDateSpan::Day());
1589 SetDateAndNotify(m_date
- wxDateSpan::Week());
1593 SetDateAndNotify(m_date
+ wxDateSpan::Week());
1597 if ( event
.ControlDown() )
1598 SetDateAndNotify(wxDateTime::Today());
1600 SetDateAndNotify(wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear()));
1604 SetDateAndNotify(wxDateTime(m_date
).SetToLastMonthDay());
1608 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1616 // ----------------------------------------------------------------------------
1617 // holidays handling
1618 // ----------------------------------------------------------------------------
1620 void wxCalendarCtrl::EnableHolidayDisplay(bool display
)
1622 long style
= GetWindowStyle();
1624 style
|= wxCAL_SHOW_HOLIDAYS
;
1626 style
&= ~wxCAL_SHOW_HOLIDAYS
;
1628 SetWindowStyle(style
);
1633 ResetHolidayAttrs();
1638 void wxCalendarCtrl::SetHolidayAttrs()
1640 if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS
)
1642 ResetHolidayAttrs();
1644 wxDateTime::Tm tm
= m_date
.GetTm();
1645 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
1646 dtEnd
= dtStart
.GetLastMonthDay();
1648 wxDateTimeArray hol
;
1649 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
1651 size_t count
= hol
.GetCount();
1652 for ( size_t n
= 0; n
< count
; n
++ )
1654 SetHoliday(hol
[n
].GetDay());
1659 void wxCalendarCtrl::SetHoliday(size_t day
)
1661 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in SetHoliday") );
1663 wxCalendarDateAttr
*attr
= GetAttr(day
);
1666 attr
= new wxCalendarDateAttr
;
1669 attr
->SetHoliday(TRUE
);
1671 // can't use SetAttr() because it would delete this pointer
1672 m_attrs
[day
- 1] = attr
;
1675 void wxCalendarCtrl::ResetHolidayAttrs()
1677 for ( size_t day
= 0; day
< 31; day
++ )
1681 m_attrs
[day
]->SetHoliday(FALSE
);
1686 // ----------------------------------------------------------------------------
1688 // ----------------------------------------------------------------------------
1690 void wxCalendarEvent::Init()
1692 m_wday
= wxDateTime::Inv_WeekDay
;
1695 wxCalendarEvent::wxCalendarEvent(wxCalendarCtrl
*cal
, wxEventType type
)
1696 : wxCommandEvent(type
, cal
->GetId())
1698 m_date
= cal
->GetDate();
1699 SetEventObject(cal
);
1702 #endif // wxUSE_CALENDARCTRL