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_colSurrounding
= 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
,
252 CreateMonthComboBox();
253 m_staticMonth
= new wxStaticText(GetParent(), wxID_ANY
, m_date
.Format(_T("%B")),
254 wxDefaultPosition
, wxDefaultSize
,
258 ShowCurrentControls();
260 // we need to set the position as well because the main control position
261 // is not the same as the one specified in pos if we have the controls
263 SetInitialSize(size
);
266 // Since we don't paint the whole background make sure that the platform
267 // will use the right one.
268 SetBackgroundColour(m_colBackground
);
275 wxGenericCalendarCtrl::~wxGenericCalendarCtrl()
277 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
282 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
285 delete m_staticMonth
;
291 void wxGenericCalendarCtrl::SetWindowStyleFlag(long style
)
293 // changing this style doesn't work because the controls are not
294 // created/shown/hidden accordingly
295 wxASSERT_MSG( (style
& wxCAL_SEQUENTIAL_MONTH_SELECTION
) ==
296 (m_windowStyle
& wxCAL_SEQUENTIAL_MONTH_SELECTION
),
297 _T("wxCAL_SEQUENTIAL_MONTH_SELECTION can't be changed after creation") );
299 wxControl::SetWindowStyleFlag(style
);
302 // ----------------------------------------------------------------------------
303 // Create the wxComboBox and wxSpinCtrl
304 // ----------------------------------------------------------------------------
306 void wxGenericCalendarCtrl::CreateMonthComboBox()
308 m_comboMonth
= new wxComboBox(GetParent(), wxID_ANY
,
313 wxCB_READONLY
| wxCLIP_SIBLINGS
);
316 for ( m
= wxDateTime::Jan
; m
< wxDateTime::Inv_Month
; wxNextMonth(m
) )
318 m_comboMonth
->Append(wxDateTime::GetMonthName(m
));
321 m_comboMonth
->SetSelection(GetDate().GetMonth());
322 m_comboMonth
->SetSize(wxDefaultCoord
,
326 wxSIZE_AUTO_WIDTH
|wxSIZE_AUTO_HEIGHT
);
328 m_comboMonth
->Connect(m_comboMonth
->GetId(), wxEVT_COMMAND_COMBOBOX_SELECTED
,
329 wxCommandEventHandler(wxGenericCalendarCtrl::OnMonthChange
),
333 void wxGenericCalendarCtrl::CreateYearSpinCtrl()
335 m_spinYear
= new wxSpinCtrl(GetParent(), wxID_ANY
,
336 GetDate().Format(_T("%Y")),
339 wxSP_ARROW_KEYS
| wxCLIP_SIBLINGS
,
340 -4300, 10000, GetDate().GetYear());
342 m_spinYear
->SetSize( 90, -1 );
345 m_spinYear
->Connect(m_spinYear
->GetId(), wxEVT_COMMAND_TEXT_UPDATED
,
346 wxCommandEventHandler(wxGenericCalendarCtrl::OnYearTextChange
),
349 m_spinYear
->Connect(m_spinYear
->GetId(), wxEVT_COMMAND_SPINCTRL_UPDATED
,
350 wxSpinEventHandler(wxGenericCalendarCtrl::OnYearChange
),
354 // ----------------------------------------------------------------------------
355 // forward wxWin functions to subcontrols
356 // ----------------------------------------------------------------------------
358 bool wxGenericCalendarCtrl::Destroy()
361 m_staticYear
->Destroy();
363 m_spinYear
->Destroy();
365 m_comboMonth
->Destroy();
367 m_staticMonth
->Destroy();
372 m_staticMonth
= NULL
;
374 return wxControl::Destroy();
377 bool wxGenericCalendarCtrl::Show(bool show
)
379 if ( !wxControl::Show(show
) )
384 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
386 if ( GetMonthControl() )
388 GetMonthControl()->Show(show
);
389 GetYearControl()->Show(show
);
396 bool wxGenericCalendarCtrl::Enable(bool enable
)
398 if ( !wxControl::Enable(enable
) )
403 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
405 GetMonthControl()->Enable(enable
);
406 GetYearControl()->Enable(enable
);
412 // ----------------------------------------------------------------------------
413 // enable/disable month/year controls
414 // ----------------------------------------------------------------------------
416 void wxGenericCalendarCtrl::ShowCurrentControls()
418 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
420 if ( AllowMonthChange() )
422 m_comboMonth
->Show();
423 m_staticMonth
->Hide();
425 if ( AllowYearChange() )
428 m_staticYear
->Hide();
436 m_comboMonth
->Hide();
437 m_staticMonth
->Show();
440 // year change not allowed here
442 m_staticYear
->Show();
444 //else: these controls are not even created, don't show/hide them
447 wxControl
*wxGenericCalendarCtrl::GetMonthControl() const
449 return AllowMonthChange() ? (wxControl
*)m_comboMonth
: (wxControl
*)m_staticMonth
;
452 wxControl
*wxGenericCalendarCtrl::GetYearControl() const
454 return AllowYearChange() ? (wxControl
*)m_spinYear
: (wxControl
*)m_staticYear
;
457 void wxGenericCalendarCtrl::EnableYearChange(bool enable
)
459 if ( enable
!= AllowYearChange() )
461 long style
= GetWindowStyle();
463 style
&= ~wxCAL_NO_YEAR_CHANGE
;
465 style
|= wxCAL_NO_YEAR_CHANGE
;
466 SetWindowStyle(style
);
468 ShowCurrentControls();
469 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
476 bool wxGenericCalendarCtrl::EnableMonthChange(bool enable
)
478 if ( !wxCalendarCtrlBase::EnableMonthChange(enable
) )
481 ShowCurrentControls();
482 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
488 // ----------------------------------------------------------------------------
490 // ----------------------------------------------------------------------------
492 bool wxGenericCalendarCtrl::SetDate(const wxDateTime
& date
)
496 bool sameMonth
= m_date
.GetMonth() == date
.GetMonth(),
497 sameYear
= m_date
.GetYear() == date
.GetYear();
499 if ( IsDateInRange(date
) )
501 if ( sameMonth
&& sameYear
)
503 // just change the day
508 if ( AllowMonthChange() && (AllowYearChange() || sameYear
) )
513 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
515 // update the controls
516 m_comboMonth
->SetSelection(m_date
.GetMonth());
518 if ( AllowYearChange() )
520 if ( !m_userChangedYear
)
521 m_spinYear
->SetValue(m_date
.Format(_T("%Y")));
525 // as the month changed, holidays did too
528 // update the calendar
539 m_userChangedYear
= false;
544 void wxGenericCalendarCtrl::ChangeDay(const wxDateTime
& date
)
546 if ( m_date
!= date
)
548 // we need to refresh the row containing the old date and the one
549 // containing the new one
550 wxDateTime dateOld
= m_date
;
553 RefreshDate(dateOld
);
555 // if the date is in the same row, it was already drawn correctly
556 if ( GetWeek(m_date
) != GetWeek(dateOld
) )
563 void wxGenericCalendarCtrl::SetDateAndNotify(const wxDateTime
& date
)
565 const wxDateTime dateOld
= GetDate();
566 if ( date
!= dateOld
&& SetDate(date
) )
568 GenerateAllChangeEvents(dateOld
);
572 // ----------------------------------------------------------------------------
574 // ----------------------------------------------------------------------------
576 bool wxGenericCalendarCtrl::SetLowerDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
580 if ( !(date
.IsValid()) || ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : true ) )
592 bool wxGenericCalendarCtrl::SetUpperDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
596 if ( !(date
.IsValid()) || ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : true ) )
608 bool wxGenericCalendarCtrl::SetDateRange(const wxDateTime
& lowerdate
/* = wxDefaultDateTime */, const wxDateTime
& upperdate
/* = wxDefaultDateTime */)
613 ( !( lowerdate
.IsValid() ) || ( ( upperdate
.IsValid() ) ? ( lowerdate
<= upperdate
) : true ) ) &&
614 ( !( upperdate
.IsValid() ) || ( ( lowerdate
.IsValid() ) ? ( upperdate
>= lowerdate
) : true ) ) )
616 m_lowdate
= lowerdate
;
617 m_highdate
= upperdate
;
627 bool wxGenericCalendarCtrl::GetDateRange(wxDateTime
*lowerdate
,
628 wxDateTime
*upperdate
) const
631 *lowerdate
= m_lowdate
;
633 *upperdate
= m_highdate
;
635 return m_lowdate
.IsValid() || m_highdate
.IsValid();
638 // ----------------------------------------------------------------------------
640 // ----------------------------------------------------------------------------
642 wxDateTime
wxGenericCalendarCtrl::GetStartDate() const
644 wxDateTime::Tm tm
= m_date
.GetTm();
646 wxDateTime date
= wxDateTime(1, tm
.mon
, tm
.year
);
649 date
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
650 ? wxDateTime::Mon
: wxDateTime::Sun
);
652 if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
)
654 // We want to offset the calendar if we start on the first..
655 if ( date
.GetDay() == 1 )
657 date
-= wxDateSpan::Week();
664 bool wxGenericCalendarCtrl::IsDateShown(const wxDateTime
& date
) const
666 if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
668 return date
.GetMonth() == m_date
.GetMonth();
676 bool wxGenericCalendarCtrl::IsDateInRange(const wxDateTime
& date
) const
678 // Check if the given date is in the range specified
679 return ( ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : true )
680 && ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : true ) );
683 bool wxGenericCalendarCtrl::ChangeYear(wxDateTime
* target
) const
687 if ( !(IsDateInRange(*target
)) )
689 if ( target
->GetYear() < m_date
.GetYear() )
691 if ( target
->GetYear() >= GetLowerDateLimit().GetYear() )
693 *target
= GetLowerDateLimit();
703 if ( target
->GetYear() <= GetUpperDateLimit().GetYear() )
705 *target
= GetUpperDateLimit();
722 bool wxGenericCalendarCtrl::ChangeMonth(wxDateTime
* target
) const
726 if ( !(IsDateInRange(*target
)) )
730 if ( target
->GetMonth() < m_date
.GetMonth() )
732 *target
= GetLowerDateLimit();
736 *target
= GetUpperDateLimit();
743 size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime
& date
) const
745 size_t retval
= date
.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
746 ? wxDateTime::Monday_First
747 : wxDateTime::Sunday_First
);
749 if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
751 // we need to offset an extra week if we "start" on the 1st of the month
752 wxDateTime::Tm tm
= date
.GetTm();
754 wxDateTime datetest
= wxDateTime(1, tm
.mon
, tm
.year
);
757 datetest
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
758 ? wxDateTime::Mon
: wxDateTime::Sun
);
760 if ( datetest
.GetDay() == 1 )
769 // ----------------------------------------------------------------------------
771 // ----------------------------------------------------------------------------
773 // this is a composite control and it must arrange its parts each time its
774 // size or position changes: the combobox and spinctrl are along the top of
775 // the available area and the calendar takes up therest of the space
777 // the static controls are supposed to be always smaller than combo/spin so we
778 // always use the latter for size calculations and position the static to take
781 // the constants used for the layout
782 #define VERT_MARGIN 5 // distance between combo and calendar
783 #define HORZ_MARGIN 5 // spin
785 wxSize
wxGenericCalendarCtrl::DoGetBestSize() const
787 // calc the size of the calendar
788 const_cast<wxGenericCalendarCtrl
*>(this)->RecalcGeometry();
790 wxCoord width
= 7*m_widthCol
,
791 height
= 7*m_heightRow
+ m_rowOffset
;
793 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
795 const wxSize bestSizeCombo
= m_comboMonth
->GetBestSize();
797 height
+= wxMax(bestSizeCombo
.y
, m_spinYear
->GetBestSize().y
)
800 // the spin control get clipped otherwise
804 wxCoord w2
= bestSizeCombo
.x
+ HORZ_MARGIN
+ GetCharWidth()*6;
809 wxSize
best(width
, height
);
810 if ( !HasFlag(wxBORDER_NONE
) )
812 best
+= GetWindowBorderSize();
820 void wxGenericCalendarCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
824 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) && m_staticMonth
)
826 wxSize sizeCombo
= m_comboMonth
->GetEffectiveMinSize();
827 wxSize sizeStatic
= m_staticMonth
->GetSize();
828 wxSize sizeSpin
= m_spinYear
->GetSize();
830 int maxHeight
= wxMax(sizeSpin
.y
, sizeCombo
.y
);
831 int dy
= (maxHeight
- sizeStatic
.y
) / 2;
833 m_comboMonth
->Move(x
, y
+ (maxHeight
- sizeCombo
.y
)/2 + 2); // FIXME, something is reporting the wrong size..
835 m_comboMonth
->Move(x
, y
+ (maxHeight
- sizeCombo
.y
)/2);
837 m_staticMonth
->SetSize(x
, y
+ dy
, sizeCombo
.x
, -1, sizeStatic
.y
);
839 int xDiff
= sizeCombo
.x
+ HORZ_MARGIN
;
841 m_spinYear
->SetSize(x
+ xDiff
, y
+ (maxHeight
- sizeSpin
.y
)/2, width
- xDiff
, maxHeight
);
842 m_staticYear
->SetSize(x
+ xDiff
, y
+ dy
, width
- xDiff
, sizeStatic
.y
);
844 yDiff
= maxHeight
+ VERT_MARGIN
;
846 else // no controls on the top
851 wxControl::DoMoveWindow(x
, y
+ yDiff
, width
, height
- yDiff
);
854 void wxGenericCalendarCtrl::DoGetSize(int *width
, int *height
) const
857 wxControl::DoGetSize( width
, height
);
859 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) && m_staticMonth
&& height
)
861 wxSize sizeCombo
= m_comboMonth
->GetEffectiveMinSize();
862 wxSize sizeSpin
= m_spinYear
->GetSize();
864 int maxHeight
= wxMax(sizeSpin
.y
, sizeCombo
.y
);
865 *height
+= maxHeight
+ VERT_MARGIN
;
868 wxControl::DoGetSize( width
, height
);
872 void wxGenericCalendarCtrl::RecalcGeometry()
876 dc
.SetFont(GetFont());
878 // determine the column width (weekday names are not necessarily wider
879 // than the numbers (in some languages), so let's not assume that they are)
881 for ( int day
= 10; day
<= 31; day
++)
884 dc
.GetTextExtent(wxString::Format(wxT("%d"), day
), &width
, &m_heightRow
);
885 if ( width
> m_widthCol
)
887 // 1.5 times the width gives nice margins even if the weekday
889 m_widthCol
= width
+width
/2;
892 wxDateTime::WeekDay wd
;
893 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
896 dc
.GetTextExtent(m_weekdays
[wd
], &width
, &m_heightRow
);
897 if ( width
> m_widthCol
)
903 // leave some margins
907 m_rowOffset
= HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) ? m_heightRow
: 0; // conditional in relation to style
910 // ----------------------------------------------------------------------------
912 // ----------------------------------------------------------------------------
914 void wxGenericCalendarCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
918 dc
.SetFont(GetFont());
923 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
924 m_date
.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
929 wxCoord x0
= wxMax( (GetSize().x
- m_widthCol
*7) /2 , 0 );
931 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
933 // draw the sequential month-selector
935 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
936 dc
.SetTextForeground(*wxBLACK
);
937 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxBRUSHSTYLE_SOLID
));
938 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxPENSTYLE_SOLID
));
939 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
941 // Get extent of month-name + year
942 wxCoord monthw
, monthh
;
943 wxString headertext
= m_date
.Format(wxT("%B %Y"));
944 dc
.GetTextExtent(headertext
, &monthw
, &monthh
);
946 // draw month-name centered above weekdays
947 wxCoord monthx
= ((m_widthCol
* 7) - monthw
) / 2 + x0
;
948 wxCoord monthy
= ((m_heightRow
- monthh
) / 2) + y
;
949 dc
.DrawText(headertext
, monthx
, monthy
);
951 // calculate the "month-arrows"
952 wxPoint leftarrow
[3];
953 wxPoint rightarrow
[3];
955 int arrowheight
= monthh
/ 2;
957 leftarrow
[0] = wxPoint(0, arrowheight
/ 2);
958 leftarrow
[1] = wxPoint(arrowheight
/ 2, 0);
959 leftarrow
[2] = wxPoint(arrowheight
/ 2, arrowheight
- 1);
961 rightarrow
[0] = wxPoint(0,0);
962 rightarrow
[1] = wxPoint(arrowheight
/ 2, arrowheight
/ 2);
963 rightarrow
[2] = wxPoint(0, arrowheight
- 1);
965 // draw the "month-arrows"
967 wxCoord arrowy
= (m_heightRow
- arrowheight
) / 2;
968 wxCoord larrowx
= (m_widthCol
- (arrowheight
/ 2)) / 2 + x0
;
969 wxCoord rarrowx
= ((m_widthCol
- (arrowheight
/ 2)) / 2) + m_widthCol
*6 + x0
;
970 m_leftArrowRect
= m_rightArrowRect
= wxRect(0,0,0,0);
972 if ( AllowMonthChange() )
974 wxDateTime ldpm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) - wxDateSpan::Day(); // last day prev month
975 // Check if range permits change
976 if ( IsDateInRange(ldpm
) && ( ( ldpm
.GetYear() == m_date
.GetYear() ) ? true : AllowYearChange() ) )
978 m_leftArrowRect
= wxRect(larrowx
- 3, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
979 dc
.SetBrush(*wxBLACK_BRUSH
);
980 dc
.SetPen(*wxBLACK_PEN
);
981 dc
.DrawPolygon(3, leftarrow
, larrowx
, arrowy
, wxWINDING_RULE
);
982 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
983 dc
.DrawRectangle(m_leftArrowRect
);
985 wxDateTime fdnm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) + wxDateSpan::Month(); // first day next month
986 if ( IsDateInRange(fdnm
) && ( ( fdnm
.GetYear() == m_date
.GetYear() ) ? true : AllowYearChange() ) )
988 m_rightArrowRect
= wxRect(rarrowx
- 4, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
989 dc
.SetBrush(*wxBLACK_BRUSH
);
990 dc
.SetPen(*wxBLACK_PEN
);
991 dc
.DrawPolygon(3, rightarrow
, rarrowx
, arrowy
, wxWINDING_RULE
);
992 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
993 dc
.DrawRectangle(m_rightArrowRect
);
1000 // first draw the week days
1001 if ( IsExposed(x0
, y
, x0
+ 7*m_widthCol
, m_heightRow
) )
1004 wxLogDebug("painting the header");
1007 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
1008 dc
.SetTextForeground(m_colHeaderFg
);
1009 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxBRUSHSTYLE_SOLID
));
1010 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxPENSTYLE_SOLID
));
1011 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
1013 bool startOnMonday
= (GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1014 for ( int wd
= 0; wd
< 7; wd
++ )
1017 if ( startOnMonday
)
1018 n
= wd
== 6 ? 0 : wd
+ 1;
1022 dc
.GetTextExtent(m_weekdays
[n
], &dayw
, &dayh
);
1023 dc
.DrawText(m_weekdays
[n
], x0
+ (wd
*m_widthCol
) + ((m_widthCol
- dayw
) / 2), y
); // center the day-name
1027 // then the calendar itself
1028 dc
.SetTextForeground(*wxBLACK
);
1029 //dc.SetFont(*wxNORMAL_FONT);
1032 wxDateTime date
= GetStartDate();
1035 wxLogDebug("starting calendar from %s\n",
1036 date
.Format("%a %d-%m-%Y %H:%M:%S").c_str());
1039 dc
.SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
1040 for ( size_t nWeek
= 1; nWeek
<= 6; nWeek
++, y
+= m_heightRow
)
1042 // if the update region doesn't intersect this row, don't paint it
1043 if ( !IsExposed(x0
, y
, x0
+ 7*m_widthCol
, m_heightRow
- 1) )
1045 date
+= wxDateSpan::Week();
1051 wxLogDebug("painting week %d at y = %d\n", nWeek
, y
);
1054 for ( int wd
= 0; wd
< 7; wd
++ )
1056 dc
.SetTextBackground(m_colBackground
);
1057 if ( IsDateShown(date
) )
1059 // don't use wxDate::Format() which prepends 0s
1060 unsigned int day
= date
.GetDay();
1061 wxString dayStr
= wxString::Format(_T("%u"), day
);
1063 dc
.GetTextExtent(dayStr
, &width
, (wxCoord
*)NULL
);
1065 bool changedColours
= false,
1066 changedFont
= false;
1069 wxCalendarDateAttr
*attr
= NULL
;
1071 if ( date
.GetMonth() != m_date
.GetMonth() || !IsDateInRange(date
) )
1073 // draw the days of adjacent months in different colour
1074 dc
.SetTextForeground(m_colSurrounding
);
1075 changedColours
= true;
1079 isSel
= date
.IsSameDate(m_date
);
1080 attr
= m_attrs
[day
- 1];
1084 dc
.SetTextForeground(m_colHighlightFg
);
1085 dc
.SetTextBackground(m_colHighlightBg
);
1087 changedColours
= true;
1091 wxColour colFg
, colBg
;
1093 if ( attr
->IsHoliday() )
1095 colFg
= m_colHolidayFg
;
1096 colBg
= m_colHolidayBg
;
1100 colFg
= attr
->GetTextColour();
1101 colBg
= attr
->GetBackgroundColour();
1106 dc
.SetTextForeground(colFg
);
1107 changedColours
= true;
1112 dc
.SetTextBackground(colBg
);
1113 changedColours
= true;
1116 if ( attr
->HasFont() )
1118 dc
.SetFont(attr
->GetFont());
1124 wxCoord x
= wd
*m_widthCol
+ (m_widthCol
- width
) / 2 + x0
;
1125 dc
.DrawText(dayStr
, x
, y
+ 1);
1127 if ( !isSel
&& attr
&& attr
->HasBorder() )
1130 if ( attr
->HasBorderColour() )
1132 colBorder
= attr
->GetBorderColour();
1136 colBorder
= GetForegroundColour();
1139 wxPen
pen(colBorder
, 1, wxPENSTYLE_SOLID
);
1141 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1143 switch ( attr
->GetBorder() )
1145 case wxCAL_BORDER_SQUARE
:
1146 dc
.DrawRectangle(x
- 2, y
,
1147 width
+ 4, m_heightRow
);
1150 case wxCAL_BORDER_ROUND
:
1151 dc
.DrawEllipse(x
- 2, y
,
1152 width
+ 4, m_heightRow
);
1156 wxFAIL_MSG(_T("unknown border type"));
1160 if ( changedColours
)
1162 dc
.SetTextForeground(GetForegroundColour());
1163 dc
.SetTextBackground(GetBackgroundColour());
1168 dc
.SetFont(GetFont());
1171 //else: just don't draw it
1173 date
+= wxDateSpan::Day();
1177 // Greying out out-of-range background
1178 bool showSurrounding
= (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) != 0;
1180 date
= ( showSurrounding
) ? GetStartDate() : wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear());
1181 if ( !IsDateInRange(date
) )
1183 wxDateTime firstOOR
= GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1185 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1186 oorbrush
.SetStyle(wxBRUSHSTYLE_FDIAGONAL_HATCH
);
1188 HighlightRange(&dc
, date
, firstOOR
, wxTRANSPARENT_PEN
, &oorbrush
);
1191 date
= ( showSurrounding
) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear());
1192 if ( !IsDateInRange(date
) )
1194 wxDateTime firstOOR
= GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1196 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1197 oorbrush
.SetStyle(wxBRUSHSTYLE_FDIAGONAL_HATCH
);
1199 HighlightRange(&dc
, firstOOR
, date
, wxTRANSPARENT_PEN
, &oorbrush
);
1203 wxLogDebug("+++ finished painting");
1207 void wxGenericCalendarCtrl::RefreshDate(const wxDateTime
& date
)
1213 // always refresh the whole row at once because our OnPaint() will draw
1214 // the whole row anyhow - and this allows the small optimisation in
1215 // OnClick() below to work
1216 rect
.x
= wxMax( (GetSize().x
- m_widthCol
*7) /2 , 0 );
1218 rect
.y
= (m_heightRow
* GetWeek(date
)) + m_rowOffset
;
1220 rect
.width
= 7*m_widthCol
;
1221 rect
.height
= m_heightRow
;
1224 // VZ: for some reason, the selected date seems to occupy more space under
1225 // MSW - this is probably some bug in the font size calculations, but I
1226 // don't know where exactly. This fix is ugly and leads to more
1227 // refreshes than really needed, but without it the selected days
1228 // leaves even more ugly underscores on screen.
1233 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
1236 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1239 Refresh(true, &rect
);
1242 void wxGenericCalendarCtrl::HighlightRange(wxPaintDC
* pDC
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, const wxPen
* pPen
, const wxBrush
* pBrush
)
1244 // Highlights the given range using pen and brush
1245 // Does nothing if todate < fromdate
1249 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate
.Format("%d %m %Y"), todate
.Format("%d %m %Y"));
1252 if ( todate
>= fromdate
)
1259 // implicit: both dates must be currently shown - checked by GetDateCoord
1260 if ( GetDateCoord(fromdate
, &fd
, &fw
) && GetDateCoord(todate
, &td
, &tw
) )
1263 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd
, fw
, td
, tw
);
1265 if ( ( (tw
- fw
) == 1 ) && ( td
< fd
) )
1267 // special case: interval 7 days or less not in same week
1268 // split in two separate intervals
1269 wxDateTime tfd
= fromdate
+ wxDateSpan::Days(7-fd
);
1270 wxDateTime ftd
= tfd
+ wxDateSpan::Day();
1272 wxLogDebug("Highlight: Separate segments");
1275 HighlightRange(pDC
, fromdate
, tfd
, pPen
, pBrush
);
1276 HighlightRange(pDC
, ftd
, todate
, pPen
, pBrush
);
1281 wxPoint corners
[8]; // potentially 8 corners in polygon
1282 wxCoord x0
= wxMax( (GetSize().x
- m_widthCol
*7) /2 , 0 );
1286 // simple case: same week
1288 corners
[0] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
);
1289 corners
[1] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, ((fw
+ 1 ) * m_heightRow
) + m_rowOffset
);
1290 corners
[2] = wxPoint(x0
+ td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
);
1291 corners
[3] = wxPoint(x0
+ td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
);
1296 // "complex" polygon
1297 corners
[cidx
] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1301 corners
[cidx
] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1302 corners
[cidx
] = wxPoint(x0
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1305 corners
[cidx
] = wxPoint(x0
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1306 corners
[cidx
] = wxPoint(x0
+ td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1310 corners
[cidx
] = wxPoint(x0
+ td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1311 corners
[cidx
] = wxPoint(x0
+ 7 * m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1314 corners
[cidx
] = wxPoint(x0
+ 7 * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1320 pDC
->SetBrush(*pBrush
);
1322 pDC
->DrawPolygon(numpoints
, corners
);
1328 wxLogDebug("--- HighlightRange ---");
1332 bool wxGenericCalendarCtrl::GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const
1337 wxLogDebug("+++ GetDateCoord: (%s) +++", date
.Format("%d %m %Y"));
1340 if ( IsDateShown(date
) )
1342 bool startOnMonday
= ( GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1345 *day
= date
.GetWeekDay();
1347 if ( *day
== 0 ) // sunday
1349 *day
= ( startOnMonday
) ? 7 : 1;
1353 *day
+= ( startOnMonday
) ? 0 : 1;
1356 int targetmonth
= date
.GetMonth() + (12 * date
.GetYear());
1357 int thismonth
= m_date
.GetMonth() + (12 * m_date
.GetYear());
1360 if ( targetmonth
== thismonth
)
1362 *week
= GetWeek(date
);
1366 if ( targetmonth
< thismonth
)
1368 *week
= 1; // trivial
1370 else // targetmonth > thismonth
1376 // get the datecoord of the last day in the month currently shown
1378 wxLogDebug(" +++ LDOM +++");
1380 GetDateCoord(ldcm
.SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear()), &lastday
, &lastweek
);
1382 wxLogDebug(" --- LDOM ---");
1385 wxTimeSpan span
= date
- ldcm
;
1387 int daysfromlast
= span
.GetDays();
1389 wxLogDebug("daysfromlast: %i", daysfromlast
);
1391 if ( daysfromlast
+ lastday
> 7 ) // past week boundary
1393 int wholeweeks
= (daysfromlast
/ 7);
1394 *week
= wholeweeks
+ lastweek
;
1395 if ( (daysfromlast
- (7 * wholeweeks
) + lastday
) > 7 )
1415 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date
.Format("%d %m %Y"), *day
, *week
);
1421 // ----------------------------------------------------------------------------
1423 // ----------------------------------------------------------------------------
1425 void wxGenericCalendarCtrl::OnDClick(wxMouseEvent
& event
)
1427 if ( HitTest(event
.GetPosition()) != wxCAL_HITTEST_DAY
)
1433 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1437 void wxGenericCalendarCtrl::OnClick(wxMouseEvent
& event
)
1440 wxDateTime::WeekDay wday
;
1441 switch ( HitTest(event
.GetPosition(), &date
, &wday
) )
1443 case wxCAL_HITTEST_DAY
:
1444 if ( IsDateInRange(date
) )
1448 GenerateEvent(wxEVT_CALENDAR_SEL_CHANGED
);
1450 // we know that the month/year never change when the user
1451 // clicks on the control so there is no need to call
1452 // GenerateAllChangeEvents() here, we know which event to send
1453 GenerateEvent(wxEVT_CALENDAR_DAY_CHANGED
);
1457 case wxCAL_HITTEST_HEADER
:
1459 wxCalendarEvent
eventWd(this, GetDate(),
1460 wxEVT_CALENDAR_WEEKDAY_CLICKED
);
1461 eventWd
.SetWeekDay(wday
);
1462 (void)GetEventHandler()->ProcessEvent(eventWd
);
1466 case wxCAL_HITTEST_DECMONTH
:
1467 case wxCAL_HITTEST_INCMONTH
:
1468 case wxCAL_HITTEST_SURROUNDING_WEEK
:
1469 SetDateAndNotify(date
); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1473 wxFAIL_MSG(_T("unknown hittest code"));
1476 case wxCAL_HITTEST_NOWHERE
:
1481 // as we don't (always) skip the message, we're not going to receive the
1482 // focus on click by default if we don't do it ourselves
1486 wxCalendarHitTestResult
wxGenericCalendarCtrl::HitTest(const wxPoint
& pos
,
1488 wxDateTime::WeekDay
*wd
)
1492 // the position where the calendar really begins
1493 wxCoord x0
= wxMax((GetSize().x
- m_widthCol
*7)/2, 0);
1495 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
1499 // we need to find out if the hit is on left arrow, on month or on right arrow
1501 if ( m_leftArrowRect
.Contains(pos
) )
1505 if ( IsDateInRange(m_date
- wxDateSpan::Month()) )
1507 *date
= m_date
- wxDateSpan::Month();
1511 *date
= GetLowerDateLimit();
1515 return wxCAL_HITTEST_DECMONTH
;
1518 if ( m_rightArrowRect
.Contains(pos
) )
1522 if ( IsDateInRange(m_date
+ wxDateSpan::Month()) )
1524 *date
= m_date
+ wxDateSpan::Month();
1528 *date
= GetUpperDateLimit();
1532 return wxCAL_HITTEST_INCMONTH
;
1537 // header: week days
1538 int wday
= (pos
.x
- x0
) / m_widthCol
;
1539 if ( pos
.y
< (m_heightRow
+ m_rowOffset
) )
1541 if ( pos
.y
> m_rowOffset
)
1545 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST
)
1547 wday
= wday
== 6 ? 0 : wday
+ 1;
1550 *wd
= (wxDateTime::WeekDay
)wday
;
1553 return wxCAL_HITTEST_HEADER
;
1557 return wxCAL_HITTEST_NOWHERE
;
1561 int week
= (pos
.y
- (m_heightRow
+ m_rowOffset
)) / m_heightRow
;
1562 if ( week
>= 6 || wday
>= 7 )
1564 return wxCAL_HITTEST_NOWHERE
;
1567 wxDateTime dt
= GetStartDate() + wxDateSpan::Days(7*week
+ wday
);
1569 if ( IsDateShown(dt
) )
1574 if ( dt
.GetMonth() == m_date
.GetMonth() )
1577 return wxCAL_HITTEST_DAY
;
1581 return wxCAL_HITTEST_SURROUNDING_WEEK
;
1586 return wxCAL_HITTEST_NOWHERE
;
1590 // ----------------------------------------------------------------------------
1591 // subcontrols events handling
1592 // ----------------------------------------------------------------------------
1594 void wxGenericCalendarCtrl::OnMonthChange(wxCommandEvent
& event
)
1596 wxDateTime::Tm tm
= m_date
.GetTm();
1598 wxDateTime::Month mon
= (wxDateTime::Month
)event
.GetInt();
1599 if ( tm
.mday
> wxDateTime::GetNumberOfDays(mon
, tm
.year
) )
1601 tm
.mday
= wxDateTime::GetNumberOfDays(mon
, tm
.year
);
1604 wxDateTime target
= wxDateTime(tm
.mday
, mon
, tm
.year
);
1606 ChangeMonth(&target
);
1607 SetDateAndNotify(target
);
1610 void wxGenericCalendarCtrl::HandleYearChange(wxCommandEvent
& event
)
1612 int year
= (int)event
.GetInt();
1613 if ( year
== INT_MIN
)
1615 // invalid year in the spin control, ignore it
1619 wxDateTime::Tm tm
= m_date
.GetTm();
1621 if ( tm
.mday
> wxDateTime::GetNumberOfDays(tm
.mon
, year
) )
1623 tm
.mday
= wxDateTime::GetNumberOfDays(tm
.mon
, year
);
1626 wxDateTime target
= wxDateTime(tm
.mday
, tm
.mon
, year
);
1628 if ( ChangeYear(&target
) )
1630 SetDateAndNotify(target
);
1634 // In this case we don't want to change the date. That would put us
1635 // inside the same year but a strange number of months forward/back..
1636 m_spinYear
->SetValue(target
.GetYear());
1640 void wxGenericCalendarCtrl::OnYearChange(wxSpinEvent
& event
)
1642 HandleYearChange( event
);
1645 void wxGenericCalendarCtrl::OnYearTextChange(wxCommandEvent
& event
)
1647 SetUserChangedYear();
1648 HandleYearChange(event
);
1651 // Responds to colour changes, and passes event on to children.
1652 void wxGenericCalendarCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1657 // Propagate the event to the children
1658 wxControl::OnSysColourChanged(event
);
1660 // Redraw control area
1661 SetBackgroundColour(m_colBackground
);
1665 // ----------------------------------------------------------------------------
1666 // keyboard interface
1667 // ----------------------------------------------------------------------------
1669 void wxGenericCalendarCtrl::OnChar(wxKeyEvent
& event
)
1672 switch ( event
.GetKeyCode() )
1676 target
= m_date
+ wxDateSpan::Year();
1677 if ( ChangeYear(&target
) )
1679 SetDateAndNotify(target
);
1685 target
= m_date
- wxDateSpan::Year();
1686 if ( ChangeYear(&target
) )
1688 SetDateAndNotify(target
);
1693 target
= m_date
- wxDateSpan::Month();
1694 ChangeMonth(&target
);
1695 SetDateAndNotify(target
); // always
1699 target
= m_date
+ wxDateSpan::Month();
1700 ChangeMonth(&target
);
1701 SetDateAndNotify(target
); // always
1705 if ( event
.ControlDown() )
1707 target
= wxDateTime(m_date
).SetToNextWeekDay(
1708 GetWindowStyle() & wxCAL_MONDAY_FIRST
1709 ? wxDateTime::Sun
: wxDateTime::Sat
);
1710 if ( !IsDateInRange(target
) )
1712 target
= GetUpperDateLimit();
1714 SetDateAndNotify(target
);
1717 SetDateAndNotify(m_date
+ wxDateSpan::Day());
1721 if ( event
.ControlDown() )
1723 target
= wxDateTime(m_date
).SetToPrevWeekDay(
1724 GetWindowStyle() & wxCAL_MONDAY_FIRST
1725 ? wxDateTime::Mon
: wxDateTime::Sun
);
1726 if ( !IsDateInRange(target
) )
1728 target
= GetLowerDateLimit();
1730 SetDateAndNotify(target
);
1733 SetDateAndNotify(m_date
- wxDateSpan::Day());
1737 SetDateAndNotify(m_date
- wxDateSpan::Week());
1741 SetDateAndNotify(m_date
+ wxDateSpan::Week());
1745 if ( event
.ControlDown() )
1746 SetDateAndNotify(wxDateTime::Today());
1748 SetDateAndNotify(wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear()));
1752 SetDateAndNotify(wxDateTime(m_date
).SetToLastMonthDay());
1756 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1764 // ----------------------------------------------------------------------------
1765 // holidays handling
1766 // ----------------------------------------------------------------------------
1768 void wxGenericCalendarCtrl::SetHoliday(size_t day
)
1770 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in SetHoliday") );
1772 wxCalendarDateAttr
*attr
= GetAttr(day
);
1775 attr
= new wxCalendarDateAttr
;
1778 attr
->SetHoliday(true);
1780 // can't use SetAttr() because it would delete this pointer
1781 m_attrs
[day
- 1] = attr
;
1784 void wxGenericCalendarCtrl::ResetHolidayAttrs()
1786 for ( size_t day
= 0; day
< 31; day
++ )
1790 m_attrs
[day
]->SetHoliday(false);
1795 void wxGenericCalendarCtrl::Mark(size_t day
, bool mark
)
1797 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in Mark") );
1799 const wxCalendarDateAttr
& m
= wxCalendarDateAttr::GetMark();
1801 if ( m_attrs
[day
- 1] )
1802 AddAttr(m_attrs
[day
- 1], m
);
1804 SetAttr(day
, new wxCalendarDateAttr(m
));
1807 if ( m_attrs
[day
- 1] )
1808 DelAttr(m_attrs
[day
- 1], m
);
1814 wxGenericCalendarCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
1816 // Use the same color scheme as wxListBox
1817 return wxListBox::GetClassDefaultAttributes(variant
);
1820 #endif // wxUSE_CALENDARCTRL