1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/dcclient.h"
29 #include "wx/settings.h"
31 #include "wx/combobox.h"
32 #include "wx/listbox.h"
33 #include "wx/stattext.h"
34 #include "wx/textctrl.h"
37 #if wxUSE_CALENDARCTRL
39 #include "wx/spinctrl.h"
41 // if wxDatePickerCtrl code doesn't define the date event, do it here as we
43 #if !wxUSE_DATEPICKCTRL
44 #define _WX_DEFINE_DATE_EVENTS_
47 #include "wx/calctrl.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 BEGIN_EVENT_TABLE(wxCalendarCtrl
, wxControl
)
56 EVT_PAINT(wxCalendarCtrl::OnPaint
)
58 EVT_CHAR(wxCalendarCtrl::OnChar
)
60 EVT_LEFT_DOWN(wxCalendarCtrl::OnClick
)
61 EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick
)
63 EVT_SYS_COLOUR_CHANGED(wxCalendarCtrl::OnSysColourChanged
)
66 #if wxUSE_EXTENDED_RTTI
67 WX_DEFINE_FLAGS( wxCalendarCtrlStyle
)
69 wxBEGIN_FLAGS( wxCalendarCtrlStyle
)
70 // new style border flags, we put them first to
71 // use them for streaming out
72 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
73 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
74 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
75 wxFLAGS_MEMBER(wxBORDER_RAISED
)
76 wxFLAGS_MEMBER(wxBORDER_STATIC
)
77 wxFLAGS_MEMBER(wxBORDER_NONE
)
79 // old style border flags
80 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
81 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
82 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
83 wxFLAGS_MEMBER(wxRAISED_BORDER
)
84 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
85 wxFLAGS_MEMBER(wxBORDER
)
87 // standard window styles
88 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
89 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
90 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
91 wxFLAGS_MEMBER(wxWANTS_CHARS
)
92 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
93 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
94 wxFLAGS_MEMBER(wxVSCROLL
)
95 wxFLAGS_MEMBER(wxHSCROLL
)
97 wxFLAGS_MEMBER(wxCAL_SUNDAY_FIRST
)
98 wxFLAGS_MEMBER(wxCAL_MONDAY_FIRST
)
99 wxFLAGS_MEMBER(wxCAL_SHOW_HOLIDAYS
)
100 wxFLAGS_MEMBER(wxCAL_NO_YEAR_CHANGE
)
101 wxFLAGS_MEMBER(wxCAL_NO_MONTH_CHANGE
)
102 wxFLAGS_MEMBER(wxCAL_SEQUENTIAL_MONTH_SELECTION
)
103 wxFLAGS_MEMBER(wxCAL_SHOW_SURROUNDING_WEEKS
)
105 wxEND_FLAGS( wxCalendarCtrlStyle
)
107 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCalendarCtrl
, wxControl
,"wx/calctrl.h")
109 wxBEGIN_PROPERTIES_TABLE(wxCalendarCtrl
)
110 wxEVENT_RANGE_PROPERTY( Updated
, wxEVT_CALENDAR_SEL_CHANGED
, wxEVT_CALENDAR_WEEKDAY_CLICKED
, wxCalendarEvent
)
111 wxHIDE_PROPERTY( Children
)
112 wxPROPERTY( Date
,wxDateTime
, SetDate
, GetDate
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
113 wxPROPERTY_FLAGS( WindowStyle
, wxCalendarCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
114 wxEND_PROPERTIES_TABLE()
116 wxBEGIN_HANDLERS_TABLE(wxCalendarCtrl
)
117 wxEND_HANDLERS_TABLE()
119 wxCONSTRUCTOR_6( wxCalendarCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxDateTime
, Date
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
121 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl
, wxControl
)
123 IMPLEMENT_DYNAMIC_CLASS(wxCalendarEvent
, wxDateEvent
)
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_SEL_CHANGED
)
130 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DAY_CHANGED
)
131 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_MONTH_CHANGED
)
132 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_YEAR_CHANGED
)
133 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DOUBLECLICKED
)
134 DEFINE_EVENT_TYPE(wxEVT_CALENDAR_WEEKDAY_CLICKED
)
136 // ============================================================================
138 // ============================================================================
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 wxCalendarCtrl::wxCalendarCtrl(wxWindow
*parent
,
146 const wxDateTime
& date
,
150 const wxString
& name
)
154 (void)Create(parent
, id
, date
, pos
, size
, style
, name
);
157 void wxCalendarCtrl::Init()
162 m_staticMonth
= NULL
;
164 m_userChangedYear
= false;
169 wxDateTime::WeekDay wd
;
170 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
172 m_weekdays
[wd
] = wxDateTime::GetWeekDayName(wd
, wxDateTime::Name_Abbr
);
175 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
183 void wxCalendarCtrl::InitColours()
185 m_colHighlightFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
186 m_colHighlightBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
187 m_colBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
188 m_colSorrounding
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
190 m_colHolidayFg
= *wxRED
;
191 // don't set m_colHolidayBg - by default, same as our bg colour
193 m_colHeaderFg
= *wxBLUE
;
194 m_colHeaderBg
= *wxLIGHT_GREY
;
197 bool wxCalendarCtrl::Create(wxWindow
*parent
,
199 const wxDateTime
& date
,
203 const wxString
& name
)
205 if ( !wxControl::Create(parent
, id
, pos
, size
,
206 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
| wxFULL_REPAINT_ON_RESIZE
,
207 wxDefaultValidator
, name
) )
212 // needed to get the arrow keys normally used for the dialog navigation
213 SetWindowStyle(style
| wxWANTS_CHARS
);
215 m_date
= date
.IsValid() ? date
: wxDateTime::Today();
217 m_lowdate
= wxDefaultDateTime
;
218 m_highdate
= wxDefaultDateTime
;
220 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
222 CreateYearSpinCtrl();
223 m_staticYear
= new wxStaticText(GetParent(), wxID_ANY
, m_date
.Format(_T("%Y")),
224 wxDefaultPosition
, wxDefaultSize
,
227 CreateMonthComboBox();
228 m_staticMonth
= new wxStaticText(GetParent(), wxID_ANY
, m_date
.Format(_T("%B")),
229 wxDefaultPosition
, wxDefaultSize
,
233 ShowCurrentControls();
235 // we need to set the position as well because the main control position
236 // is not the same as the one specified in pos if we have the controls
241 // Since we don't paint the whole background make sure that the platform
242 // will use the right one.
243 SetBackgroundColour(m_colBackground
);
250 wxCalendarCtrl::~wxCalendarCtrl()
252 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
258 // ----------------------------------------------------------------------------
259 // Create the wxComboBox and wxSpinCtrl
260 // ----------------------------------------------------------------------------
262 void wxCalendarCtrl::CreateMonthComboBox()
264 m_comboMonth
= new wxComboBox(GetParent(), wxID_ANY
,
269 wxCB_READONLY
| wxCLIP_SIBLINGS
);
272 for ( m
= wxDateTime::Jan
; m
< wxDateTime::Inv_Month
; wxNextMonth(m
) )
274 m_comboMonth
->Append(wxDateTime::GetMonthName(m
));
277 m_comboMonth
->SetSelection(GetDate().GetMonth());
278 m_comboMonth
->SetSize(wxDefaultCoord
,
282 wxSIZE_AUTO_WIDTH
|wxSIZE_AUTO_HEIGHT
);
284 m_comboMonth
->Connect(wxEVT_COMMAND_COMBOBOX_SELECTED
,
285 wxCommandEventHandler(wxCalendarCtrl::OnMonthChange
),
289 void wxCalendarCtrl::CreateYearSpinCtrl()
291 m_spinYear
= new wxSpinCtrl(GetParent(), wxID_ANY
,
292 GetDate().Format(_T("%Y")),
295 wxSP_ARROW_KEYS
| wxCLIP_SIBLINGS
,
296 -4300, 10000, GetDate().GetYear());
298 m_spinYear
->Connect(wxEVT_COMMAND_TEXT_UPDATED
,
299 wxCommandEventHandler(wxCalendarCtrl::OnYearTextChange
),
302 m_spinYear
->Connect(wxEVT_COMMAND_SPINCTRL_UPDATED
,
303 wxCommandEventHandler(wxCalendarCtrl::OnYearChange
),
307 // ----------------------------------------------------------------------------
308 // forward wxWin functions to subcontrols
309 // ----------------------------------------------------------------------------
311 bool wxCalendarCtrl::Destroy()
314 m_staticYear
->Destroy();
316 m_spinYear
->Destroy();
318 m_comboMonth
->Destroy();
320 m_staticMonth
->Destroy();
325 m_staticMonth
= NULL
;
327 return wxControl::Destroy();
330 bool wxCalendarCtrl::Show(bool show
)
332 if ( !wxControl::Show(show
) )
337 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
339 if ( GetMonthControl() )
341 GetMonthControl()->Show(show
);
342 GetYearControl()->Show(show
);
349 bool wxCalendarCtrl::Enable(bool enable
)
351 if ( !wxControl::Enable(enable
) )
356 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
358 GetMonthControl()->Enable(enable
);
359 GetYearControl()->Enable(enable
);
365 // ----------------------------------------------------------------------------
366 // enable/disable month/year controls
367 // ----------------------------------------------------------------------------
369 void wxCalendarCtrl::ShowCurrentControls()
371 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
373 if ( AllowMonthChange() )
375 m_comboMonth
->Show();
376 m_staticMonth
->Hide();
378 if ( AllowYearChange() )
381 m_staticYear
->Hide();
389 m_comboMonth
->Hide();
390 m_staticMonth
->Show();
393 // year change not allowed here
395 m_staticYear
->Show();
399 wxControl
*wxCalendarCtrl::GetMonthControl() const
401 return AllowMonthChange() ? (wxControl
*)m_comboMonth
: (wxControl
*)m_staticMonth
;
404 wxControl
*wxCalendarCtrl::GetYearControl() const
406 return AllowYearChange() ? (wxControl
*)m_spinYear
: (wxControl
*)m_staticYear
;
409 void wxCalendarCtrl::EnableYearChange(bool enable
)
411 if ( enable
!= AllowYearChange() )
413 long style
= GetWindowStyle();
415 style
&= ~wxCAL_NO_YEAR_CHANGE
;
417 style
|= wxCAL_NO_YEAR_CHANGE
;
418 SetWindowStyle(style
);
420 ShowCurrentControls();
421 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
428 void wxCalendarCtrl::EnableMonthChange(bool enable
)
430 if ( enable
!= AllowMonthChange() )
432 long style
= GetWindowStyle();
434 style
&= ~wxCAL_NO_MONTH_CHANGE
;
436 style
|= wxCAL_NO_MONTH_CHANGE
;
437 SetWindowStyle(style
);
439 ShowCurrentControls();
440 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
447 // ----------------------------------------------------------------------------
449 // ----------------------------------------------------------------------------
451 bool wxCalendarCtrl::SetDate(const wxDateTime
& date
)
455 bool sameMonth
= m_date
.GetMonth() == date
.GetMonth(),
456 sameYear
= m_date
.GetYear() == date
.GetYear();
458 if ( IsDateInRange(date
) )
460 if ( sameMonth
&& sameYear
)
462 // just change the day
467 if ( AllowMonthChange() && (AllowYearChange() || sameYear
) )
472 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
474 // update the controls
475 m_comboMonth
->SetSelection(m_date
.GetMonth());
477 if ( AllowYearChange() )
479 if ( !m_userChangedYear
)
480 m_spinYear
->SetValue(m_date
.Format(_T("%Y")));
484 // as the month changed, holidays did too
487 // update the calendar
498 m_userChangedYear
= false;
503 void wxCalendarCtrl::ChangeDay(const wxDateTime
& date
)
505 if ( m_date
!= date
)
507 // we need to refresh the row containing the old date and the one
508 // containing the new one
509 wxDateTime dateOld
= m_date
;
512 RefreshDate(dateOld
);
514 // if the date is in the same row, it was already drawn correctly
515 if ( GetWeek(m_date
) != GetWeek(dateOld
) )
522 void wxCalendarCtrl::SetDateAndNotify(const wxDateTime
& date
)
524 wxDateTime::Tm tm1
= m_date
.GetTm(),
528 if ( tm1
.year
!= tm2
.year
)
529 type
= wxEVT_CALENDAR_YEAR_CHANGED
;
530 else if ( tm1
.mon
!= tm2
.mon
)
531 type
= wxEVT_CALENDAR_MONTH_CHANGED
;
532 else if ( tm1
.mday
!= tm2
.mday
)
533 type
= wxEVT_CALENDAR_DAY_CHANGED
;
539 GenerateEvents(type
, wxEVT_CALENDAR_SEL_CHANGED
);
543 // ----------------------------------------------------------------------------
545 // ----------------------------------------------------------------------------
547 bool wxCalendarCtrl::SetLowerDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
551 if ( !(date
.IsValid()) || ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : true ) )
563 bool wxCalendarCtrl::SetUpperDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
567 if ( !(date
.IsValid()) || ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : true ) )
579 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& lowerdate
/* = wxDefaultDateTime */, const wxDateTime
& upperdate
/* = wxDefaultDateTime */)
584 ( !( lowerdate
.IsValid() ) || ( ( upperdate
.IsValid() ) ? ( lowerdate
<= upperdate
) : true ) ) &&
585 ( !( upperdate
.IsValid() ) || ( ( lowerdate
.IsValid() ) ? ( upperdate
>= lowerdate
) : true ) ) )
587 m_lowdate
= lowerdate
;
588 m_highdate
= upperdate
;
598 // ----------------------------------------------------------------------------
600 // ----------------------------------------------------------------------------
602 wxDateTime
wxCalendarCtrl::GetStartDate() const
604 wxDateTime::Tm tm
= m_date
.GetTm();
606 wxDateTime date
= wxDateTime(1, tm
.mon
, tm
.year
);
609 date
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
610 ? wxDateTime::Mon
: wxDateTime::Sun
);
612 if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
)
614 // We want to offset the calendar if we start on the first..
615 if ( date
.GetDay() == 1 )
617 date
-= wxDateSpan::Week();
624 bool wxCalendarCtrl::IsDateShown(const wxDateTime
& date
) const
626 if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
628 return date
.GetMonth() == m_date
.GetMonth();
636 bool wxCalendarCtrl::IsDateInRange(const wxDateTime
& date
) const
638 // Check if the given date is in the range specified
639 return ( ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : true )
640 && ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : true ) );
643 bool wxCalendarCtrl::ChangeYear(wxDateTime
* target
) const
647 if ( !(IsDateInRange(*target
)) )
649 if ( target
->GetYear() < m_date
.GetYear() )
651 if ( target
->GetYear() >= GetLowerDateLimit().GetYear() )
653 *target
= GetLowerDateLimit();
663 if ( target
->GetYear() <= GetUpperDateLimit().GetYear() )
665 *target
= GetUpperDateLimit();
682 bool wxCalendarCtrl::ChangeMonth(wxDateTime
* target
) const
686 if ( !(IsDateInRange(*target
)) )
690 if ( target
->GetMonth() < m_date
.GetMonth() )
692 *target
= GetLowerDateLimit();
696 *target
= GetUpperDateLimit();
703 size_t wxCalendarCtrl::GetWeek(const wxDateTime
& date
) const
705 size_t retval
= date
.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
706 ? wxDateTime::Monday_First
707 : wxDateTime::Sunday_First
);
709 if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
711 // we need to offset an extra week if we "start" on the 1st of the month
712 wxDateTime::Tm tm
= date
.GetTm();
714 wxDateTime datetest
= wxDateTime(1, tm
.mon
, tm
.year
);
717 datetest
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
718 ? wxDateTime::Mon
: wxDateTime::Sun
);
720 if ( datetest
.GetDay() == 1 )
729 // ----------------------------------------------------------------------------
731 // ----------------------------------------------------------------------------
733 // this is a composite control and it must arrange its parts each time its
734 // size or position changes: the combobox and spinctrl are along the top of
735 // the available area and the calendar takes up therest of the space
737 // the static controls are supposed to be always smaller than combo/spin so we
738 // always use the latter for size calculations and position the static to take
741 // the constants used for the layout
742 #define VERT_MARGIN 5 // distance between combo and calendar
744 #define HORZ_MARGIN 5 // spin
746 #define HORZ_MARGIN 15 // spin
748 wxSize
wxCalendarCtrl::DoGetBestSize() const
750 // calc the size of the calendar
751 ((wxCalendarCtrl
*)this)->RecalcGeometry(); // const_cast
753 wxCoord width
= 7*m_widthCol
,
754 height
= 7*m_heightRow
+ m_rowOffset
+ VERT_MARGIN
;
756 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
758 // the combobox doesn't report its height correctly (it returns the
759 // height including the drop down list) so don't use it
760 height
+= m_spinYear
->GetBestSize().y
;
763 wxCoord w2
= m_comboMonth
->GetBestSize().x
+ HORZ_MARGIN
+ GetCharWidth()*6;
768 if ( !HasFlag(wxBORDER_NONE
) )
770 // the border would clip the last line otherwise
775 wxSize
best(width
, height
);
780 void wxCalendarCtrl::DoSetSize(int x
, int y
,
781 int width
, int height
,
784 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
787 void wxCalendarCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
791 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) && m_staticMonth
)
793 wxSize sizeCombo
= m_comboMonth
->GetSize();
794 wxSize sizeStatic
= m_staticMonth
->GetSize();
795 wxSize sizeSpin
= m_spinYear
->GetSize();
796 int dy
= (sizeCombo
.y
- sizeStatic
.y
) / 2;
797 m_comboMonth
->Move(x
, y
);
798 m_staticMonth
->SetSize(x
, y
+ dy
, sizeCombo
.x
, sizeStatic
.y
);
800 int xDiff
= sizeCombo
.x
+ HORZ_MARGIN
;
802 m_spinYear
->SetSize(x
+ xDiff
, y
, width
- xDiff
, sizeCombo
.y
);
803 m_staticYear
->SetSize(x
+ xDiff
, y
+ dy
, width
- xDiff
, sizeStatic
.y
);
805 yDiff
= wxMax(sizeSpin
.y
, sizeCombo
.y
) + VERT_MARGIN
;
807 else // no controls on the top
812 wxControl::DoMoveWindow(x
, y
+ yDiff
, width
, height
- yDiff
);
815 void wxCalendarCtrl::DoGetPosition(int *x
, int *y
) const
817 wxControl::DoGetPosition(x
, y
);
819 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) && GetMonthControl() )
821 // our real top corner is not in this position
824 *y
-= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
829 void wxCalendarCtrl::DoGetSize(int *width
, int *height
) const
831 wxControl::DoGetSize(width
, height
);
833 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
835 // our real height is bigger
836 if ( height
&& GetMonthControl())
838 *height
+= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
843 void wxCalendarCtrl::RecalcGeometry()
847 dc
.SetFont(GetFont());
849 // determine the column width (weekday names are not necessarily wider
850 // than the numbers (in some languages), so let's not assume that they are)
852 for ( int day
= 10; day
<= 31; day
++)
855 dc
.GetTextExtent(wxString::Format(wxT("%d"), day
), &width
, &m_heightRow
);
856 if ( width
> m_widthCol
)
858 // 1.5 times the width gives nice margins even if the weekday
860 m_widthCol
= width
+width
/2;
863 wxDateTime::WeekDay wd
;
864 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
867 dc
.GetTextExtent(m_weekdays
[wd
], &width
, &m_heightRow
);
868 if ( width
> m_widthCol
)
874 // leave some margins
878 m_rowOffset
= (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) ? m_heightRow
: 0; // conditional in relation to style
881 // ----------------------------------------------------------------------------
883 // ----------------------------------------------------------------------------
885 void wxCalendarCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
889 dc
.SetFont(GetFont());
894 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
895 m_date
.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
900 wxCoord x0
= wxMax( (GetSize().x
- m_widthCol
*7) /2 , 0 );
902 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
904 // draw the sequential month-selector
906 dc
.SetBackgroundMode(wxTRANSPARENT
);
907 dc
.SetTextForeground(*wxBLACK
);
908 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
909 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
910 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
912 // Get extent of month-name + year
913 wxCoord monthw
, monthh
;
914 wxString headertext
= m_date
.Format(wxT("%B %Y"));
915 dc
.GetTextExtent(headertext
, &monthw
, &monthh
);
917 // draw month-name centered above weekdays
918 wxCoord monthx
= ((m_widthCol
* 7) - monthw
) / 2 + x0
;
919 wxCoord monthy
= ((m_heightRow
- monthh
) / 2) + y
;
920 dc
.DrawText(headertext
, monthx
, monthy
);
922 // calculate the "month-arrows"
923 wxPoint leftarrow
[3];
924 wxPoint rightarrow
[3];
926 int arrowheight
= monthh
/ 2;
928 leftarrow
[0] = wxPoint(0, arrowheight
/ 2);
929 leftarrow
[1] = wxPoint(arrowheight
/ 2, 0);
930 leftarrow
[2] = wxPoint(arrowheight
/ 2, arrowheight
- 1);
932 rightarrow
[0] = wxPoint(0,0);
933 rightarrow
[1] = wxPoint(arrowheight
/ 2, arrowheight
/ 2);
934 rightarrow
[2] = wxPoint(0, arrowheight
- 1);
936 // draw the "month-arrows"
938 wxCoord arrowy
= (m_heightRow
- arrowheight
) / 2;
939 wxCoord larrowx
= (m_widthCol
- (arrowheight
/ 2)) / 2 + x0
;
940 wxCoord rarrowx
= ((m_widthCol
- (arrowheight
/ 2)) / 2) + m_widthCol
*6 + x0
;
941 m_leftArrowRect
= m_rightArrowRect
= wxRect(0,0,0,0);
943 if ( AllowMonthChange() )
945 wxDateTime ldpm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) - wxDateSpan::Day(); // last day prev month
946 // Check if range permits change
947 if ( IsDateInRange(ldpm
) && ( ( ldpm
.GetYear() == m_date
.GetYear() ) ? true : AllowYearChange() ) )
949 m_leftArrowRect
= wxRect(larrowx
- 3, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
950 dc
.SetBrush(*wxBLACK_BRUSH
);
951 dc
.SetPen(*wxBLACK_PEN
);
952 dc
.DrawPolygon(3, leftarrow
, larrowx
, arrowy
, wxWINDING_RULE
);
953 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
954 dc
.DrawRectangle(m_leftArrowRect
);
956 wxDateTime fdnm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) + wxDateSpan::Month(); // first day next month
957 if ( IsDateInRange(fdnm
) && ( ( fdnm
.GetYear() == m_date
.GetYear() ) ? true : AllowYearChange() ) )
959 m_rightArrowRect
= wxRect(rarrowx
- 4, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
960 dc
.SetBrush(*wxBLACK_BRUSH
);
961 dc
.SetPen(*wxBLACK_PEN
);
962 dc
.DrawPolygon(3, rightarrow
, rarrowx
, arrowy
, wxWINDING_RULE
);
963 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
964 dc
.DrawRectangle(m_rightArrowRect
);
971 // first draw the week days
972 if ( IsExposed(x0
, y
, x0
+ 7*m_widthCol
, m_heightRow
) )
975 wxLogDebug("painting the header");
978 dc
.SetBackgroundMode(wxTRANSPARENT
);
979 dc
.SetTextForeground(m_colHeaderFg
);
980 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
981 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
982 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
984 bool startOnMonday
= (GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
985 for ( int wd
= 0; wd
< 7; wd
++ )
989 n
= wd
== 6 ? 0 : wd
+ 1;
993 dc
.GetTextExtent(m_weekdays
[n
], &dayw
, &dayh
);
994 dc
.DrawText(m_weekdays
[n
], x0
+ (wd
*m_widthCol
) + ((m_widthCol
- dayw
) / 2), y
); // center the day-name
998 // then the calendar itself
999 dc
.SetTextForeground(*wxBLACK
);
1000 //dc.SetFont(*wxNORMAL_FONT);
1003 wxDateTime date
= GetStartDate();
1006 wxLogDebug("starting calendar from %s\n",
1007 date
.Format("%a %d-%m-%Y %H:%M:%S").c_str());
1010 dc
.SetBackgroundMode(wxSOLID
);
1011 for ( size_t nWeek
= 1; nWeek
<= 6; nWeek
++, y
+= m_heightRow
)
1013 // if the update region doesn't intersect this row, don't paint it
1014 if ( !IsExposed(x0
, y
, x0
+ 7*m_widthCol
, m_heightRow
- 1) )
1016 date
+= wxDateSpan::Week();
1022 wxLogDebug("painting week %d at y = %d\n", nWeek
, y
);
1025 for ( int wd
= 0; wd
< 7; wd
++ )
1027 dc
.SetTextBackground(m_colBackground
);
1028 if ( IsDateShown(date
) )
1030 // don't use wxDate::Format() which prepends 0s
1031 unsigned int day
= date
.GetDay();
1032 wxString dayStr
= wxString::Format(_T("%u"), day
);
1034 dc
.GetTextExtent(dayStr
, &width
, (wxCoord
*)NULL
);
1036 bool changedColours
= false,
1037 changedFont
= false;
1040 wxCalendarDateAttr
*attr
= NULL
;
1042 if ( date
.GetMonth() != m_date
.GetMonth() || !IsDateInRange(date
) )
1044 // surrounding week or out-of-range
1046 dc
.SetTextForeground(m_colSorrounding
);
1047 changedColours
= true;
1051 isSel
= date
.IsSameDate(m_date
);
1052 attr
= m_attrs
[day
- 1];
1056 dc
.SetTextForeground(m_colHighlightFg
);
1057 dc
.SetTextBackground(m_colHighlightBg
);
1059 changedColours
= true;
1063 wxColour colFg
, colBg
;
1065 if ( attr
->IsHoliday() )
1067 colFg
= m_colHolidayFg
;
1068 colBg
= m_colHolidayBg
;
1072 colFg
= attr
->GetTextColour();
1073 colBg
= attr
->GetBackgroundColour();
1078 dc
.SetTextForeground(colFg
);
1079 changedColours
= true;
1084 dc
.SetTextBackground(colBg
);
1085 changedColours
= true;
1088 if ( attr
->HasFont() )
1090 dc
.SetFont(attr
->GetFont());
1096 wxCoord x
= wd
*m_widthCol
+ (m_widthCol
- width
) / 2 + x0
;
1097 dc
.DrawText(dayStr
, x
, y
+ 1);
1099 if ( !isSel
&& attr
&& attr
->HasBorder() )
1102 if ( attr
->HasBorderColour() )
1104 colBorder
= attr
->GetBorderColour();
1108 colBorder
= GetForegroundColour();
1111 wxPen
pen(colBorder
, 1, wxSOLID
);
1113 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1115 switch ( attr
->GetBorder() )
1117 case wxCAL_BORDER_SQUARE
:
1118 dc
.DrawRectangle(x
- 2, y
,
1119 width
+ 4, m_heightRow
);
1122 case wxCAL_BORDER_ROUND
:
1123 dc
.DrawEllipse(x
- 2, y
,
1124 width
+ 4, m_heightRow
);
1128 wxFAIL_MSG(_T("unknown border type"));
1132 if ( changedColours
)
1134 dc
.SetTextForeground(GetForegroundColour());
1135 dc
.SetTextBackground(GetBackgroundColour());
1140 dc
.SetFont(GetFont());
1143 //else: just don't draw it
1145 date
+= wxDateSpan::Day();
1149 // Greying out out-of-range background
1150 bool showSurrounding
= (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) != 0;
1152 date
= ( showSurrounding
) ? GetStartDate() : wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear());
1153 if ( !IsDateInRange(date
) )
1155 wxDateTime firstOOR
= GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1157 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1158 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1160 HighlightRange(&dc
, date
, firstOOR
, wxTRANSPARENT_PEN
, &oorbrush
);
1163 date
= ( showSurrounding
) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear());
1164 if ( !IsDateInRange(date
) )
1166 wxDateTime firstOOR
= GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1168 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1169 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1171 HighlightRange(&dc
, firstOOR
, date
, wxTRANSPARENT_PEN
, &oorbrush
);
1175 wxLogDebug("+++ finished painting");
1179 void wxCalendarCtrl::RefreshDate(const wxDateTime
& date
)
1185 // always refresh the whole row at once because our OnPaint() will draw
1186 // the whole row anyhow - and this allows the small optimisation in
1187 // OnClick() below to work
1188 rect
.x
= wxMax( (GetSize().x
- m_widthCol
*7) /2 , 0 );
1190 rect
.y
= (m_heightRow
* GetWeek(date
)) + m_rowOffset
;
1192 rect
.width
= 7*m_widthCol
;
1193 rect
.height
= m_heightRow
;
1196 // VZ: for some reason, the selected date seems to occupy more space under
1197 // MSW - this is probably some bug in the font size calculations, but I
1198 // don't know where exactly. This fix is ugly and leads to more
1199 // refreshes than really needed, but without it the selected days
1200 // leaves even more ugly underscores on screen.
1205 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
1208 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1211 Refresh(true, &rect
);
1214 void wxCalendarCtrl::HighlightRange(wxPaintDC
* pDC
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, const wxPen
* pPen
, const wxBrush
* pBrush
)
1216 // Highlights the given range using pen and brush
1217 // Does nothing if todate < fromdate
1221 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate
.Format("%d %m %Y"), todate
.Format("%d %m %Y"));
1224 if ( todate
>= fromdate
)
1231 // implicit: both dates must be currently shown - checked by GetDateCoord
1232 if ( GetDateCoord(fromdate
, &fd
, &fw
) && GetDateCoord(todate
, &td
, &tw
) )
1235 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd
, fw
, td
, tw
);
1237 if ( ( (tw
- fw
) == 1 ) && ( td
< fd
) )
1239 // special case: interval 7 days or less not in same week
1240 // split in two separate intervals
1241 wxDateTime tfd
= fromdate
+ wxDateSpan::Days(7-fd
);
1242 wxDateTime ftd
= tfd
+ wxDateSpan::Day();
1244 wxLogDebug("Highlight: Separate segments");
1247 HighlightRange(pDC
, fromdate
, tfd
, pPen
, pBrush
);
1248 HighlightRange(pDC
, ftd
, todate
, pPen
, pBrush
);
1253 wxPoint corners
[8]; // potentially 8 corners in polygon
1254 wxCoord x0
= wxMax( (GetSize().x
- m_widthCol
*7) /2 , 0 );
1258 // simple case: same week
1260 corners
[0] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
);
1261 corners
[1] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, ((fw
+ 1 ) * m_heightRow
) + m_rowOffset
);
1262 corners
[2] = wxPoint(x0
+ td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
);
1263 corners
[3] = wxPoint(x0
+ td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
);
1268 // "complex" polygon
1269 corners
[cidx
] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1273 corners
[cidx
] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1274 corners
[cidx
] = wxPoint(x0
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1277 corners
[cidx
] = wxPoint(x0
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1278 corners
[cidx
] = wxPoint(x0
+ td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1282 corners
[cidx
] = wxPoint(x0
+ td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1283 corners
[cidx
] = wxPoint(x0
+ 7 * m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1286 corners
[cidx
] = wxPoint(x0
+ 7 * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1292 pDC
->SetBrush(*pBrush
);
1294 pDC
->DrawPolygon(numpoints
, corners
);
1300 wxLogDebug("--- HighlightRange ---");
1304 bool wxCalendarCtrl::GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const
1309 wxLogDebug("+++ GetDateCoord: (%s) +++", date
.Format("%d %m %Y"));
1312 if ( IsDateShown(date
) )
1314 bool startOnMonday
= ( GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1317 *day
= date
.GetWeekDay();
1319 if ( *day
== 0 ) // sunday
1321 *day
= ( startOnMonday
) ? 7 : 1;
1325 *day
+= ( startOnMonday
) ? 0 : 1;
1328 int targetmonth
= date
.GetMonth() + (12 * date
.GetYear());
1329 int thismonth
= m_date
.GetMonth() + (12 * m_date
.GetYear());
1332 if ( targetmonth
== thismonth
)
1334 *week
= GetWeek(date
);
1338 if ( targetmonth
< thismonth
)
1340 *week
= 1; // trivial
1342 else // targetmonth > thismonth
1348 // get the datecoord of the last day in the month currently shown
1350 wxLogDebug(" +++ LDOM +++");
1352 GetDateCoord(ldcm
.SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear()), &lastday
, &lastweek
);
1354 wxLogDebug(" --- LDOM ---");
1357 wxTimeSpan span
= date
- ldcm
;
1359 int daysfromlast
= span
.GetDays();
1361 wxLogDebug("daysfromlast: %i", daysfromlast
);
1363 if ( daysfromlast
+ lastday
> 7 ) // past week boundary
1365 int wholeweeks
= (daysfromlast
/ 7);
1366 *week
= wholeweeks
+ lastweek
;
1367 if ( (daysfromlast
- (7 * wholeweeks
) + lastday
) > 7 )
1387 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date
.Format("%d %m %Y"), *day
, *week
);
1393 // ----------------------------------------------------------------------------
1395 // ----------------------------------------------------------------------------
1397 void wxCalendarCtrl::OnDClick(wxMouseEvent
& event
)
1399 if ( HitTest(event
.GetPosition()) != wxCAL_HITTEST_DAY
)
1405 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1409 void wxCalendarCtrl::OnClick(wxMouseEvent
& event
)
1412 wxDateTime::WeekDay wday
;
1413 switch ( HitTest(event
.GetPosition(), &date
, &wday
) )
1415 case wxCAL_HITTEST_DAY
:
1416 if ( IsDateInRange(date
) )
1420 GenerateEvents(wxEVT_CALENDAR_DAY_CHANGED
,
1421 wxEVT_CALENDAR_SEL_CHANGED
);
1425 case wxCAL_HITTEST_HEADER
:
1427 wxCalendarEvent
eventWd(this, wxEVT_CALENDAR_WEEKDAY_CLICKED
);
1428 eventWd
.m_wday
= wday
;
1429 (void)GetEventHandler()->ProcessEvent(eventWd
);
1433 case wxCAL_HITTEST_DECMONTH
:
1434 case wxCAL_HITTEST_INCMONTH
:
1435 case wxCAL_HITTEST_SURROUNDING_WEEK
:
1436 SetDateAndNotify(date
); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1440 wxFAIL_MSG(_T("unknown hittest code"));
1443 case wxCAL_HITTEST_NOWHERE
:
1449 wxCalendarHitTestResult
wxCalendarCtrl::HitTest(const wxPoint
& pos
,
1451 wxDateTime::WeekDay
*wd
)
1454 // use the correct x-pos
1455 wxCoord x0
= wxMax((GetSize().x
- m_widthCol
*7) /2, 0);
1456 wxPoint pos_corr
= pos
;
1459 wxCoord y
= pos_corr
.y
;
1461 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1462 if ( (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
1466 // we need to find out if the hit is on left arrow, on month or on right arrow
1468 if ( wxRegion(m_leftArrowRect
).Contains(pos_corr
) == wxInRegion
)
1472 if ( IsDateInRange(m_date
- wxDateSpan::Month()) )
1474 *date
= m_date
- wxDateSpan::Month();
1478 *date
= GetLowerDateLimit();
1482 return wxCAL_HITTEST_DECMONTH
;
1485 if ( wxRegion(m_rightArrowRect
).Contains(pos_corr
) == wxInRegion
)
1489 if ( IsDateInRange(m_date
+ wxDateSpan::Month()) )
1491 *date
= m_date
+ wxDateSpan::Month();
1495 *date
= GetUpperDateLimit();
1499 return wxCAL_HITTEST_INCMONTH
;
1504 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1507 int wday
= pos_corr
.x
/ m_widthCol
;
1508 // if ( y < m_heightRow )
1509 if ( y
< (m_heightRow
+ m_rowOffset
) )
1511 if ( y
> m_rowOffset
)
1515 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST
)
1517 wday
= wday
== 6 ? 0 : wday
+ 1;
1520 *wd
= (wxDateTime::WeekDay
)wday
;
1523 return wxCAL_HITTEST_HEADER
;
1527 return wxCAL_HITTEST_NOWHERE
;
1531 // int week = (y - m_heightRow) / m_heightRow;
1532 int week
= (y
- (m_heightRow
+ m_rowOffset
)) / m_heightRow
;
1533 if ( week
>= 6 || wday
>= 7 )
1535 return wxCAL_HITTEST_NOWHERE
;
1538 wxDateTime dt
= GetStartDate() + wxDateSpan::Days(7*week
+ wday
);
1540 if ( IsDateShown(dt
) )
1545 if ( dt
.GetMonth() == m_date
.GetMonth() )
1548 return wxCAL_HITTEST_DAY
;
1552 return wxCAL_HITTEST_SURROUNDING_WEEK
;
1557 return wxCAL_HITTEST_NOWHERE
;
1561 // ----------------------------------------------------------------------------
1562 // subcontrols events handling
1563 // ----------------------------------------------------------------------------
1565 void wxCalendarCtrl::OnMonthChange(wxCommandEvent
& event
)
1567 wxDateTime::Tm tm
= m_date
.GetTm();
1569 wxDateTime::Month mon
= (wxDateTime::Month
)event
.GetInt();
1570 if ( tm
.mday
> wxDateTime::GetNumberOfDays(mon
, tm
.year
) )
1572 tm
.mday
= wxDateTime::GetNumberOfDays(mon
, tm
.year
);
1575 wxDateTime target
= wxDateTime(tm
.mday
, mon
, tm
.year
);
1577 ChangeMonth(&target
);
1578 SetDateAndNotify(target
);
1581 void wxCalendarCtrl::OnYearChange(wxCommandEvent
& event
)
1583 int year
= (int)event
.GetInt();
1584 if ( year
== INT_MIN
)
1586 // invalid year in the spin control, ignore it
1590 wxDateTime::Tm tm
= m_date
.GetTm();
1592 if ( tm
.mday
> wxDateTime::GetNumberOfDays(tm
.mon
, year
) )
1594 tm
.mday
= wxDateTime::GetNumberOfDays(tm
.mon
, year
);
1597 wxDateTime target
= wxDateTime(tm
.mday
, tm
.mon
, year
);
1599 if ( ChangeYear(&target
) )
1601 SetDateAndNotify(target
);
1605 // In this case we don't want to change the date. That would put us
1606 // inside the same year but a strange number of months forward/back..
1607 m_spinYear
->SetValue(target
.GetYear());
1611 void wxCalendarCtrl::OnYearTextChange(wxCommandEvent
& event
)
1613 SetUserChangedYear();
1614 OnYearChange(event
);
1617 // Responds to colour changes, and passes event on to children.
1618 void wxCalendarCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1623 // Propagate the event to the children
1624 wxControl::OnSysColourChanged(event
);
1626 // Redraw control area
1627 SetBackgroundColour(m_colBackground
);
1631 // ----------------------------------------------------------------------------
1632 // keyboard interface
1633 // ----------------------------------------------------------------------------
1635 void wxCalendarCtrl::OnChar(wxKeyEvent
& event
)
1638 switch ( event
.GetKeyCode() )
1642 target
= m_date
+ wxDateSpan::Year();
1643 if ( ChangeYear(&target
) )
1645 SetDateAndNotify(target
);
1651 target
= m_date
- wxDateSpan::Year();
1652 if ( ChangeYear(&target
) )
1654 SetDateAndNotify(target
);
1659 target
= m_date
- wxDateSpan::Month();
1660 ChangeMonth(&target
);
1661 SetDateAndNotify(target
); // always
1665 target
= m_date
+ wxDateSpan::Month();
1666 ChangeMonth(&target
);
1667 SetDateAndNotify(target
); // always
1671 if ( event
.ControlDown() )
1673 target
= wxDateTime(m_date
).SetToNextWeekDay(
1674 GetWindowStyle() & wxCAL_MONDAY_FIRST
1675 ? wxDateTime::Sun
: wxDateTime::Sat
);
1676 if ( !IsDateInRange(target
) )
1678 target
= GetUpperDateLimit();
1680 SetDateAndNotify(target
);
1683 SetDateAndNotify(m_date
+ wxDateSpan::Day());
1687 if ( event
.ControlDown() )
1689 target
= wxDateTime(m_date
).SetToPrevWeekDay(
1690 GetWindowStyle() & wxCAL_MONDAY_FIRST
1691 ? wxDateTime::Mon
: wxDateTime::Sun
);
1692 if ( !IsDateInRange(target
) )
1694 target
= GetLowerDateLimit();
1696 SetDateAndNotify(target
);
1699 SetDateAndNotify(m_date
- wxDateSpan::Day());
1703 SetDateAndNotify(m_date
- wxDateSpan::Week());
1707 SetDateAndNotify(m_date
+ wxDateSpan::Week());
1711 if ( event
.ControlDown() )
1712 SetDateAndNotify(wxDateTime::Today());
1714 SetDateAndNotify(wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear()));
1718 SetDateAndNotify(wxDateTime(m_date
).SetToLastMonthDay());
1722 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1730 // ----------------------------------------------------------------------------
1731 // holidays handling
1732 // ----------------------------------------------------------------------------
1734 void wxCalendarCtrl::EnableHolidayDisplay(bool display
)
1736 long style
= GetWindowStyle();
1738 style
|= wxCAL_SHOW_HOLIDAYS
;
1740 style
&= ~wxCAL_SHOW_HOLIDAYS
;
1742 SetWindowStyle(style
);
1747 ResetHolidayAttrs();
1752 void wxCalendarCtrl::SetHolidayAttrs()
1754 if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS
)
1756 ResetHolidayAttrs();
1758 wxDateTime::Tm tm
= m_date
.GetTm();
1759 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
1760 dtEnd
= dtStart
.GetLastMonthDay();
1762 wxDateTimeArray hol
;
1763 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
1765 size_t count
= hol
.GetCount();
1766 for ( size_t n
= 0; n
< count
; n
++ )
1768 SetHoliday(hol
[n
].GetDay());
1773 void wxCalendarCtrl::SetHoliday(size_t day
)
1775 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in SetHoliday") );
1777 wxCalendarDateAttr
*attr
= GetAttr(day
);
1780 attr
= new wxCalendarDateAttr
;
1783 attr
->SetHoliday(true);
1785 // can't use SetAttr() because it would delete this pointer
1786 m_attrs
[day
- 1] = attr
;
1789 void wxCalendarCtrl::ResetHolidayAttrs()
1791 for ( size_t day
= 0; day
< 31; day
++ )
1795 m_attrs
[day
]->SetHoliday(false);
1803 wxCalendarCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
1805 // Use the same color scheme as wxListBox
1806 return wxListBox::GetClassDefaultAttributes(variant
);
1809 #endif // wxUSE_CALENDARCTRL