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 // set the style first to avoid assert in our SetWindowStyleFlag()
206 m_windowStyle
= style
;
208 if ( !wxControl::Create(parent
, id
, pos
, size
,
209 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
| wxFULL_REPAINT_ON_RESIZE
,
210 wxDefaultValidator
, name
) )
215 // needed to get the arrow keys normally used for the dialog navigation
216 SetWindowStyle(style
| wxWANTS_CHARS
);
218 m_date
= date
.IsValid() ? date
: wxDateTime::Today();
220 m_lowdate
= wxDefaultDateTime
;
221 m_highdate
= wxDefaultDateTime
;
223 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
225 CreateYearSpinCtrl();
226 m_staticYear
= new wxStaticText(GetParent(), wxID_ANY
, m_date
.Format(_T("%Y")),
227 wxDefaultPosition
, wxDefaultSize
,
230 CreateMonthComboBox();
231 m_staticMonth
= new wxStaticText(GetParent(), wxID_ANY
, m_date
.Format(_T("%B")),
232 wxDefaultPosition
, wxDefaultSize
,
236 ShowCurrentControls();
238 // we need to set the position as well because the main control position
239 // is not the same as the one specified in pos if we have the controls
241 SetInitialSize(size
);
244 // Since we don't paint the whole background make sure that the platform
245 // will use the right one.
246 SetBackgroundColour(m_colBackground
);
253 wxCalendarCtrl::~wxCalendarCtrl()
255 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
260 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
263 delete m_staticMonth
;
269 void wxCalendarCtrl::SetWindowStyleFlag(long style
)
271 // changing this style doesn't work because the controls are not
272 // created/shown/hidden accordingly
273 wxASSERT_MSG( (style
& wxCAL_SEQUENTIAL_MONTH_SELECTION
) ==
274 (m_windowStyle
& wxCAL_SEQUENTIAL_MONTH_SELECTION
),
275 _T("wxCAL_SEQUENTIAL_MONTH_SELECTION can't be changed after creation") );
277 wxControl::SetWindowStyleFlag(style
);
280 // ----------------------------------------------------------------------------
281 // Create the wxComboBox and wxSpinCtrl
282 // ----------------------------------------------------------------------------
284 void wxCalendarCtrl::CreateMonthComboBox()
286 m_comboMonth
= new wxComboBox(GetParent(), wxID_ANY
,
291 wxCB_READONLY
| wxCLIP_SIBLINGS
);
294 for ( m
= wxDateTime::Jan
; m
< wxDateTime::Inv_Month
; wxNextMonth(m
) )
296 m_comboMonth
->Append(wxDateTime::GetMonthName(m
));
299 m_comboMonth
->SetSelection(GetDate().GetMonth());
300 m_comboMonth
->SetSize(wxDefaultCoord
,
304 wxSIZE_AUTO_WIDTH
|wxSIZE_AUTO_HEIGHT
);
306 m_comboMonth
->Connect(wxEVT_COMMAND_COMBOBOX_SELECTED
,
307 wxCommandEventHandler(wxCalendarCtrl::OnMonthChange
),
311 void wxCalendarCtrl::CreateYearSpinCtrl()
313 m_spinYear
= new wxSpinCtrl(GetParent(), wxID_ANY
,
314 GetDate().Format(_T("%Y")),
317 wxSP_ARROW_KEYS
| wxCLIP_SIBLINGS
,
318 -4300, 10000, GetDate().GetYear());
320 m_spinYear
->Connect(wxEVT_COMMAND_TEXT_UPDATED
,
321 wxCommandEventHandler(wxCalendarCtrl::OnYearTextChange
),
324 m_spinYear
->Connect(wxEVT_COMMAND_SPINCTRL_UPDATED
,
325 wxCommandEventHandler(wxCalendarCtrl::OnYearChange
),
329 // ----------------------------------------------------------------------------
330 // forward wxWin functions to subcontrols
331 // ----------------------------------------------------------------------------
333 bool wxCalendarCtrl::Destroy()
336 m_staticYear
->Destroy();
338 m_spinYear
->Destroy();
340 m_comboMonth
->Destroy();
342 m_staticMonth
->Destroy();
347 m_staticMonth
= NULL
;
349 return wxControl::Destroy();
352 bool wxCalendarCtrl::Show(bool show
)
354 if ( !wxControl::Show(show
) )
359 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
361 if ( GetMonthControl() )
363 GetMonthControl()->Show(show
);
364 GetYearControl()->Show(show
);
371 bool wxCalendarCtrl::Enable(bool enable
)
373 if ( !wxControl::Enable(enable
) )
378 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
380 GetMonthControl()->Enable(enable
);
381 GetYearControl()->Enable(enable
);
387 // ----------------------------------------------------------------------------
388 // enable/disable month/year controls
389 // ----------------------------------------------------------------------------
391 void wxCalendarCtrl::ShowCurrentControls()
393 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
395 if ( AllowMonthChange() )
397 m_comboMonth
->Show();
398 m_staticMonth
->Hide();
400 if ( AllowYearChange() )
403 m_staticYear
->Hide();
411 m_comboMonth
->Hide();
412 m_staticMonth
->Show();
415 // year change not allowed here
417 m_staticYear
->Show();
419 //else: these controls are not even created, don't show/hide them
422 wxControl
*wxCalendarCtrl::GetMonthControl() const
424 return AllowMonthChange() ? (wxControl
*)m_comboMonth
: (wxControl
*)m_staticMonth
;
427 wxControl
*wxCalendarCtrl::GetYearControl() const
429 return AllowYearChange() ? (wxControl
*)m_spinYear
: (wxControl
*)m_staticYear
;
432 void wxCalendarCtrl::EnableYearChange(bool enable
)
434 if ( enable
!= AllowYearChange() )
436 long style
= GetWindowStyle();
438 style
&= ~wxCAL_NO_YEAR_CHANGE
;
440 style
|= wxCAL_NO_YEAR_CHANGE
;
441 SetWindowStyle(style
);
443 ShowCurrentControls();
444 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
451 void wxCalendarCtrl::EnableMonthChange(bool enable
)
453 if ( enable
!= AllowMonthChange() )
455 long style
= GetWindowStyle();
457 style
&= ~wxCAL_NO_MONTH_CHANGE
;
459 style
|= wxCAL_NO_MONTH_CHANGE
;
460 SetWindowStyle(style
);
462 ShowCurrentControls();
463 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
470 // ----------------------------------------------------------------------------
472 // ----------------------------------------------------------------------------
474 bool wxCalendarCtrl::SetDate(const wxDateTime
& date
)
478 bool sameMonth
= m_date
.GetMonth() == date
.GetMonth(),
479 sameYear
= m_date
.GetYear() == date
.GetYear();
481 if ( IsDateInRange(date
) )
483 if ( sameMonth
&& sameYear
)
485 // just change the day
490 if ( AllowMonthChange() && (AllowYearChange() || sameYear
) )
495 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
497 // update the controls
498 m_comboMonth
->SetSelection(m_date
.GetMonth());
500 if ( AllowYearChange() )
502 if ( !m_userChangedYear
)
503 m_spinYear
->SetValue(m_date
.Format(_T("%Y")));
507 // as the month changed, holidays did too
510 // update the calendar
521 m_userChangedYear
= false;
526 void wxCalendarCtrl::ChangeDay(const wxDateTime
& date
)
528 if ( m_date
!= date
)
530 // we need to refresh the row containing the old date and the one
531 // containing the new one
532 wxDateTime dateOld
= m_date
;
535 RefreshDate(dateOld
);
537 // if the date is in the same row, it was already drawn correctly
538 if ( GetWeek(m_date
) != GetWeek(dateOld
) )
545 void wxCalendarCtrl::SetDateAndNotify(const wxDateTime
& date
)
547 wxDateTime::Tm tm1
= m_date
.GetTm(),
551 if ( tm1
.year
!= tm2
.year
)
552 type
= wxEVT_CALENDAR_YEAR_CHANGED
;
553 else if ( tm1
.mon
!= tm2
.mon
)
554 type
= wxEVT_CALENDAR_MONTH_CHANGED
;
555 else if ( tm1
.mday
!= tm2
.mday
)
556 type
= wxEVT_CALENDAR_DAY_CHANGED
;
562 GenerateEvents(type
, wxEVT_CALENDAR_SEL_CHANGED
);
566 // ----------------------------------------------------------------------------
568 // ----------------------------------------------------------------------------
570 bool wxCalendarCtrl::SetLowerDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
574 if ( !(date
.IsValid()) || ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : true ) )
586 bool wxCalendarCtrl::SetUpperDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
590 if ( !(date
.IsValid()) || ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : true ) )
602 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& lowerdate
/* = wxDefaultDateTime */, const wxDateTime
& upperdate
/* = wxDefaultDateTime */)
607 ( !( lowerdate
.IsValid() ) || ( ( upperdate
.IsValid() ) ? ( lowerdate
<= upperdate
) : true ) ) &&
608 ( !( upperdate
.IsValid() ) || ( ( lowerdate
.IsValid() ) ? ( upperdate
>= lowerdate
) : true ) ) )
610 m_lowdate
= lowerdate
;
611 m_highdate
= upperdate
;
621 // ----------------------------------------------------------------------------
623 // ----------------------------------------------------------------------------
625 wxDateTime
wxCalendarCtrl::GetStartDate() const
627 wxDateTime::Tm tm
= m_date
.GetTm();
629 wxDateTime date
= wxDateTime(1, tm
.mon
, tm
.year
);
632 date
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
633 ? wxDateTime::Mon
: wxDateTime::Sun
);
635 if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
)
637 // We want to offset the calendar if we start on the first..
638 if ( date
.GetDay() == 1 )
640 date
-= wxDateSpan::Week();
647 bool wxCalendarCtrl::IsDateShown(const wxDateTime
& date
) const
649 if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
651 return date
.GetMonth() == m_date
.GetMonth();
659 bool wxCalendarCtrl::IsDateInRange(const wxDateTime
& date
) const
661 // Check if the given date is in the range specified
662 return ( ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : true )
663 && ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : true ) );
666 bool wxCalendarCtrl::ChangeYear(wxDateTime
* target
) const
670 if ( !(IsDateInRange(*target
)) )
672 if ( target
->GetYear() < m_date
.GetYear() )
674 if ( target
->GetYear() >= GetLowerDateLimit().GetYear() )
676 *target
= GetLowerDateLimit();
686 if ( target
->GetYear() <= GetUpperDateLimit().GetYear() )
688 *target
= GetUpperDateLimit();
705 bool wxCalendarCtrl::ChangeMonth(wxDateTime
* target
) const
709 if ( !(IsDateInRange(*target
)) )
713 if ( target
->GetMonth() < m_date
.GetMonth() )
715 *target
= GetLowerDateLimit();
719 *target
= GetUpperDateLimit();
726 size_t wxCalendarCtrl::GetWeek(const wxDateTime
& date
) const
728 size_t retval
= date
.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
729 ? wxDateTime::Monday_First
730 : wxDateTime::Sunday_First
);
732 if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
734 // we need to offset an extra week if we "start" on the 1st of the month
735 wxDateTime::Tm tm
= date
.GetTm();
737 wxDateTime datetest
= wxDateTime(1, tm
.mon
, tm
.year
);
740 datetest
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
741 ? wxDateTime::Mon
: wxDateTime::Sun
);
743 if ( datetest
.GetDay() == 1 )
752 // ----------------------------------------------------------------------------
754 // ----------------------------------------------------------------------------
756 // this is a composite control and it must arrange its parts each time its
757 // size or position changes: the combobox and spinctrl are along the top of
758 // the available area and the calendar takes up therest of the space
760 // the static controls are supposed to be always smaller than combo/spin so we
761 // always use the latter for size calculations and position the static to take
764 // the constants used for the layout
765 #define VERT_MARGIN 5 // distance between combo and calendar
767 #define HORZ_MARGIN 5 // spin
769 #define HORZ_MARGIN 15 // spin
771 wxSize
wxCalendarCtrl::DoGetBestSize() const
773 // calc the size of the calendar
774 ((wxCalendarCtrl
*)this)->RecalcGeometry(); // const_cast
776 wxCoord width
= 7*m_widthCol
,
777 height
= 7*m_heightRow
+ m_rowOffset
+ VERT_MARGIN
;
779 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
781 // the combobox doesn't report its height correctly (it returns the
782 // height including the drop down list) so don't use it
783 height
+= m_spinYear
->GetBestSize().y
;
786 wxCoord w2
= m_comboMonth
->GetBestSize().x
+ HORZ_MARGIN
+ GetCharWidth()*6;
791 if ( !HasFlag(wxBORDER_NONE
) )
793 // the border would clip the last line otherwise
798 wxSize
best(width
, height
);
803 void wxCalendarCtrl::DoSetSize(int x
, int y
,
804 int width
, int height
,
807 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
810 void wxCalendarCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
814 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) && m_staticMonth
)
816 wxSize sizeCombo
= m_comboMonth
->GetEffectiveMinSize();
817 wxSize sizeStatic
= m_staticMonth
->GetSize();
818 wxSize sizeSpin
= m_spinYear
->GetSize();
820 // wxMSW sometimes reports the wrong combo height,
821 // so on this platform we'll use the spin control
824 int maxHeight
= sizeSpin
.y
;
825 int requiredSpinHeight
= -1;
827 int maxHeight
= sizeCombo
.y
;
828 int requiredSpinHeight
= sizeCombo
.y
;
830 int dy
= (maxHeight
- sizeStatic
.y
) / 2;
831 m_comboMonth
->Move(x
, y
);
832 m_staticMonth
->SetSize(x
, y
+ dy
, sizeCombo
.x
, -1, sizeStatic
.y
);
834 int xDiff
= sizeCombo
.x
+ HORZ_MARGIN
;
836 m_spinYear
->SetSize(x
+ xDiff
, y
, width
- xDiff
, requiredSpinHeight
);
837 m_staticYear
->SetSize(x
+ xDiff
, y
+ dy
, width
- xDiff
, sizeStatic
.y
);
839 yDiff
= wxMax(sizeSpin
.y
, maxHeight
) + VERT_MARGIN
;
841 else // no controls on the top
846 wxControl::DoMoveWindow(x
, y
+ yDiff
, width
, height
- yDiff
);
849 void wxCalendarCtrl::DoGetPosition(int *x
, int *y
) const
851 wxControl::DoGetPosition(x
, y
);
853 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) && GetMonthControl() )
855 // our real top corner is not in this position
858 *y
-= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
864 void wxCalendarCtrl::DoGetSize(int *width
, int *height
) const
866 wxControl::DoGetSize(width
, height
);
868 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
870 // our real height is bigger
871 if ( height
&& GetMonthControl())
873 *height
+= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
879 void wxCalendarCtrl::RecalcGeometry()
883 dc
.SetFont(GetFont());
885 // determine the column width (weekday names are not necessarily wider
886 // than the numbers (in some languages), so let's not assume that they are)
888 for ( int day
= 10; day
<= 31; day
++)
891 dc
.GetTextExtent(wxString::Format(wxT("%d"), day
), &width
, &m_heightRow
);
892 if ( width
> m_widthCol
)
894 // 1.5 times the width gives nice margins even if the weekday
896 m_widthCol
= width
+width
/2;
899 wxDateTime::WeekDay wd
;
900 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
903 dc
.GetTextExtent(m_weekdays
[wd
], &width
, &m_heightRow
);
904 if ( width
> m_widthCol
)
910 // leave some margins
914 m_rowOffset
= HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) ? m_heightRow
: 0; // conditional in relation to style
917 // ----------------------------------------------------------------------------
919 // ----------------------------------------------------------------------------
921 void wxCalendarCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
925 dc
.SetFont(GetFont());
930 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
931 m_date
.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
936 wxCoord x0
= wxMax( (GetSize().x
- m_widthCol
*7) /2 , 0 );
938 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
940 // draw the sequential month-selector
942 dc
.SetBackgroundMode(wxTRANSPARENT
);
943 dc
.SetTextForeground(*wxBLACK
);
944 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
945 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
946 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
948 // Get extent of month-name + year
949 wxCoord monthw
, monthh
;
950 wxString headertext
= m_date
.Format(wxT("%B %Y"));
951 dc
.GetTextExtent(headertext
, &monthw
, &monthh
);
953 // draw month-name centered above weekdays
954 wxCoord monthx
= ((m_widthCol
* 7) - monthw
) / 2 + x0
;
955 wxCoord monthy
= ((m_heightRow
- monthh
) / 2) + y
;
956 dc
.DrawText(headertext
, monthx
, monthy
);
958 // calculate the "month-arrows"
959 wxPoint leftarrow
[3];
960 wxPoint rightarrow
[3];
962 int arrowheight
= monthh
/ 2;
964 leftarrow
[0] = wxPoint(0, arrowheight
/ 2);
965 leftarrow
[1] = wxPoint(arrowheight
/ 2, 0);
966 leftarrow
[2] = wxPoint(arrowheight
/ 2, arrowheight
- 1);
968 rightarrow
[0] = wxPoint(0,0);
969 rightarrow
[1] = wxPoint(arrowheight
/ 2, arrowheight
/ 2);
970 rightarrow
[2] = wxPoint(0, arrowheight
- 1);
972 // draw the "month-arrows"
974 wxCoord arrowy
= (m_heightRow
- arrowheight
) / 2;
975 wxCoord larrowx
= (m_widthCol
- (arrowheight
/ 2)) / 2 + x0
;
976 wxCoord rarrowx
= ((m_widthCol
- (arrowheight
/ 2)) / 2) + m_widthCol
*6 + x0
;
977 m_leftArrowRect
= m_rightArrowRect
= wxRect(0,0,0,0);
979 if ( AllowMonthChange() )
981 wxDateTime ldpm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) - wxDateSpan::Day(); // last day prev month
982 // Check if range permits change
983 if ( IsDateInRange(ldpm
) && ( ( ldpm
.GetYear() == m_date
.GetYear() ) ? true : AllowYearChange() ) )
985 m_leftArrowRect
= wxRect(larrowx
- 3, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
986 dc
.SetBrush(*wxBLACK_BRUSH
);
987 dc
.SetPen(*wxBLACK_PEN
);
988 dc
.DrawPolygon(3, leftarrow
, larrowx
, arrowy
, wxWINDING_RULE
);
989 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
990 dc
.DrawRectangle(m_leftArrowRect
);
992 wxDateTime fdnm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) + wxDateSpan::Month(); // first day next month
993 if ( IsDateInRange(fdnm
) && ( ( fdnm
.GetYear() == m_date
.GetYear() ) ? true : AllowYearChange() ) )
995 m_rightArrowRect
= wxRect(rarrowx
- 4, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
996 dc
.SetBrush(*wxBLACK_BRUSH
);
997 dc
.SetPen(*wxBLACK_PEN
);
998 dc
.DrawPolygon(3, rightarrow
, rarrowx
, arrowy
, wxWINDING_RULE
);
999 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1000 dc
.DrawRectangle(m_rightArrowRect
);
1007 // first draw the week days
1008 if ( IsExposed(x0
, y
, x0
+ 7*m_widthCol
, m_heightRow
) )
1011 wxLogDebug("painting the header");
1014 dc
.SetBackgroundMode(wxTRANSPARENT
);
1015 dc
.SetTextForeground(m_colHeaderFg
);
1016 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
1017 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
1018 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
1020 bool startOnMonday
= (GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1021 for ( int wd
= 0; wd
< 7; wd
++ )
1024 if ( startOnMonday
)
1025 n
= wd
== 6 ? 0 : wd
+ 1;
1029 dc
.GetTextExtent(m_weekdays
[n
], &dayw
, &dayh
);
1030 dc
.DrawText(m_weekdays
[n
], x0
+ (wd
*m_widthCol
) + ((m_widthCol
- dayw
) / 2), y
); // center the day-name
1034 // then the calendar itself
1035 dc
.SetTextForeground(*wxBLACK
);
1036 //dc.SetFont(*wxNORMAL_FONT);
1039 wxDateTime date
= GetStartDate();
1042 wxLogDebug("starting calendar from %s\n",
1043 date
.Format("%a %d-%m-%Y %H:%M:%S").c_str());
1046 dc
.SetBackgroundMode(wxSOLID
);
1047 for ( size_t nWeek
= 1; nWeek
<= 6; nWeek
++, y
+= m_heightRow
)
1049 // if the update region doesn't intersect this row, don't paint it
1050 if ( !IsExposed(x0
, y
, x0
+ 7*m_widthCol
, m_heightRow
- 1) )
1052 date
+= wxDateSpan::Week();
1058 wxLogDebug("painting week %d at y = %d\n", nWeek
, y
);
1061 for ( int wd
= 0; wd
< 7; wd
++ )
1063 dc
.SetTextBackground(m_colBackground
);
1064 if ( IsDateShown(date
) )
1066 // don't use wxDate::Format() which prepends 0s
1067 unsigned int day
= date
.GetDay();
1068 wxString dayStr
= wxString::Format(_T("%u"), day
);
1070 dc
.GetTextExtent(dayStr
, &width
, (wxCoord
*)NULL
);
1072 bool changedColours
= false,
1073 changedFont
= false;
1076 wxCalendarDateAttr
*attr
= NULL
;
1078 if ( date
.GetMonth() != m_date
.GetMonth() || !IsDateInRange(date
) )
1080 // surrounding week or out-of-range
1082 dc
.SetTextForeground(m_colSorrounding
);
1083 changedColours
= true;
1087 isSel
= date
.IsSameDate(m_date
);
1088 attr
= m_attrs
[day
- 1];
1092 dc
.SetTextForeground(m_colHighlightFg
);
1093 dc
.SetTextBackground(m_colHighlightBg
);
1095 changedColours
= true;
1099 wxColour colFg
, colBg
;
1101 if ( attr
->IsHoliday() )
1103 colFg
= m_colHolidayFg
;
1104 colBg
= m_colHolidayBg
;
1108 colFg
= attr
->GetTextColour();
1109 colBg
= attr
->GetBackgroundColour();
1114 dc
.SetTextForeground(colFg
);
1115 changedColours
= true;
1120 dc
.SetTextBackground(colBg
);
1121 changedColours
= true;
1124 if ( attr
->HasFont() )
1126 dc
.SetFont(attr
->GetFont());
1132 wxCoord x
= wd
*m_widthCol
+ (m_widthCol
- width
) / 2 + x0
;
1133 dc
.DrawText(dayStr
, x
, y
+ 1);
1135 if ( !isSel
&& attr
&& attr
->HasBorder() )
1138 if ( attr
->HasBorderColour() )
1140 colBorder
= attr
->GetBorderColour();
1144 colBorder
= GetForegroundColour();
1147 wxPen
pen(colBorder
, 1, wxSOLID
);
1149 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1151 switch ( attr
->GetBorder() )
1153 case wxCAL_BORDER_SQUARE
:
1154 dc
.DrawRectangle(x
- 2, y
,
1155 width
+ 4, m_heightRow
);
1158 case wxCAL_BORDER_ROUND
:
1159 dc
.DrawEllipse(x
- 2, y
,
1160 width
+ 4, m_heightRow
);
1164 wxFAIL_MSG(_T("unknown border type"));
1168 if ( changedColours
)
1170 dc
.SetTextForeground(GetForegroundColour());
1171 dc
.SetTextBackground(GetBackgroundColour());
1176 dc
.SetFont(GetFont());
1179 //else: just don't draw it
1181 date
+= wxDateSpan::Day();
1185 // Greying out out-of-range background
1186 bool showSurrounding
= (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) != 0;
1188 date
= ( showSurrounding
) ? GetStartDate() : wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear());
1189 if ( !IsDateInRange(date
) )
1191 wxDateTime firstOOR
= GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1193 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1194 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1196 HighlightRange(&dc
, date
, firstOOR
, wxTRANSPARENT_PEN
, &oorbrush
);
1199 date
= ( showSurrounding
) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear());
1200 if ( !IsDateInRange(date
) )
1202 wxDateTime firstOOR
= GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1204 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1205 oorbrush
.SetStyle(wxFDIAGONAL_HATCH
);
1207 HighlightRange(&dc
, firstOOR
, date
, wxTRANSPARENT_PEN
, &oorbrush
);
1211 wxLogDebug("+++ finished painting");
1215 void wxCalendarCtrl::RefreshDate(const wxDateTime
& date
)
1221 // always refresh the whole row at once because our OnPaint() will draw
1222 // the whole row anyhow - and this allows the small optimisation in
1223 // OnClick() below to work
1224 rect
.x
= wxMax( (GetSize().x
- m_widthCol
*7) /2 , 0 );
1226 rect
.y
= (m_heightRow
* GetWeek(date
)) + m_rowOffset
;
1228 rect
.width
= 7*m_widthCol
;
1229 rect
.height
= m_heightRow
;
1232 // VZ: for some reason, the selected date seems to occupy more space under
1233 // MSW - this is probably some bug in the font size calculations, but I
1234 // don't know where exactly. This fix is ugly and leads to more
1235 // refreshes than really needed, but without it the selected days
1236 // leaves even more ugly underscores on screen.
1241 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
1244 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1247 Refresh(true, &rect
);
1250 void wxCalendarCtrl::HighlightRange(wxPaintDC
* pDC
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, const wxPen
* pPen
, const wxBrush
* pBrush
)
1252 // Highlights the given range using pen and brush
1253 // Does nothing if todate < fromdate
1257 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate
.Format("%d %m %Y"), todate
.Format("%d %m %Y"));
1260 if ( todate
>= fromdate
)
1267 // implicit: both dates must be currently shown - checked by GetDateCoord
1268 if ( GetDateCoord(fromdate
, &fd
, &fw
) && GetDateCoord(todate
, &td
, &tw
) )
1271 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd
, fw
, td
, tw
);
1273 if ( ( (tw
- fw
) == 1 ) && ( td
< fd
) )
1275 // special case: interval 7 days or less not in same week
1276 // split in two separate intervals
1277 wxDateTime tfd
= fromdate
+ wxDateSpan::Days(7-fd
);
1278 wxDateTime ftd
= tfd
+ wxDateSpan::Day();
1280 wxLogDebug("Highlight: Separate segments");
1283 HighlightRange(pDC
, fromdate
, tfd
, pPen
, pBrush
);
1284 HighlightRange(pDC
, ftd
, todate
, pPen
, pBrush
);
1289 wxPoint corners
[8]; // potentially 8 corners in polygon
1290 wxCoord x0
= wxMax( (GetSize().x
- m_widthCol
*7) /2 , 0 );
1294 // simple case: same week
1296 corners
[0] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
);
1297 corners
[1] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, ((fw
+ 1 ) * m_heightRow
) + m_rowOffset
);
1298 corners
[2] = wxPoint(x0
+ td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
);
1299 corners
[3] = wxPoint(x0
+ td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
);
1304 // "complex" polygon
1305 corners
[cidx
] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1309 corners
[cidx
] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1310 corners
[cidx
] = wxPoint(x0
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1313 corners
[cidx
] = wxPoint(x0
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1314 corners
[cidx
] = wxPoint(x0
+ td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1318 corners
[cidx
] = wxPoint(x0
+ td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1319 corners
[cidx
] = wxPoint(x0
+ 7 * m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1322 corners
[cidx
] = wxPoint(x0
+ 7 * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1328 pDC
->SetBrush(*pBrush
);
1330 pDC
->DrawPolygon(numpoints
, corners
);
1336 wxLogDebug("--- HighlightRange ---");
1340 bool wxCalendarCtrl::GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const
1345 wxLogDebug("+++ GetDateCoord: (%s) +++", date
.Format("%d %m %Y"));
1348 if ( IsDateShown(date
) )
1350 bool startOnMonday
= ( GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1353 *day
= date
.GetWeekDay();
1355 if ( *day
== 0 ) // sunday
1357 *day
= ( startOnMonday
) ? 7 : 1;
1361 *day
+= ( startOnMonday
) ? 0 : 1;
1364 int targetmonth
= date
.GetMonth() + (12 * date
.GetYear());
1365 int thismonth
= m_date
.GetMonth() + (12 * m_date
.GetYear());
1368 if ( targetmonth
== thismonth
)
1370 *week
= GetWeek(date
);
1374 if ( targetmonth
< thismonth
)
1376 *week
= 1; // trivial
1378 else // targetmonth > thismonth
1384 // get the datecoord of the last day in the month currently shown
1386 wxLogDebug(" +++ LDOM +++");
1388 GetDateCoord(ldcm
.SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear()), &lastday
, &lastweek
);
1390 wxLogDebug(" --- LDOM ---");
1393 wxTimeSpan span
= date
- ldcm
;
1395 int daysfromlast
= span
.GetDays();
1397 wxLogDebug("daysfromlast: %i", daysfromlast
);
1399 if ( daysfromlast
+ lastday
> 7 ) // past week boundary
1401 int wholeweeks
= (daysfromlast
/ 7);
1402 *week
= wholeweeks
+ lastweek
;
1403 if ( (daysfromlast
- (7 * wholeweeks
) + lastday
) > 7 )
1423 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date
.Format("%d %m %Y"), *day
, *week
);
1429 // ----------------------------------------------------------------------------
1431 // ----------------------------------------------------------------------------
1433 void wxCalendarCtrl::OnDClick(wxMouseEvent
& event
)
1435 if ( HitTest(event
.GetPosition()) != wxCAL_HITTEST_DAY
)
1441 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1445 void wxCalendarCtrl::OnClick(wxMouseEvent
& event
)
1448 wxDateTime::WeekDay wday
;
1449 switch ( HitTest(event
.GetPosition(), &date
, &wday
) )
1451 case wxCAL_HITTEST_DAY
:
1452 if ( IsDateInRange(date
) )
1456 GenerateEvents(wxEVT_CALENDAR_DAY_CHANGED
,
1457 wxEVT_CALENDAR_SEL_CHANGED
);
1461 case wxCAL_HITTEST_HEADER
:
1463 wxCalendarEvent
eventWd(this, wxEVT_CALENDAR_WEEKDAY_CLICKED
);
1464 eventWd
.m_wday
= wday
;
1465 (void)GetEventHandler()->ProcessEvent(eventWd
);
1469 case wxCAL_HITTEST_DECMONTH
:
1470 case wxCAL_HITTEST_INCMONTH
:
1471 case wxCAL_HITTEST_SURROUNDING_WEEK
:
1472 SetDateAndNotify(date
); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1476 wxFAIL_MSG(_T("unknown hittest code"));
1479 case wxCAL_HITTEST_NOWHERE
:
1485 wxCalendarHitTestResult
wxCalendarCtrl::HitTest(const wxPoint
& pos
,
1487 wxDateTime::WeekDay
*wd
)
1490 // use the correct x-pos
1491 wxCoord x0
= wxMax((GetSize().x
- m_widthCol
*7) /2, 0);
1492 wxPoint pos_corr
= pos
;
1495 wxCoord y
= pos_corr
.y
;
1497 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1498 if ( (GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
1502 // we need to find out if the hit is on left arrow, on month or on right arrow
1504 if ( wxRegion(m_leftArrowRect
).Contains(pos_corr
) == wxInRegion
)
1508 if ( IsDateInRange(m_date
- wxDateSpan::Month()) )
1510 *date
= m_date
- wxDateSpan::Month();
1514 *date
= GetLowerDateLimit();
1518 return wxCAL_HITTEST_DECMONTH
;
1521 if ( wxRegion(m_rightArrowRect
).Contains(pos_corr
) == wxInRegion
)
1525 if ( IsDateInRange(m_date
+ wxDateSpan::Month()) )
1527 *date
= m_date
+ wxDateSpan::Month();
1531 *date
= GetUpperDateLimit();
1535 return wxCAL_HITTEST_INCMONTH
;
1540 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1543 int wday
= pos_corr
.x
/ m_widthCol
;
1544 // if ( y < m_heightRow )
1545 if ( y
< (m_heightRow
+ m_rowOffset
) )
1547 if ( y
> m_rowOffset
)
1551 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST
)
1553 wday
= wday
== 6 ? 0 : wday
+ 1;
1556 *wd
= (wxDateTime::WeekDay
)wday
;
1559 return wxCAL_HITTEST_HEADER
;
1563 return wxCAL_HITTEST_NOWHERE
;
1567 // int week = (y - m_heightRow) / m_heightRow;
1568 int week
= (y
- (m_heightRow
+ m_rowOffset
)) / m_heightRow
;
1569 if ( week
>= 6 || wday
>= 7 )
1571 return wxCAL_HITTEST_NOWHERE
;
1574 wxDateTime dt
= GetStartDate() + wxDateSpan::Days(7*week
+ wday
);
1576 if ( IsDateShown(dt
) )
1581 if ( dt
.GetMonth() == m_date
.GetMonth() )
1584 return wxCAL_HITTEST_DAY
;
1588 return wxCAL_HITTEST_SURROUNDING_WEEK
;
1593 return wxCAL_HITTEST_NOWHERE
;
1597 // ----------------------------------------------------------------------------
1598 // subcontrols events handling
1599 // ----------------------------------------------------------------------------
1601 void wxCalendarCtrl::OnMonthChange(wxCommandEvent
& event
)
1603 wxDateTime::Tm tm
= m_date
.GetTm();
1605 wxDateTime::Month mon
= (wxDateTime::Month
)event
.GetInt();
1606 if ( tm
.mday
> wxDateTime::GetNumberOfDays(mon
, tm
.year
) )
1608 tm
.mday
= wxDateTime::GetNumberOfDays(mon
, tm
.year
);
1611 wxDateTime target
= wxDateTime(tm
.mday
, mon
, tm
.year
);
1613 ChangeMonth(&target
);
1614 SetDateAndNotify(target
);
1617 void wxCalendarCtrl::OnYearChange(wxCommandEvent
& event
)
1619 int year
= (int)event
.GetInt();
1620 if ( year
== INT_MIN
)
1622 // invalid year in the spin control, ignore it
1626 wxDateTime::Tm tm
= m_date
.GetTm();
1628 if ( tm
.mday
> wxDateTime::GetNumberOfDays(tm
.mon
, year
) )
1630 tm
.mday
= wxDateTime::GetNumberOfDays(tm
.mon
, year
);
1633 wxDateTime target
= wxDateTime(tm
.mday
, tm
.mon
, year
);
1635 if ( ChangeYear(&target
) )
1637 SetDateAndNotify(target
);
1641 // In this case we don't want to change the date. That would put us
1642 // inside the same year but a strange number of months forward/back..
1643 m_spinYear
->SetValue(target
.GetYear());
1647 void wxCalendarCtrl::OnYearTextChange(wxCommandEvent
& event
)
1649 SetUserChangedYear();
1650 OnYearChange(event
);
1653 // Responds to colour changes, and passes event on to children.
1654 void wxCalendarCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1659 // Propagate the event to the children
1660 wxControl::OnSysColourChanged(event
);
1662 // Redraw control area
1663 SetBackgroundColour(m_colBackground
);
1667 // ----------------------------------------------------------------------------
1668 // keyboard interface
1669 // ----------------------------------------------------------------------------
1671 void wxCalendarCtrl::OnChar(wxKeyEvent
& event
)
1674 switch ( event
.GetKeyCode() )
1678 target
= m_date
+ wxDateSpan::Year();
1679 if ( ChangeYear(&target
) )
1681 SetDateAndNotify(target
);
1687 target
= m_date
- wxDateSpan::Year();
1688 if ( ChangeYear(&target
) )
1690 SetDateAndNotify(target
);
1695 target
= m_date
- wxDateSpan::Month();
1696 ChangeMonth(&target
);
1697 SetDateAndNotify(target
); // always
1701 target
= m_date
+ wxDateSpan::Month();
1702 ChangeMonth(&target
);
1703 SetDateAndNotify(target
); // always
1707 if ( event
.ControlDown() )
1709 target
= wxDateTime(m_date
).SetToNextWeekDay(
1710 GetWindowStyle() & wxCAL_MONDAY_FIRST
1711 ? wxDateTime::Sun
: wxDateTime::Sat
);
1712 if ( !IsDateInRange(target
) )
1714 target
= GetUpperDateLimit();
1716 SetDateAndNotify(target
);
1719 SetDateAndNotify(m_date
+ wxDateSpan::Day());
1723 if ( event
.ControlDown() )
1725 target
= wxDateTime(m_date
).SetToPrevWeekDay(
1726 GetWindowStyle() & wxCAL_MONDAY_FIRST
1727 ? wxDateTime::Mon
: wxDateTime::Sun
);
1728 if ( !IsDateInRange(target
) )
1730 target
= GetLowerDateLimit();
1732 SetDateAndNotify(target
);
1735 SetDateAndNotify(m_date
- wxDateSpan::Day());
1739 SetDateAndNotify(m_date
- wxDateSpan::Week());
1743 SetDateAndNotify(m_date
+ wxDateSpan::Week());
1747 if ( event
.ControlDown() )
1748 SetDateAndNotify(wxDateTime::Today());
1750 SetDateAndNotify(wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear()));
1754 SetDateAndNotify(wxDateTime(m_date
).SetToLastMonthDay());
1758 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1766 // ----------------------------------------------------------------------------
1767 // holidays handling
1768 // ----------------------------------------------------------------------------
1770 void wxCalendarCtrl::EnableHolidayDisplay(bool display
)
1772 long style
= GetWindowStyle();
1774 style
|= wxCAL_SHOW_HOLIDAYS
;
1776 style
&= ~wxCAL_SHOW_HOLIDAYS
;
1778 SetWindowStyle(style
);
1783 ResetHolidayAttrs();
1788 void wxCalendarCtrl::SetHolidayAttrs()
1790 if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS
)
1792 ResetHolidayAttrs();
1794 wxDateTime::Tm tm
= m_date
.GetTm();
1795 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
1796 dtEnd
= dtStart
.GetLastMonthDay();
1798 wxDateTimeArray hol
;
1799 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
1801 size_t count
= hol
.GetCount();
1802 for ( size_t n
= 0; n
< count
; n
++ )
1804 SetHoliday(hol
[n
].GetDay());
1809 void wxCalendarCtrl::SetHoliday(size_t day
)
1811 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in SetHoliday") );
1813 wxCalendarDateAttr
*attr
= GetAttr(day
);
1816 attr
= new wxCalendarDateAttr
;
1819 attr
->SetHoliday(true);
1821 // can't use SetAttr() because it would delete this pointer
1822 m_attrs
[day
- 1] = attr
;
1825 void wxCalendarCtrl::ResetHolidayAttrs()
1827 for ( size_t day
= 0; day
< 31; day
++ )
1831 m_attrs
[day
]->SetHoliday(false);
1839 wxCalendarCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
1841 // Use the same color scheme as wxListBox
1842 return wxListBox::GetClassDefaultAttributes(variant
);
1845 #endif // wxUSE_CALENDARCTRL