1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/calctrlg.cpp
3 // Purpose: implementation of the wxGenericCalendarCtrl
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"
38 #if wxUSE_CALENDARCTRL
40 #include "wx/spinctrl.h"
41 #include "wx/calctrl.h"
42 #include "wx/generic/calctrlg.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 BEGIN_EVENT_TABLE(wxGenericCalendarCtrl
, wxControl
)
51 EVT_PAINT(wxGenericCalendarCtrl::OnPaint
)
53 EVT_CHAR(wxGenericCalendarCtrl::OnChar
)
55 EVT_LEFT_DOWN(wxGenericCalendarCtrl::OnClick
)
56 EVT_LEFT_DCLICK(wxGenericCalendarCtrl::OnDClick
)
58 EVT_SYS_COLOUR_CHANGED(wxGenericCalendarCtrl::OnSysColourChanged
)
61 #if wxUSE_EXTENDED_RTTI
62 WX_DEFINE_FLAGS( wxCalendarCtrlStyle
)
64 wxBEGIN_FLAGS( wxCalendarCtrlStyle
)
65 // new style border flags, we put them first to
66 // use them for streaming out
67 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
68 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
69 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
70 wxFLAGS_MEMBER(wxBORDER_RAISED
)
71 wxFLAGS_MEMBER(wxBORDER_STATIC
)
72 wxFLAGS_MEMBER(wxBORDER_NONE
)
74 // old style border flags
75 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
76 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
77 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
78 wxFLAGS_MEMBER(wxRAISED_BORDER
)
79 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
80 wxFLAGS_MEMBER(wxBORDER
)
82 // standard window styles
83 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
84 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
85 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
86 wxFLAGS_MEMBER(wxWANTS_CHARS
)
87 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
88 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
89 wxFLAGS_MEMBER(wxVSCROLL
)
90 wxFLAGS_MEMBER(wxHSCROLL
)
92 wxFLAGS_MEMBER(wxCAL_SUNDAY_FIRST
)
93 wxFLAGS_MEMBER(wxCAL_MONDAY_FIRST
)
94 wxFLAGS_MEMBER(wxCAL_SHOW_HOLIDAYS
)
95 wxFLAGS_MEMBER(wxCAL_NO_YEAR_CHANGE
)
96 wxFLAGS_MEMBER(wxCAL_NO_MONTH_CHANGE
)
97 wxFLAGS_MEMBER(wxCAL_SEQUENTIAL_MONTH_SELECTION
)
98 wxFLAGS_MEMBER(wxCAL_SHOW_SURROUNDING_WEEKS
)
100 wxEND_FLAGS( wxCalendarCtrlStyle
)
102 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericCalendarCtrl
, wxControl
,"wx/calctrl.h")
104 wxBEGIN_PROPERTIES_TABLE(wxGenericCalendarCtrl
)
105 wxEVENT_RANGE_PROPERTY( Updated
, wxEVT_CALENDAR_SEL_CHANGED
, wxEVT_CALENDAR_WEEKDAY_CLICKED
, wxCalendarEvent
)
106 wxHIDE_PROPERTY( Children
)
107 wxPROPERTY( Date
,wxDateTime
, SetDate
, GetDate
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
108 wxPROPERTY_FLAGS( WindowStyle
, wxCalendarCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
109 wxEND_PROPERTIES_TABLE()
111 wxBEGIN_HANDLERS_TABLE(wxGenericCalendarCtrl
)
112 wxEND_HANDLERS_TABLE()
114 wxCONSTRUCTOR_6( wxGenericCalendarCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxDateTime
, Date
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
116 IMPLEMENT_DYNAMIC_CLASS(wxGenericCalendarCtrl
, wxControl
)
119 // ============================================================================
121 // ============================================================================
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
130 // add attributes that are set in attr
131 void AddAttr(wxCalendarDateAttr
*self
, const wxCalendarDateAttr
& attr
)
133 if (attr
.HasTextColour())
134 self
->SetTextColour(attr
.GetTextColour());
135 if (attr
.HasBackgroundColour())
136 self
->SetBackgroundColour(attr
.GetBackgroundColour());
137 if (attr
.HasBorderColour())
138 self
->SetBorderColour(attr
.GetBorderColour());
140 self
->SetFont(attr
.GetFont());
141 if (attr
.HasBorder())
142 self
->SetBorder(attr
.GetBorder());
143 if (attr
.IsHoliday())
144 self
->SetHoliday(true);
147 // remove attributes that are set in attr
148 void DelAttr(wxCalendarDateAttr
*self
, const wxCalendarDateAttr
&attr
)
150 if (attr
.HasTextColour())
151 self
->SetTextColour(wxNullColour
);
152 if (attr
.HasBackgroundColour())
153 self
->SetBackgroundColour(wxNullColour
);
154 if (attr
.HasBorderColour())
155 self
->SetBorderColour(wxNullColour
);
157 self
->SetFont(wxNullFont
);
158 if (attr
.HasBorder())
159 self
->SetBorder(wxCAL_BORDER_NONE
);
160 if (attr
.IsHoliday())
161 self
->SetHoliday(false);
164 } // anonymous namespace
166 // ----------------------------------------------------------------------------
167 // wxGenericCalendarCtrl
168 // ----------------------------------------------------------------------------
170 wxGenericCalendarCtrl::wxGenericCalendarCtrl(wxWindow
*parent
,
172 const wxDateTime
& date
,
176 const wxString
& name
)
180 (void)Create(parent
, id
, date
, pos
, size
, style
, name
);
183 void wxGenericCalendarCtrl::Init()
188 m_staticMonth
= NULL
;
190 m_userChangedYear
= false;
195 wxDateTime::WeekDay wd
;
196 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
198 m_weekdays
[wd
] = wxDateTime::GetWeekDayName(wd
, wxDateTime::Name_Abbr
);
201 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
209 void wxGenericCalendarCtrl::InitColours()
211 m_colHighlightFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
212 m_colHighlightBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
213 m_colBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
214 m_colSorrounding
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
216 m_colHolidayFg
= *wxRED
;
217 // don't set m_colHolidayBg - by default, same as our bg colour
219 m_colHeaderFg
= *wxBLUE
;
220 m_colHeaderBg
= *wxLIGHT_GREY
;
223 bool wxGenericCalendarCtrl::Create(wxWindow
*parent
,
225 const wxDateTime
& date
,
229 const wxString
& name
)
231 if ( !wxControl::Create(parent
, id
, pos
, size
,
232 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
| wxFULL_REPAINT_ON_RESIZE
,
233 wxDefaultValidator
, name
) )
238 // needed to get the arrow keys normally used for the dialog navigation
239 SetWindowStyle(style
| wxWANTS_CHARS
);
241 m_date
= date
.IsValid() ? date
: wxDateTime::Today();
243 m_lowdate
= wxDefaultDateTime
;
244 m_highdate
= wxDefaultDateTime
;
246 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
248 CreateYearSpinCtrl();
249 m_staticYear
= new wxStaticText(GetParent(), wxID_ANY
, m_date
.Format(_T("%Y")),
250 wxDefaultPosition
, wxDefaultSize
,
253 CreateMonthComboBox();
254 m_staticMonth
= new wxStaticText(GetParent(), wxID_ANY
, m_date
.Format(_T("%B")),
255 wxDefaultPosition
, wxDefaultSize
,
259 ShowCurrentControls();
261 // we need to set the position as well because the main control position
262 // is not the same as the one specified in pos if we have the controls
264 SetInitialSize(size
);
267 // Since we don't paint the whole background make sure that the platform
268 // will use the right one.
269 SetBackgroundColour(m_colBackground
);
276 wxGenericCalendarCtrl::~wxGenericCalendarCtrl()
278 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
283 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
286 delete m_staticMonth
;
292 void wxGenericCalendarCtrl::SetWindowStyleFlag(long style
)
294 // changing this style doesn't work because the controls are not
295 // created/shown/hidden accordingly
296 wxASSERT_MSG( (style
& wxCAL_SEQUENTIAL_MONTH_SELECTION
) ==
297 (m_windowStyle
& wxCAL_SEQUENTIAL_MONTH_SELECTION
),
298 _T("wxCAL_SEQUENTIAL_MONTH_SELECTION can't be changed after creation") );
300 wxControl::SetWindowStyleFlag(style
);
303 // ----------------------------------------------------------------------------
304 // Create the wxComboBox and wxSpinCtrl
305 // ----------------------------------------------------------------------------
307 void wxGenericCalendarCtrl::CreateMonthComboBox()
309 m_comboMonth
= new wxComboBox(GetParent(), wxID_ANY
,
314 wxCB_READONLY
| wxCLIP_SIBLINGS
);
317 for ( m
= wxDateTime::Jan
; m
< wxDateTime::Inv_Month
; wxNextMonth(m
) )
319 m_comboMonth
->Append(wxDateTime::GetMonthName(m
));
322 m_comboMonth
->SetSelection(GetDate().GetMonth());
323 m_comboMonth
->SetSize(wxDefaultCoord
,
327 wxSIZE_AUTO_WIDTH
|wxSIZE_AUTO_HEIGHT
);
329 m_comboMonth
->Connect(m_comboMonth
->GetId(), wxEVT_COMMAND_COMBOBOX_SELECTED
,
330 wxCommandEventHandler(wxGenericCalendarCtrl::OnMonthChange
),
334 void wxGenericCalendarCtrl::CreateYearSpinCtrl()
336 m_spinYear
= new wxSpinCtrl(GetParent(), wxID_ANY
,
337 GetDate().Format(_T("%Y")),
340 wxSP_ARROW_KEYS
| wxCLIP_SIBLINGS
,
341 -4300, 10000, GetDate().GetYear());
343 m_spinYear
->Connect(m_spinYear
->GetId(), wxEVT_COMMAND_TEXT_UPDATED
,
344 wxCommandEventHandler(wxGenericCalendarCtrl::OnYearTextChange
),
347 m_spinYear
->Connect(m_spinYear
->GetId(), wxEVT_COMMAND_SPINCTRL_UPDATED
,
348 wxCommandEventHandler(wxGenericCalendarCtrl::OnYearChange
),
352 // ----------------------------------------------------------------------------
353 // forward wxWin functions to subcontrols
354 // ----------------------------------------------------------------------------
356 bool wxGenericCalendarCtrl::Destroy()
359 m_staticYear
->Destroy();
361 m_spinYear
->Destroy();
363 m_comboMonth
->Destroy();
365 m_staticMonth
->Destroy();
370 m_staticMonth
= NULL
;
372 return wxControl::Destroy();
375 bool wxGenericCalendarCtrl::Show(bool show
)
377 if ( !wxControl::Show(show
) )
382 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
384 if ( GetMonthControl() )
386 GetMonthControl()->Show(show
);
387 GetYearControl()->Show(show
);
394 bool wxGenericCalendarCtrl::Enable(bool enable
)
396 if ( !wxControl::Enable(enable
) )
401 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
403 GetMonthControl()->Enable(enable
);
404 GetYearControl()->Enable(enable
);
410 // ----------------------------------------------------------------------------
411 // enable/disable month/year controls
412 // ----------------------------------------------------------------------------
414 void wxGenericCalendarCtrl::ShowCurrentControls()
416 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
418 if ( AllowMonthChange() )
420 m_comboMonth
->Show();
421 m_staticMonth
->Hide();
423 if ( AllowYearChange() )
426 m_staticYear
->Hide();
434 m_comboMonth
->Hide();
435 m_staticMonth
->Show();
438 // year change not allowed here
440 m_staticYear
->Show();
442 //else: these controls are not even created, don't show/hide them
445 wxControl
*wxGenericCalendarCtrl::GetMonthControl() const
447 return AllowMonthChange() ? (wxControl
*)m_comboMonth
: (wxControl
*)m_staticMonth
;
450 wxControl
*wxGenericCalendarCtrl::GetYearControl() const
452 return AllowYearChange() ? (wxControl
*)m_spinYear
: (wxControl
*)m_staticYear
;
455 void wxGenericCalendarCtrl::EnableYearChange(bool enable
)
457 if ( enable
!= AllowYearChange() )
459 long style
= GetWindowStyle();
461 style
&= ~wxCAL_NO_YEAR_CHANGE
;
463 style
|= wxCAL_NO_YEAR_CHANGE
;
464 SetWindowStyle(style
);
466 ShowCurrentControls();
467 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
474 bool wxGenericCalendarCtrl::EnableMonthChange(bool enable
)
476 if ( !wxCalendarCtrlBase::EnableMonthChange(enable
) )
479 ShowCurrentControls();
480 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
486 // ----------------------------------------------------------------------------
488 // ----------------------------------------------------------------------------
490 bool wxGenericCalendarCtrl::SetDate(const wxDateTime
& date
)
494 bool sameMonth
= m_date
.GetMonth() == date
.GetMonth(),
495 sameYear
= m_date
.GetYear() == date
.GetYear();
497 if ( IsDateInRange(date
) )
499 if ( sameMonth
&& sameYear
)
501 // just change the day
506 if ( AllowMonthChange() && (AllowYearChange() || sameYear
) )
511 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
513 // update the controls
514 m_comboMonth
->SetSelection(m_date
.GetMonth());
516 if ( AllowYearChange() )
518 if ( !m_userChangedYear
)
519 m_spinYear
->SetValue(m_date
.Format(_T("%Y")));
523 // as the month changed, holidays did too
526 // update the calendar
537 m_userChangedYear
= false;
542 void wxGenericCalendarCtrl::ChangeDay(const wxDateTime
& date
)
544 if ( m_date
!= date
)
546 // we need to refresh the row containing the old date and the one
547 // containing the new one
548 wxDateTime dateOld
= m_date
;
551 RefreshDate(dateOld
);
553 // if the date is in the same row, it was already drawn correctly
554 if ( GetWeek(m_date
) != GetWeek(dateOld
) )
561 void wxGenericCalendarCtrl::SetDateAndNotify(const wxDateTime
& date
)
563 wxDateTime::Tm tm1
= m_date
.GetTm(),
566 const bool pageChanged
= tm1
.year
!= tm2
.year
|| tm1
.mon
!= tm2
.mon
;
568 if ( !pageChanged
&& tm1
.mday
== tm2
.mday
)
573 GenerateEvent(wxEVT_CALENDAR_SEL_CHANGED
);
575 GenerateEvent(wxEVT_CALENDAR_PAGE_CHANGED
);
577 // send also one of the deprecated events
578 if ( tm1
.year
!= tm2
.year
)
579 GenerateEvent(wxEVT_CALENDAR_YEAR_CHANGED
);
580 else if ( tm1
.mon
!= tm2
.mon
)
581 GenerateEvent(wxEVT_CALENDAR_MONTH_CHANGED
);
583 GenerateEvent(wxEVT_CALENDAR_DAY_CHANGED
);
587 // ----------------------------------------------------------------------------
589 // ----------------------------------------------------------------------------
591 bool wxGenericCalendarCtrl::SetLowerDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
595 if ( !(date
.IsValid()) || ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : true ) )
607 bool wxGenericCalendarCtrl::SetUpperDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
611 if ( !(date
.IsValid()) || ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : true ) )
623 bool wxGenericCalendarCtrl::SetDateRange(const wxDateTime
& lowerdate
/* = wxDefaultDateTime */, const wxDateTime
& upperdate
/* = wxDefaultDateTime */)
628 ( !( lowerdate
.IsValid() ) || ( ( upperdate
.IsValid() ) ? ( lowerdate
<= upperdate
) : true ) ) &&
629 ( !( upperdate
.IsValid() ) || ( ( lowerdate
.IsValid() ) ? ( upperdate
>= lowerdate
) : true ) ) )
631 m_lowdate
= lowerdate
;
632 m_highdate
= upperdate
;
642 // ----------------------------------------------------------------------------
644 // ----------------------------------------------------------------------------
646 wxDateTime
wxGenericCalendarCtrl::GetStartDate() const
648 wxDateTime::Tm tm
= m_date
.GetTm();
650 wxDateTime date
= wxDateTime(1, tm
.mon
, tm
.year
);
653 date
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
654 ? wxDateTime::Mon
: wxDateTime::Sun
);
656 if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
)
658 // We want to offset the calendar if we start on the first..
659 if ( date
.GetDay() == 1 )
661 date
-= wxDateSpan::Week();
668 bool wxGenericCalendarCtrl::IsDateShown(const wxDateTime
& date
) const
670 if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
672 return date
.GetMonth() == m_date
.GetMonth();
680 bool wxGenericCalendarCtrl::IsDateInRange(const wxDateTime
& date
) const
682 // Check if the given date is in the range specified
683 return ( ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : true )
684 && ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : true ) );
687 bool wxGenericCalendarCtrl::ChangeYear(wxDateTime
* target
) const
691 if ( !(IsDateInRange(*target
)) )
693 if ( target
->GetYear() < m_date
.GetYear() )
695 if ( target
->GetYear() >= GetLowerDateLimit().GetYear() )
697 *target
= GetLowerDateLimit();
707 if ( target
->GetYear() <= GetUpperDateLimit().GetYear() )
709 *target
= GetUpperDateLimit();
726 bool wxGenericCalendarCtrl::ChangeMonth(wxDateTime
* target
) const
730 if ( !(IsDateInRange(*target
)) )
734 if ( target
->GetMonth() < m_date
.GetMonth() )
736 *target
= GetLowerDateLimit();
740 *target
= GetUpperDateLimit();
747 size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime
& date
) const
749 size_t retval
= date
.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
750 ? wxDateTime::Monday_First
751 : wxDateTime::Sunday_First
);
753 if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
755 // we need to offset an extra week if we "start" on the 1st of the month
756 wxDateTime::Tm tm
= date
.GetTm();
758 wxDateTime datetest
= wxDateTime(1, tm
.mon
, tm
.year
);
761 datetest
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
762 ? wxDateTime::Mon
: wxDateTime::Sun
);
764 if ( datetest
.GetDay() == 1 )
773 // ----------------------------------------------------------------------------
775 // ----------------------------------------------------------------------------
777 // this is a composite control and it must arrange its parts each time its
778 // size or position changes: the combobox and spinctrl are along the top of
779 // the available area and the calendar takes up therest of the space
781 // the static controls are supposed to be always smaller than combo/spin so we
782 // always use the latter for size calculations and position the static to take
785 // the constants used for the layout
786 #define VERT_MARGIN 5 // distance between combo and calendar
788 #define HORZ_MARGIN 5 // spin
790 #define HORZ_MARGIN 15 // spin
792 wxSize
wxGenericCalendarCtrl::DoGetBestSize() const
794 // calc the size of the calendar
795 wx_const_cast(wxGenericCalendarCtrl
*, this)->RecalcGeometry();
797 wxCoord width
= 7*m_widthCol
,
798 height
= 7*m_heightRow
+ m_rowOffset
+ VERT_MARGIN
;
800 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
802 // the combobox doesn't report its height correctly (it returns the
803 // height including the drop down list) so don't use it
804 height
+= m_spinYear
->GetBestSize().y
;
807 wxCoord w2
= m_comboMonth
->GetBestSize().x
+ HORZ_MARGIN
+ GetCharWidth()*6;
812 if ( !HasFlag(wxBORDER_NONE
) )
814 // the border would clip the last line otherwise
819 wxSize
best(width
, height
);
824 void wxGenericCalendarCtrl::DoSetSize(int x
, int y
,
825 int width
, int height
,
828 wxControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
831 void wxGenericCalendarCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
835 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) && m_staticMonth
)
837 wxSize sizeCombo
= m_comboMonth
->GetEffectiveMinSize();
838 wxSize sizeStatic
= m_staticMonth
->GetSize();
839 wxSize sizeSpin
= m_spinYear
->GetSize();
841 // wxMSW sometimes reports the wrong combo height,
842 // so on this platform we'll use the spin control
845 int maxHeight
= sizeSpin
.y
;
846 int requiredSpinHeight
= -1;
848 int maxHeight
= sizeCombo
.y
;
849 int requiredSpinHeight
= sizeCombo
.y
;
851 int dy
= (maxHeight
- sizeStatic
.y
) / 2;
852 m_comboMonth
->Move(x
, y
);
853 m_staticMonth
->SetSize(x
, y
+ dy
, sizeCombo
.x
, -1, sizeStatic
.y
);
855 int xDiff
= sizeCombo
.x
+ HORZ_MARGIN
;
857 m_spinYear
->SetSize(x
+ xDiff
, y
, width
- xDiff
, requiredSpinHeight
);
858 m_staticYear
->SetSize(x
+ xDiff
, y
+ dy
, width
- xDiff
, sizeStatic
.y
);
860 yDiff
= wxMax(sizeSpin
.y
, maxHeight
) + VERT_MARGIN
;
862 else // no controls on the top
867 wxControl::DoMoveWindow(x
, y
+ yDiff
, width
, height
- yDiff
);
870 void wxGenericCalendarCtrl::DoGetPosition(int *x
, int *y
) const
872 wxControl::DoGetPosition(x
, y
);
874 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) && GetMonthControl() )
876 // our real top corner is not in this position
879 *y
-= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
885 void wxGenericCalendarCtrl::DoGetSize(int *width
, int *height
) const
887 wxControl::DoGetSize(width
, height
);
889 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
891 // our real height is bigger
892 if ( height
&& GetMonthControl())
894 *height
+= GetMonthControl()->GetSize().y
+ VERT_MARGIN
;
900 void wxGenericCalendarCtrl::RecalcGeometry()
904 dc
.SetFont(GetFont());
906 // determine the column width (weekday names are not necessarily wider
907 // than the numbers (in some languages), so let's not assume that they are)
909 for ( int day
= 10; day
<= 31; day
++)
912 dc
.GetTextExtent(wxString::Format(wxT("%d"), day
), &width
, &m_heightRow
);
913 if ( width
> m_widthCol
)
915 // 1.5 times the width gives nice margins even if the weekday
917 m_widthCol
= width
+width
/2;
920 wxDateTime::WeekDay wd
;
921 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
924 dc
.GetTextExtent(m_weekdays
[wd
], &width
, &m_heightRow
);
925 if ( width
> m_widthCol
)
931 // leave some margins
935 m_rowOffset
= HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) ? m_heightRow
: 0; // conditional in relation to style
938 // ----------------------------------------------------------------------------
940 // ----------------------------------------------------------------------------
942 void wxGenericCalendarCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
946 dc
.SetFont(GetFont());
951 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
952 m_date
.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
957 wxCoord x0
= wxMax( (GetSize().x
- m_widthCol
*7) /2 , 0 );
959 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
961 // draw the sequential month-selector
963 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
964 dc
.SetTextForeground(*wxBLACK
);
965 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxBRUSHSTYLE_SOLID
));
966 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxPENSTYLE_SOLID
));
967 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
969 // Get extent of month-name + year
970 wxCoord monthw
, monthh
;
971 wxString headertext
= m_date
.Format(wxT("%B %Y"));
972 dc
.GetTextExtent(headertext
, &monthw
, &monthh
);
974 // draw month-name centered above weekdays
975 wxCoord monthx
= ((m_widthCol
* 7) - monthw
) / 2 + x0
;
976 wxCoord monthy
= ((m_heightRow
- monthh
) / 2) + y
;
977 dc
.DrawText(headertext
, monthx
, monthy
);
979 // calculate the "month-arrows"
980 wxPoint leftarrow
[3];
981 wxPoint rightarrow
[3];
983 int arrowheight
= monthh
/ 2;
985 leftarrow
[0] = wxPoint(0, arrowheight
/ 2);
986 leftarrow
[1] = wxPoint(arrowheight
/ 2, 0);
987 leftarrow
[2] = wxPoint(arrowheight
/ 2, arrowheight
- 1);
989 rightarrow
[0] = wxPoint(0,0);
990 rightarrow
[1] = wxPoint(arrowheight
/ 2, arrowheight
/ 2);
991 rightarrow
[2] = wxPoint(0, arrowheight
- 1);
993 // draw the "month-arrows"
995 wxCoord arrowy
= (m_heightRow
- arrowheight
) / 2;
996 wxCoord larrowx
= (m_widthCol
- (arrowheight
/ 2)) / 2 + x0
;
997 wxCoord rarrowx
= ((m_widthCol
- (arrowheight
/ 2)) / 2) + m_widthCol
*6 + x0
;
998 m_leftArrowRect
= m_rightArrowRect
= wxRect(0,0,0,0);
1000 if ( AllowMonthChange() )
1002 wxDateTime ldpm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) - wxDateSpan::Day(); // last day prev month
1003 // Check if range permits change
1004 if ( IsDateInRange(ldpm
) && ( ( ldpm
.GetYear() == m_date
.GetYear() ) ? true : AllowYearChange() ) )
1006 m_leftArrowRect
= wxRect(larrowx
- 3, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
1007 dc
.SetBrush(*wxBLACK_BRUSH
);
1008 dc
.SetPen(*wxBLACK_PEN
);
1009 dc
.DrawPolygon(3, leftarrow
, larrowx
, arrowy
, wxWINDING_RULE
);
1010 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1011 dc
.DrawRectangle(m_leftArrowRect
);
1013 wxDateTime fdnm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) + wxDateSpan::Month(); // first day next month
1014 if ( IsDateInRange(fdnm
) && ( ( fdnm
.GetYear() == m_date
.GetYear() ) ? true : AllowYearChange() ) )
1016 m_rightArrowRect
= wxRect(rarrowx
- 4, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
1017 dc
.SetBrush(*wxBLACK_BRUSH
);
1018 dc
.SetPen(*wxBLACK_PEN
);
1019 dc
.DrawPolygon(3, rightarrow
, rarrowx
, arrowy
, wxWINDING_RULE
);
1020 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1021 dc
.DrawRectangle(m_rightArrowRect
);
1028 // first draw the week days
1029 if ( IsExposed(x0
, y
, x0
+ 7*m_widthCol
, m_heightRow
) )
1032 wxLogDebug("painting the header");
1035 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
1036 dc
.SetTextForeground(m_colHeaderFg
);
1037 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxBRUSHSTYLE_SOLID
));
1038 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxPENSTYLE_SOLID
));
1039 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
1041 bool startOnMonday
= (GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1042 for ( int wd
= 0; wd
< 7; wd
++ )
1045 if ( startOnMonday
)
1046 n
= wd
== 6 ? 0 : wd
+ 1;
1050 dc
.GetTextExtent(m_weekdays
[n
], &dayw
, &dayh
);
1051 dc
.DrawText(m_weekdays
[n
], x0
+ (wd
*m_widthCol
) + ((m_widthCol
- dayw
) / 2), y
); // center the day-name
1055 // then the calendar itself
1056 dc
.SetTextForeground(*wxBLACK
);
1057 //dc.SetFont(*wxNORMAL_FONT);
1060 wxDateTime date
= GetStartDate();
1063 wxLogDebug("starting calendar from %s\n",
1064 date
.Format("%a %d-%m-%Y %H:%M:%S").c_str());
1067 dc
.SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
1068 for ( size_t nWeek
= 1; nWeek
<= 6; nWeek
++, y
+= m_heightRow
)
1070 // if the update region doesn't intersect this row, don't paint it
1071 if ( !IsExposed(x0
, y
, x0
+ 7*m_widthCol
, m_heightRow
- 1) )
1073 date
+= wxDateSpan::Week();
1079 wxLogDebug("painting week %d at y = %d\n", nWeek
, y
);
1082 for ( int wd
= 0; wd
< 7; wd
++ )
1084 dc
.SetTextBackground(m_colBackground
);
1085 if ( IsDateShown(date
) )
1087 // don't use wxDate::Format() which prepends 0s
1088 unsigned int day
= date
.GetDay();
1089 wxString dayStr
= wxString::Format(_T("%u"), day
);
1091 dc
.GetTextExtent(dayStr
, &width
, (wxCoord
*)NULL
);
1093 bool changedColours
= false,
1094 changedFont
= false;
1097 wxCalendarDateAttr
*attr
= NULL
;
1099 if ( date
.GetMonth() != m_date
.GetMonth() || !IsDateInRange(date
) )
1101 // surrounding week or out-of-range
1103 dc
.SetTextForeground(m_colSorrounding
);
1104 changedColours
= true;
1108 isSel
= date
.IsSameDate(m_date
);
1109 attr
= m_attrs
[day
- 1];
1113 dc
.SetTextForeground(m_colHighlightFg
);
1114 dc
.SetTextBackground(m_colHighlightBg
);
1116 changedColours
= true;
1120 wxColour colFg
, colBg
;
1122 if ( attr
->IsHoliday() )
1124 colFg
= m_colHolidayFg
;
1125 colBg
= m_colHolidayBg
;
1129 colFg
= attr
->GetTextColour();
1130 colBg
= attr
->GetBackgroundColour();
1135 dc
.SetTextForeground(colFg
);
1136 changedColours
= true;
1141 dc
.SetTextBackground(colBg
);
1142 changedColours
= true;
1145 if ( attr
->HasFont() )
1147 dc
.SetFont(attr
->GetFont());
1153 wxCoord x
= wd
*m_widthCol
+ (m_widthCol
- width
) / 2 + x0
;
1154 dc
.DrawText(dayStr
, x
, y
+ 1);
1156 if ( !isSel
&& attr
&& attr
->HasBorder() )
1159 if ( attr
->HasBorderColour() )
1161 colBorder
= attr
->GetBorderColour();
1165 colBorder
= GetForegroundColour();
1168 wxPen
pen(colBorder
, 1, wxPENSTYLE_SOLID
);
1170 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1172 switch ( attr
->GetBorder() )
1174 case wxCAL_BORDER_SQUARE
:
1175 dc
.DrawRectangle(x
- 2, y
,
1176 width
+ 4, m_heightRow
);
1179 case wxCAL_BORDER_ROUND
:
1180 dc
.DrawEllipse(x
- 2, y
,
1181 width
+ 4, m_heightRow
);
1185 wxFAIL_MSG(_T("unknown border type"));
1189 if ( changedColours
)
1191 dc
.SetTextForeground(GetForegroundColour());
1192 dc
.SetTextBackground(GetBackgroundColour());
1197 dc
.SetFont(GetFont());
1200 //else: just don't draw it
1202 date
+= wxDateSpan::Day();
1206 // Greying out out-of-range background
1207 bool showSurrounding
= (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) != 0;
1209 date
= ( showSurrounding
) ? GetStartDate() : wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear());
1210 if ( !IsDateInRange(date
) )
1212 wxDateTime firstOOR
= GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1214 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1215 oorbrush
.SetStyle(wxBRUSHSTYLE_FDIAGONAL_HATCH
);
1217 HighlightRange(&dc
, date
, firstOOR
, wxTRANSPARENT_PEN
, &oorbrush
);
1220 date
= ( showSurrounding
) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear());
1221 if ( !IsDateInRange(date
) )
1223 wxDateTime firstOOR
= GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1225 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1226 oorbrush
.SetStyle(wxBRUSHSTYLE_FDIAGONAL_HATCH
);
1228 HighlightRange(&dc
, firstOOR
, date
, wxTRANSPARENT_PEN
, &oorbrush
);
1232 wxLogDebug("+++ finished painting");
1236 void wxGenericCalendarCtrl::RefreshDate(const wxDateTime
& date
)
1242 // always refresh the whole row at once because our OnPaint() will draw
1243 // the whole row anyhow - and this allows the small optimisation in
1244 // OnClick() below to work
1245 rect
.x
= wxMax( (GetSize().x
- m_widthCol
*7) /2 , 0 );
1247 rect
.y
= (m_heightRow
* GetWeek(date
)) + m_rowOffset
;
1249 rect
.width
= 7*m_widthCol
;
1250 rect
.height
= m_heightRow
;
1253 // VZ: for some reason, the selected date seems to occupy more space under
1254 // MSW - this is probably some bug in the font size calculations, but I
1255 // don't know where exactly. This fix is ugly and leads to more
1256 // refreshes than really needed, but without it the selected days
1257 // leaves even more ugly underscores on screen.
1262 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
1265 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1268 Refresh(true, &rect
);
1271 void wxGenericCalendarCtrl::HighlightRange(wxPaintDC
* pDC
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, const wxPen
* pPen
, const wxBrush
* pBrush
)
1273 // Highlights the given range using pen and brush
1274 // Does nothing if todate < fromdate
1278 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate
.Format("%d %m %Y"), todate
.Format("%d %m %Y"));
1281 if ( todate
>= fromdate
)
1288 // implicit: both dates must be currently shown - checked by GetDateCoord
1289 if ( GetDateCoord(fromdate
, &fd
, &fw
) && GetDateCoord(todate
, &td
, &tw
) )
1292 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd
, fw
, td
, tw
);
1294 if ( ( (tw
- fw
) == 1 ) && ( td
< fd
) )
1296 // special case: interval 7 days or less not in same week
1297 // split in two separate intervals
1298 wxDateTime tfd
= fromdate
+ wxDateSpan::Days(7-fd
);
1299 wxDateTime ftd
= tfd
+ wxDateSpan::Day();
1301 wxLogDebug("Highlight: Separate segments");
1304 HighlightRange(pDC
, fromdate
, tfd
, pPen
, pBrush
);
1305 HighlightRange(pDC
, ftd
, todate
, pPen
, pBrush
);
1310 wxPoint corners
[8]; // potentially 8 corners in polygon
1311 wxCoord x0
= wxMax( (GetSize().x
- m_widthCol
*7) /2 , 0 );
1315 // simple case: same week
1317 corners
[0] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
);
1318 corners
[1] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, ((fw
+ 1 ) * m_heightRow
) + m_rowOffset
);
1319 corners
[2] = wxPoint(x0
+ td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
);
1320 corners
[3] = wxPoint(x0
+ td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
);
1325 // "complex" polygon
1326 corners
[cidx
] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1330 corners
[cidx
] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1331 corners
[cidx
] = wxPoint(x0
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1334 corners
[cidx
] = wxPoint(x0
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1335 corners
[cidx
] = wxPoint(x0
+ td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1339 corners
[cidx
] = wxPoint(x0
+ td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1340 corners
[cidx
] = wxPoint(x0
+ 7 * m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1343 corners
[cidx
] = wxPoint(x0
+ 7 * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1349 pDC
->SetBrush(*pBrush
);
1351 pDC
->DrawPolygon(numpoints
, corners
);
1357 wxLogDebug("--- HighlightRange ---");
1361 bool wxGenericCalendarCtrl::GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const
1366 wxLogDebug("+++ GetDateCoord: (%s) +++", date
.Format("%d %m %Y"));
1369 if ( IsDateShown(date
) )
1371 bool startOnMonday
= ( GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1374 *day
= date
.GetWeekDay();
1376 if ( *day
== 0 ) // sunday
1378 *day
= ( startOnMonday
) ? 7 : 1;
1382 *day
+= ( startOnMonday
) ? 0 : 1;
1385 int targetmonth
= date
.GetMonth() + (12 * date
.GetYear());
1386 int thismonth
= m_date
.GetMonth() + (12 * m_date
.GetYear());
1389 if ( targetmonth
== thismonth
)
1391 *week
= GetWeek(date
);
1395 if ( targetmonth
< thismonth
)
1397 *week
= 1; // trivial
1399 else // targetmonth > thismonth
1405 // get the datecoord of the last day in the month currently shown
1407 wxLogDebug(" +++ LDOM +++");
1409 GetDateCoord(ldcm
.SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear()), &lastday
, &lastweek
);
1411 wxLogDebug(" --- LDOM ---");
1414 wxTimeSpan span
= date
- ldcm
;
1416 int daysfromlast
= span
.GetDays();
1418 wxLogDebug("daysfromlast: %i", daysfromlast
);
1420 if ( daysfromlast
+ lastday
> 7 ) // past week boundary
1422 int wholeweeks
= (daysfromlast
/ 7);
1423 *week
= wholeweeks
+ lastweek
;
1424 if ( (daysfromlast
- (7 * wholeweeks
) + lastday
) > 7 )
1444 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date
.Format("%d %m %Y"), *day
, *week
);
1450 // ----------------------------------------------------------------------------
1452 // ----------------------------------------------------------------------------
1454 void wxGenericCalendarCtrl::OnDClick(wxMouseEvent
& event
)
1456 if ( HitTest(event
.GetPosition()) != wxCAL_HITTEST_DAY
)
1462 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1466 void wxGenericCalendarCtrl::OnClick(wxMouseEvent
& event
)
1469 wxDateTime::WeekDay wday
;
1470 switch ( HitTest(event
.GetPosition(), &date
, &wday
) )
1472 case wxCAL_HITTEST_DAY
:
1473 if ( IsDateInRange(date
) )
1477 GenerateEvent(wxEVT_CALENDAR_SEL_CHANGED
);
1478 GenerateEvent(wxEVT_CALENDAR_DAY_CHANGED
);
1482 case wxCAL_HITTEST_HEADER
:
1484 wxCalendarEvent
eventWd(this, GetDate(),
1485 wxEVT_CALENDAR_WEEKDAY_CLICKED
);
1486 eventWd
.SetWeekDay(wday
);
1487 (void)GetEventHandler()->ProcessEvent(eventWd
);
1491 case wxCAL_HITTEST_DECMONTH
:
1492 case wxCAL_HITTEST_INCMONTH
:
1493 case wxCAL_HITTEST_SURROUNDING_WEEK
:
1494 SetDateAndNotify(date
); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1498 wxFAIL_MSG(_T("unknown hittest code"));
1501 case wxCAL_HITTEST_NOWHERE
:
1507 wxCalendarHitTestResult
wxGenericCalendarCtrl::HitTest(const wxPoint
& pos
,
1509 wxDateTime::WeekDay
*wd
)
1513 // the position where the calendar really begins
1514 wxCoord x0
= wxMax((GetSize().x
- m_widthCol
*7)/2, 0);
1516 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
1520 // we need to find out if the hit is on left arrow, on month or on right arrow
1522 if ( m_leftArrowRect
.Contains(pos
) )
1526 if ( IsDateInRange(m_date
- wxDateSpan::Month()) )
1528 *date
= m_date
- wxDateSpan::Month();
1532 *date
= GetLowerDateLimit();
1536 return wxCAL_HITTEST_DECMONTH
;
1539 if ( m_rightArrowRect
.Contains(pos
) )
1543 if ( IsDateInRange(m_date
+ wxDateSpan::Month()) )
1545 *date
= m_date
+ wxDateSpan::Month();
1549 *date
= GetUpperDateLimit();
1553 return wxCAL_HITTEST_INCMONTH
;
1558 // header: week days
1559 int wday
= (pos
.x
- x0
) / m_widthCol
;
1560 if ( pos
.y
< (m_heightRow
+ m_rowOffset
) )
1562 if ( pos
.y
> m_rowOffset
)
1566 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST
)
1568 wday
= wday
== 6 ? 0 : wday
+ 1;
1571 *wd
= (wxDateTime::WeekDay
)wday
;
1574 return wxCAL_HITTEST_HEADER
;
1578 return wxCAL_HITTEST_NOWHERE
;
1582 int week
= (pos
.y
- (m_heightRow
+ m_rowOffset
)) / m_heightRow
;
1583 if ( week
>= 6 || wday
>= 7 )
1585 return wxCAL_HITTEST_NOWHERE
;
1588 wxDateTime dt
= GetStartDate() + wxDateSpan::Days(7*week
+ wday
);
1590 if ( IsDateShown(dt
) )
1595 if ( dt
.GetMonth() == m_date
.GetMonth() )
1598 return wxCAL_HITTEST_DAY
;
1602 return wxCAL_HITTEST_SURROUNDING_WEEK
;
1607 return wxCAL_HITTEST_NOWHERE
;
1611 // ----------------------------------------------------------------------------
1612 // subcontrols events handling
1613 // ----------------------------------------------------------------------------
1615 void wxGenericCalendarCtrl::OnMonthChange(wxCommandEvent
& event
)
1617 wxDateTime::Tm tm
= m_date
.GetTm();
1619 wxDateTime::Month mon
= (wxDateTime::Month
)event
.GetInt();
1620 if ( tm
.mday
> wxDateTime::GetNumberOfDays(mon
, tm
.year
) )
1622 tm
.mday
= wxDateTime::GetNumberOfDays(mon
, tm
.year
);
1625 wxDateTime target
= wxDateTime(tm
.mday
, mon
, tm
.year
);
1627 ChangeMonth(&target
);
1628 SetDateAndNotify(target
);
1631 void wxGenericCalendarCtrl::OnYearChange(wxCommandEvent
& event
)
1633 int year
= (int)event
.GetInt();
1634 if ( year
== INT_MIN
)
1636 // invalid year in the spin control, ignore it
1640 wxDateTime::Tm tm
= m_date
.GetTm();
1642 if ( tm
.mday
> wxDateTime::GetNumberOfDays(tm
.mon
, year
) )
1644 tm
.mday
= wxDateTime::GetNumberOfDays(tm
.mon
, year
);
1647 wxDateTime target
= wxDateTime(tm
.mday
, tm
.mon
, year
);
1649 if ( ChangeYear(&target
) )
1651 SetDateAndNotify(target
);
1655 // In this case we don't want to change the date. That would put us
1656 // inside the same year but a strange number of months forward/back..
1657 m_spinYear
->SetValue(target
.GetYear());
1661 void wxGenericCalendarCtrl::OnYearTextChange(wxCommandEvent
& event
)
1663 SetUserChangedYear();
1664 OnYearChange(event
);
1667 // Responds to colour changes, and passes event on to children.
1668 void wxGenericCalendarCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1673 // Propagate the event to the children
1674 wxControl::OnSysColourChanged(event
);
1676 // Redraw control area
1677 SetBackgroundColour(m_colBackground
);
1681 // ----------------------------------------------------------------------------
1682 // keyboard interface
1683 // ----------------------------------------------------------------------------
1685 void wxGenericCalendarCtrl::OnChar(wxKeyEvent
& event
)
1688 switch ( event
.GetKeyCode() )
1692 target
= m_date
+ wxDateSpan::Year();
1693 if ( ChangeYear(&target
) )
1695 SetDateAndNotify(target
);
1701 target
= m_date
- wxDateSpan::Year();
1702 if ( ChangeYear(&target
) )
1704 SetDateAndNotify(target
);
1709 target
= m_date
- wxDateSpan::Month();
1710 ChangeMonth(&target
);
1711 SetDateAndNotify(target
); // always
1715 target
= m_date
+ wxDateSpan::Month();
1716 ChangeMonth(&target
);
1717 SetDateAndNotify(target
); // always
1721 if ( event
.ControlDown() )
1723 target
= wxDateTime(m_date
).SetToNextWeekDay(
1724 GetWindowStyle() & wxCAL_MONDAY_FIRST
1725 ? wxDateTime::Sun
: wxDateTime::Sat
);
1726 if ( !IsDateInRange(target
) )
1728 target
= GetUpperDateLimit();
1730 SetDateAndNotify(target
);
1733 SetDateAndNotify(m_date
+ wxDateSpan::Day());
1737 if ( event
.ControlDown() )
1739 target
= wxDateTime(m_date
).SetToPrevWeekDay(
1740 GetWindowStyle() & wxCAL_MONDAY_FIRST
1741 ? wxDateTime::Mon
: wxDateTime::Sun
);
1742 if ( !IsDateInRange(target
) )
1744 target
= GetLowerDateLimit();
1746 SetDateAndNotify(target
);
1749 SetDateAndNotify(m_date
- wxDateSpan::Day());
1753 SetDateAndNotify(m_date
- wxDateSpan::Week());
1757 SetDateAndNotify(m_date
+ wxDateSpan::Week());
1761 if ( event
.ControlDown() )
1762 SetDateAndNotify(wxDateTime::Today());
1764 SetDateAndNotify(wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear()));
1768 SetDateAndNotify(wxDateTime(m_date
).SetToLastMonthDay());
1772 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1780 // ----------------------------------------------------------------------------
1781 // holidays handling
1782 // ----------------------------------------------------------------------------
1784 void wxGenericCalendarCtrl::EnableHolidayDisplay(bool display
)
1786 long style
= GetWindowStyle();
1788 style
|= wxCAL_SHOW_HOLIDAYS
;
1790 style
&= ~wxCAL_SHOW_HOLIDAYS
;
1792 SetWindowStyle(style
);
1797 ResetHolidayAttrs();
1802 void wxGenericCalendarCtrl::SetHolidayAttrs()
1804 if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS
)
1806 ResetHolidayAttrs();
1808 wxDateTime::Tm tm
= m_date
.GetTm();
1809 wxDateTime
dtStart(1, tm
.mon
, tm
.year
),
1810 dtEnd
= dtStart
.GetLastMonthDay();
1812 wxDateTimeArray hol
;
1813 wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart
, dtEnd
, hol
);
1815 size_t count
= hol
.GetCount();
1816 for ( size_t n
= 0; n
< count
; n
++ )
1818 SetHoliday(hol
[n
].GetDay());
1823 void wxGenericCalendarCtrl::SetHoliday(size_t day
)
1825 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in SetHoliday") );
1827 wxCalendarDateAttr
*attr
= GetAttr(day
);
1830 attr
= new wxCalendarDateAttr
;
1833 attr
->SetHoliday(true);
1835 // can't use SetAttr() because it would delete this pointer
1836 m_attrs
[day
- 1] = attr
;
1839 void wxGenericCalendarCtrl::ResetHolidayAttrs()
1841 for ( size_t day
= 0; day
< 31; day
++ )
1845 m_attrs
[day
]->SetHoliday(false);
1850 void wxGenericCalendarCtrl::Mark(size_t day
, bool mark
)
1852 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in Mark") );
1854 const wxCalendarDateAttr
& m
= wxCalendarDateAttr::GetMark();
1856 if ( m_attrs
[day
- 1] )
1857 AddAttr(m_attrs
[day
- 1], m
);
1859 SetAttr(day
, new wxCalendarDateAttr(m
));
1862 if ( m_attrs
[day
- 1] )
1863 DelAttr(m_attrs
[day
- 1], m
);
1869 wxGenericCalendarCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
1871 // Use the same color scheme as wxListBox
1872 return wxListBox::GetClassDefaultAttributes(variant
);
1875 #endif // wxUSE_CALENDARCTRL