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
)
99 wxFLAGS_MEMBER(wxCAL_SHOW_WEEK_NUMBERS
)
101 wxEND_FLAGS( wxCalendarCtrlStyle
)
103 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericCalendarCtrl
, wxControl
,"wx/calctrl.h")
105 wxBEGIN_PROPERTIES_TABLE(wxGenericCalendarCtrl
)
106 wxEVENT_RANGE_PROPERTY( Updated
, wxEVT_CALENDAR_SEL_CHANGED
, wxEVT_CALENDAR_WEEK_CLICKED
, wxCalendarEvent
)
107 wxHIDE_PROPERTY( Children
)
108 wxPROPERTY( Date
,wxDateTime
, SetDate
, GetDate
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
109 wxPROPERTY_FLAGS( WindowStyle
, wxCalendarCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
110 wxEND_PROPERTIES_TABLE()
112 wxBEGIN_HANDLERS_TABLE(wxGenericCalendarCtrl
)
113 wxEND_HANDLERS_TABLE()
115 wxCONSTRUCTOR_6( wxGenericCalendarCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxDateTime
, Date
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
117 IMPLEMENT_DYNAMIC_CLASS(wxGenericCalendarCtrl
, wxControl
)
120 // ============================================================================
122 // ============================================================================
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
131 // add attributes that are set in attr
132 void AddAttr(wxCalendarDateAttr
*self
, const wxCalendarDateAttr
& attr
)
134 if (attr
.HasTextColour())
135 self
->SetTextColour(attr
.GetTextColour());
136 if (attr
.HasBackgroundColour())
137 self
->SetBackgroundColour(attr
.GetBackgroundColour());
138 if (attr
.HasBorderColour())
139 self
->SetBorderColour(attr
.GetBorderColour());
141 self
->SetFont(attr
.GetFont());
142 if (attr
.HasBorder())
143 self
->SetBorder(attr
.GetBorder());
144 if (attr
.IsHoliday())
145 self
->SetHoliday(true);
148 // remove attributes that are set in attr
149 void DelAttr(wxCalendarDateAttr
*self
, const wxCalendarDateAttr
&attr
)
151 if (attr
.HasTextColour())
152 self
->SetTextColour(wxNullColour
);
153 if (attr
.HasBackgroundColour())
154 self
->SetBackgroundColour(wxNullColour
);
155 if (attr
.HasBorderColour())
156 self
->SetBorderColour(wxNullColour
);
158 self
->SetFont(wxNullFont
);
159 if (attr
.HasBorder())
160 self
->SetBorder(wxCAL_BORDER_NONE
);
161 if (attr
.IsHoliday())
162 self
->SetHoliday(false);
165 } // anonymous namespace
167 // ----------------------------------------------------------------------------
168 // wxGenericCalendarCtrl
169 // ----------------------------------------------------------------------------
171 wxGenericCalendarCtrl::wxGenericCalendarCtrl(wxWindow
*parent
,
173 const wxDateTime
& date
,
177 const wxString
& name
)
181 (void)Create(parent
, id
, date
, pos
, size
, style
, name
);
184 void wxGenericCalendarCtrl::Init()
189 m_staticMonth
= NULL
;
191 m_userChangedYear
= false;
195 m_calendarWeekWidth
= 0;
197 wxDateTime::WeekDay wd
;
198 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
200 m_weekdays
[wd
] = wxDateTime::GetWeekDayName(wd
, wxDateTime::Name_Abbr
);
203 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
211 void wxGenericCalendarCtrl::InitColours()
213 m_colHighlightFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
214 m_colHighlightBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
215 m_colBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
216 m_colSurrounding
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
218 m_colHolidayFg
= *wxRED
;
219 // don't set m_colHolidayBg - by default, same as our bg colour
221 m_colHeaderFg
= *wxBLUE
;
222 m_colHeaderBg
= *wxLIGHT_GREY
;
225 bool wxGenericCalendarCtrl::Create(wxWindow
*parent
,
227 const wxDateTime
& date
,
231 const wxString
& name
)
233 if ( !wxControl::Create(parent
, id
, pos
, size
,
234 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
| wxFULL_REPAINT_ON_RESIZE
,
235 wxDefaultValidator
, name
) )
240 // needed to get the arrow keys normally used for the dialog navigation
241 SetWindowStyle(style
| wxWANTS_CHARS
);
243 m_date
= date
.IsValid() ? date
: wxDateTime::Today();
245 m_lowdate
= wxDefaultDateTime
;
246 m_highdate
= wxDefaultDateTime
;
248 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
250 CreateYearSpinCtrl();
251 m_staticYear
= new wxStaticText(GetParent(), wxID_ANY
, m_date
.Format(_T("%Y")),
252 wxDefaultPosition
, wxDefaultSize
,
254 CreateMonthComboBox();
255 m_staticMonth
= new wxStaticText(GetParent(), wxID_ANY
, m_date
.Format(_T("%B")),
256 wxDefaultPosition
, wxDefaultSize
,
260 ShowCurrentControls();
262 // we need to set the position as well because the main control position
263 // is not the same as the one specified in pos if we have the controls
265 SetInitialSize(size
);
268 // Since we don't paint the whole background make sure that the platform
269 // will use the right one.
270 SetBackgroundColour(m_colBackground
);
277 wxGenericCalendarCtrl::~wxGenericCalendarCtrl()
279 for ( size_t n
= 0; n
< WXSIZEOF(m_attrs
); n
++ )
284 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
287 delete m_staticMonth
;
293 void wxGenericCalendarCtrl::SetWindowStyleFlag(long style
)
295 // changing this style doesn't work because the controls are not
296 // created/shown/hidden accordingly
297 wxASSERT_MSG( (style
& wxCAL_SEQUENTIAL_MONTH_SELECTION
) ==
298 (m_windowStyle
& wxCAL_SEQUENTIAL_MONTH_SELECTION
),
299 _T("wxCAL_SEQUENTIAL_MONTH_SELECTION can't be changed after creation") );
301 wxControl::SetWindowStyleFlag(style
);
304 // ----------------------------------------------------------------------------
305 // Create the wxComboBox and wxSpinCtrl
306 // ----------------------------------------------------------------------------
308 void wxGenericCalendarCtrl::CreateMonthComboBox()
310 m_comboMonth
= new wxComboBox(GetParent(), wxID_ANY
,
315 wxCB_READONLY
| wxCLIP_SIBLINGS
);
318 for ( m
= wxDateTime::Jan
; m
< wxDateTime::Inv_Month
; wxNextMonth(m
) )
320 m_comboMonth
->Append(wxDateTime::GetMonthName(m
));
323 m_comboMonth
->SetSelection(GetDate().GetMonth());
324 m_comboMonth
->SetSize(wxDefaultCoord
,
328 wxSIZE_AUTO_WIDTH
|wxSIZE_AUTO_HEIGHT
);
330 m_comboMonth
->Connect(m_comboMonth
->GetId(), wxEVT_COMMAND_COMBOBOX_SELECTED
,
331 wxCommandEventHandler(wxGenericCalendarCtrl::OnMonthChange
),
335 void wxGenericCalendarCtrl::CreateYearSpinCtrl()
337 m_spinYear
= new wxSpinCtrl(GetParent(), wxID_ANY
,
338 GetDate().Format(_T("%Y")),
341 wxSP_ARROW_KEYS
| wxCLIP_SIBLINGS
,
342 -4300, 10000, GetDate().GetYear());
344 m_spinYear
->SetSize( 90, -1 );
347 m_spinYear
->Connect(m_spinYear
->GetId(), wxEVT_COMMAND_TEXT_UPDATED
,
348 wxCommandEventHandler(wxGenericCalendarCtrl::OnYearTextChange
),
351 m_spinYear
->Connect(m_spinYear
->GetId(), wxEVT_COMMAND_SPINCTRL_UPDATED
,
352 wxSpinEventHandler(wxGenericCalendarCtrl::OnYearChange
),
356 // ----------------------------------------------------------------------------
357 // forward wxWin functions to subcontrols
358 // ----------------------------------------------------------------------------
360 bool wxGenericCalendarCtrl::Destroy()
363 m_staticYear
->Destroy();
365 m_spinYear
->Destroy();
367 m_comboMonth
->Destroy();
369 m_staticMonth
->Destroy();
374 m_staticMonth
= NULL
;
376 return wxControl::Destroy();
379 bool wxGenericCalendarCtrl::Show(bool show
)
381 if ( !wxControl::Show(show
) )
386 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
388 if ( GetMonthControl() )
390 GetMonthControl()->Show(show
);
391 GetYearControl()->Show(show
);
398 bool wxGenericCalendarCtrl::Enable(bool enable
)
400 if ( !wxControl::Enable(enable
) )
405 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
407 GetMonthControl()->Enable(enable
);
408 GetYearControl()->Enable(enable
);
414 // ----------------------------------------------------------------------------
415 // enable/disable month/year controls
416 // ----------------------------------------------------------------------------
418 void wxGenericCalendarCtrl::ShowCurrentControls()
420 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
422 if ( AllowMonthChange() )
424 m_comboMonth
->Show();
425 m_staticMonth
->Hide();
427 if ( AllowYearChange() )
430 m_staticYear
->Hide();
438 m_comboMonth
->Hide();
439 m_staticMonth
->Show();
442 // year change not allowed here
444 m_staticYear
->Show();
446 //else: these controls are not even created, don't show/hide them
449 wxControl
*wxGenericCalendarCtrl::GetMonthControl() const
451 return AllowMonthChange() ? (wxControl
*)m_comboMonth
: (wxControl
*)m_staticMonth
;
454 wxControl
*wxGenericCalendarCtrl::GetYearControl() const
456 return AllowYearChange() ? (wxControl
*)m_spinYear
: (wxControl
*)m_staticYear
;
459 void wxGenericCalendarCtrl::EnableYearChange(bool enable
)
461 if ( enable
!= AllowYearChange() )
463 long style
= GetWindowStyle();
465 style
&= ~wxCAL_NO_YEAR_CHANGE
;
467 style
|= wxCAL_NO_YEAR_CHANGE
;
468 SetWindowStyle(style
);
470 ShowCurrentControls();
471 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
478 bool wxGenericCalendarCtrl::EnableMonthChange(bool enable
)
480 if ( !wxCalendarCtrlBase::EnableMonthChange(enable
) )
483 ShowCurrentControls();
484 if ( GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
)
490 // ----------------------------------------------------------------------------
492 // ----------------------------------------------------------------------------
494 bool wxGenericCalendarCtrl::SetDate(const wxDateTime
& date
)
498 bool sameMonth
= m_date
.GetMonth() == date
.GetMonth(),
499 sameYear
= m_date
.GetYear() == date
.GetYear();
501 if ( IsDateInRange(date
) )
503 if ( sameMonth
&& sameYear
)
505 // just change the day
510 if ( AllowMonthChange() && (AllowYearChange() || sameYear
) )
515 if ( !(GetWindowStyle() & wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
517 // update the controls
518 m_comboMonth
->SetSelection(m_date
.GetMonth());
520 if ( AllowYearChange() )
522 if ( !m_userChangedYear
)
523 m_spinYear
->SetValue(m_date
.Format(_T("%Y")));
527 // as the month changed, holidays did too
530 // update the calendar
541 m_userChangedYear
= false;
546 void wxGenericCalendarCtrl::ChangeDay(const wxDateTime
& date
)
548 if ( m_date
!= date
)
550 // we need to refresh the row containing the old date and the one
551 // containing the new one
552 wxDateTime dateOld
= m_date
;
555 RefreshDate(dateOld
);
557 // if the date is in the same row, it was already drawn correctly
558 if ( GetWeek(m_date
) != GetWeek(dateOld
) )
565 void wxGenericCalendarCtrl::SetDateAndNotify(const wxDateTime
& date
)
567 const wxDateTime dateOld
= GetDate();
568 if ( date
!= dateOld
&& SetDate(date
) )
570 GenerateAllChangeEvents(dateOld
);
574 // ----------------------------------------------------------------------------
576 // ----------------------------------------------------------------------------
578 bool wxGenericCalendarCtrl::SetLowerDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
582 if ( !(date
.IsValid()) || ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : true ) )
594 bool wxGenericCalendarCtrl::SetUpperDateLimit(const wxDateTime
& date
/* = wxDefaultDateTime */)
598 if ( !(date
.IsValid()) || ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : true ) )
610 bool wxGenericCalendarCtrl::SetDateRange(const wxDateTime
& lowerdate
/* = wxDefaultDateTime */, const wxDateTime
& upperdate
/* = wxDefaultDateTime */)
615 ( !( lowerdate
.IsValid() ) || ( ( upperdate
.IsValid() ) ? ( lowerdate
<= upperdate
) : true ) ) &&
616 ( !( upperdate
.IsValid() ) || ( ( lowerdate
.IsValid() ) ? ( upperdate
>= lowerdate
) : true ) ) )
618 m_lowdate
= lowerdate
;
619 m_highdate
= upperdate
;
629 bool wxGenericCalendarCtrl::GetDateRange(wxDateTime
*lowerdate
,
630 wxDateTime
*upperdate
) const
633 *lowerdate
= m_lowdate
;
635 *upperdate
= m_highdate
;
637 return m_lowdate
.IsValid() || m_highdate
.IsValid();
640 // ----------------------------------------------------------------------------
642 // ----------------------------------------------------------------------------
644 wxDateTime
wxGenericCalendarCtrl::GetStartDate() const
646 wxDateTime::Tm tm
= m_date
.GetTm();
648 wxDateTime date
= wxDateTime(1, tm
.mon
, tm
.year
);
651 date
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
652 ? wxDateTime::Mon
: wxDateTime::Sun
);
654 if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
)
656 // We want to offset the calendar if we start on the first..
657 if ( date
.GetDay() == 1 )
659 date
-= wxDateSpan::Week();
666 bool wxGenericCalendarCtrl::IsDateShown(const wxDateTime
& date
) const
668 if ( !(GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
670 return date
.GetMonth() == m_date
.GetMonth();
678 bool wxGenericCalendarCtrl::IsDateInRange(const wxDateTime
& date
) const
680 // Check if the given date is in the range specified
681 return ( ( ( m_lowdate
.IsValid() ) ? ( date
>= m_lowdate
) : true )
682 && ( ( m_highdate
.IsValid() ) ? ( date
<= m_highdate
) : true ) );
685 bool wxGenericCalendarCtrl::ChangeYear(wxDateTime
* target
) const
689 if ( !(IsDateInRange(*target
)) )
691 if ( target
->GetYear() < m_date
.GetYear() )
693 if ( target
->GetYear() >= GetLowerDateLimit().GetYear() )
695 *target
= GetLowerDateLimit();
705 if ( target
->GetYear() <= GetUpperDateLimit().GetYear() )
707 *target
= GetUpperDateLimit();
724 bool wxGenericCalendarCtrl::ChangeMonth(wxDateTime
* target
) const
728 if ( !(IsDateInRange(*target
)) )
732 if ( target
->GetMonth() < m_date
.GetMonth() )
734 *target
= GetLowerDateLimit();
738 *target
= GetUpperDateLimit();
745 size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime
& date
) const
747 size_t retval
= date
.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
748 ? wxDateTime::Monday_First
749 : wxDateTime::Sunday_First
);
751 if ( (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) )
753 // we need to offset an extra week if we "start" on the 1st of the month
754 wxDateTime::Tm tm
= date
.GetTm();
756 wxDateTime datetest
= wxDateTime(1, tm
.mon
, tm
.year
);
759 datetest
.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
760 ? wxDateTime::Mon
: wxDateTime::Sun
);
762 if ( datetest
.GetDay() == 1 )
771 // ----------------------------------------------------------------------------
773 // ----------------------------------------------------------------------------
775 // this is a composite control and it must arrange its parts each time its
776 // size or position changes: the combobox and spinctrl are along the top of
777 // the available area and the calendar takes up the rest of the space
779 // the static controls are supposed to be always smaller than combo/spin so we
780 // always use the latter for size calculations and position the static to take
783 // the constants used for the layout
784 #define VERT_MARGIN 5 // distance between combo and calendar
785 #define HORZ_MARGIN 5 // spin
787 wxSize
wxGenericCalendarCtrl::DoGetBestSize() const
789 // calc the size of the calendar
790 const_cast<wxGenericCalendarCtrl
*>(this)->RecalcGeometry();
792 wxCoord width
= 7*m_widthCol
+ m_calendarWeekWidth
,
793 height
= 7*m_heightRow
+ m_rowOffset
+ VERT_MARGIN
;
795 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
797 const wxSize bestSizeCombo
= m_comboMonth
->GetBestSize();
799 height
+= wxMax(bestSizeCombo
.y
, m_spinYear
->GetBestSize().y
)
802 // the spin control get clipped otherwise
806 wxCoord w2
= bestSizeCombo
.x
+ HORZ_MARGIN
+ GetCharWidth()*6;
811 wxSize
best(width
, height
);
812 if ( !HasFlag(wxBORDER_NONE
) )
814 best
+= GetWindowBorderSize();
822 void wxGenericCalendarCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
826 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) && m_staticMonth
)
828 wxSize sizeCombo
= m_comboMonth
->GetEffectiveMinSize();
829 wxSize sizeStatic
= m_staticMonth
->GetSize();
830 wxSize sizeSpin
= m_spinYear
->GetSize();
832 int maxHeight
= wxMax(sizeSpin
.y
, sizeCombo
.y
);
833 int dy
= (maxHeight
- sizeStatic
.y
) / 2;
835 m_comboMonth
->Move(x
, y
+ (maxHeight
- sizeCombo
.y
)/2 + 2); // FIXME, something is reporting the wrong size..
837 m_comboMonth
->Move(x
, y
+ (maxHeight
- sizeCombo
.y
)/2);
839 m_staticMonth
->SetSize(x
, y
+ dy
, sizeCombo
.x
, -1, sizeStatic
.y
);
841 int xDiff
= sizeCombo
.x
+ HORZ_MARGIN
;
843 m_spinYear
->SetSize(x
+ xDiff
, y
+ (maxHeight
- sizeSpin
.y
)/2, width
- xDiff
, maxHeight
);
844 m_staticYear
->SetSize(x
+ xDiff
, y
+ dy
, width
- xDiff
, sizeStatic
.y
);
846 yDiff
= maxHeight
+ VERT_MARGIN
;
848 else // no controls on the top
853 wxControl::DoMoveWindow(x
, y
+ yDiff
, width
, height
- yDiff
);
856 void wxGenericCalendarCtrl::DoGetSize(int *width
, int *height
) const
859 wxControl::DoGetSize( width
, height
);
861 if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) && m_staticMonth
&& height
)
863 wxSize sizeCombo
= m_comboMonth
->GetEffectiveMinSize();
864 wxSize sizeSpin
= m_spinYear
->GetSize();
866 int maxHeight
= wxMax(sizeSpin
.y
, sizeCombo
.y
);
867 *height
+= maxHeight
+ VERT_MARGIN
;
870 wxControl::DoGetSize( width
, height
);
874 void wxGenericCalendarCtrl::RecalcGeometry()
878 dc
.SetFont(GetFont());
880 // determine the column width (weekday names are not necessarily wider
881 // than the numbers (in some languages), so let's not assume that they are)
883 for ( int day
= 10; day
<= 31; day
++)
886 dc
.GetTextExtent(wxString::Format(wxT("%d"), day
), &width
, &m_heightRow
);
887 if ( width
> m_widthCol
)
889 // 1.5 times the width gives nice margins even if the weekday
891 m_widthCol
= width
+width
/2;
894 wxDateTime::WeekDay wd
;
895 for ( wd
= wxDateTime::Sun
; wd
< wxDateTime::Inv_WeekDay
; wxNextWDay(wd
) )
898 dc
.GetTextExtent(m_weekdays
[wd
], &width
, &m_heightRow
);
899 if ( width
> m_widthCol
)
905 m_calendarWeekWidth
= HasFlag( wxCAL_SHOW_WEEK_NUMBERS
)
906 ? dc
.GetTextExtent( wxString::Format( wxT( "%d" ), 42 )).GetWidth() + 4 : 0;
908 // leave some margins
912 m_rowOffset
= HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) ? m_heightRow
: 0; // conditional in relation to style
915 // ----------------------------------------------------------------------------
917 // ----------------------------------------------------------------------------
919 void wxGenericCalendarCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
923 dc
.SetFont(GetFont());
928 wxLogDebug("--- starting to paint, selection: %s, week %u\n",
929 m_date
.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
934 wxCoord x0
= m_calendarWeekWidth
;
936 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
938 // draw the sequential month-selector
940 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
941 dc
.SetTextForeground(*wxBLACK
);
942 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxBRUSHSTYLE_SOLID
));
943 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxPENSTYLE_SOLID
));
944 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
946 // Get extent of month-name + year
947 wxCoord monthw
, monthh
;
948 wxString headertext
= m_date
.Format(wxT("%B %Y"));
949 dc
.GetTextExtent(headertext
, &monthw
, &monthh
);
951 // draw month-name centered above weekdays
952 wxCoord monthx
= ((m_widthCol
* 7) - monthw
) / 2 + x0
;
953 wxCoord monthy
= ((m_heightRow
- monthh
) / 2) + y
;
954 dc
.DrawText(headertext
, monthx
, monthy
);
956 // calculate the "month-arrows"
957 wxPoint leftarrow
[3];
958 wxPoint rightarrow
[3];
960 int arrowheight
= monthh
/ 2;
962 leftarrow
[0] = wxPoint(0, arrowheight
/ 2);
963 leftarrow
[1] = wxPoint(arrowheight
/ 2, 0);
964 leftarrow
[2] = wxPoint(arrowheight
/ 2, arrowheight
- 1);
966 rightarrow
[0] = wxPoint(0,0);
967 rightarrow
[1] = wxPoint(arrowheight
/ 2, arrowheight
/ 2);
968 rightarrow
[2] = wxPoint(0, arrowheight
- 1);
970 // draw the "month-arrows"
972 wxCoord arrowy
= (m_heightRow
- arrowheight
) / 2;
973 wxCoord larrowx
= (m_widthCol
- (arrowheight
/ 2)) / 2 + x0
;
974 wxCoord rarrowx
= ((m_widthCol
- (arrowheight
/ 2)) / 2) + m_widthCol
*6 + x0
;
975 m_leftArrowRect
= m_rightArrowRect
= wxRect(0,0,0,0);
977 if ( AllowMonthChange() )
979 wxDateTime ldpm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) - wxDateSpan::Day(); // last day prev month
980 // Check if range permits change
981 if ( IsDateInRange(ldpm
) && ( ( ldpm
.GetYear() == m_date
.GetYear() ) ? true : AllowYearChange() ) )
983 m_leftArrowRect
= wxRect(larrowx
- 3, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
984 dc
.SetBrush(*wxBLACK_BRUSH
);
985 dc
.SetPen(*wxBLACK_PEN
);
986 dc
.DrawPolygon(3, leftarrow
, larrowx
, arrowy
, wxWINDING_RULE
);
987 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
988 dc
.DrawRectangle(m_leftArrowRect
);
990 wxDateTime fdnm
= wxDateTime(1,m_date
.GetMonth(), m_date
.GetYear()) + wxDateSpan::Month(); // first day next month
991 if ( IsDateInRange(fdnm
) && ( ( fdnm
.GetYear() == m_date
.GetYear() ) ? true : AllowYearChange() ) )
993 m_rightArrowRect
= wxRect(rarrowx
- 4, arrowy
- 3, (arrowheight
/ 2) + 8, (arrowheight
+ 6));
994 dc
.SetBrush(*wxBLACK_BRUSH
);
995 dc
.SetPen(*wxBLACK_PEN
);
996 dc
.DrawPolygon(3, rightarrow
, rarrowx
, arrowy
, wxWINDING_RULE
);
997 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
998 dc
.DrawRectangle(m_rightArrowRect
);
1005 // first draw the week days
1006 if ( IsExposed(x0
, y
, x0
+ 7*m_widthCol
, m_heightRow
) )
1009 wxLogDebug("painting the header");
1012 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
1013 dc
.SetTextForeground(m_colHeaderFg
);
1014 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxBRUSHSTYLE_SOLID
));
1015 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxPENSTYLE_SOLID
));
1016 dc
.DrawRectangle(0, y
, GetClientSize().x
, m_heightRow
);
1018 bool startOnMonday
= (GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1019 for ( int wd
= 0; wd
< 7; wd
++ )
1022 if ( startOnMonday
)
1023 n
= wd
== 6 ? 0 : wd
+ 1;
1027 dc
.GetTextExtent(m_weekdays
[n
], &dayw
, &dayh
);
1028 dc
.DrawText(m_weekdays
[n
], x0
+ (wd
*m_widthCol
) + ((m_widthCol
- dayw
) / 2), y
); // center the day-name
1032 // then the calendar itself
1033 dc
.SetTextForeground(*wxBLACK
);
1034 //dc.SetFont(*wxNORMAL_FONT);
1038 // draw column with calendar week nr
1039 if ( HasFlag( wxCAL_SHOW_WEEK_NUMBERS
) && IsExposed( 0, y
, m_calendarWeekWidth
, m_heightRow
* 6 ))
1041 dc
.SetBackgroundMode(wxTRANSPARENT
);
1042 dc
.SetBrush(wxBrush(m_colHeaderBg
, wxSOLID
));
1043 dc
.SetPen(wxPen(m_colHeaderBg
, 1, wxSOLID
));
1044 dc
.DrawRectangle( 0, y
, m_calendarWeekWidth
, m_heightRow
* 6 );
1045 wxDateTime date
= GetStartDate();
1046 for ( size_t i
= 0; i
< 6; ++i
)
1048 const int weekNr
= date
.GetWeekOfYear();
1049 wxString text
= wxString::Format( wxT( "%d" ), weekNr
);
1050 dc
.DrawText( text
, m_calendarWeekWidth
- dc
.GetTextExtent( text
).GetWidth() - 2, y
+ m_heightRow
* i
);
1051 date
+= wxDateSpan::Week();
1055 wxDateTime date
= GetStartDate();
1058 wxLogDebug("starting calendar from %s\n",
1059 date
.Format("%a %d-%m-%Y %H:%M:%S").c_str());
1062 dc
.SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
1063 for ( size_t nWeek
= 1; nWeek
<= 6; nWeek
++, y
+= m_heightRow
)
1065 // if the update region doesn't intersect this row, don't paint it
1066 if ( !IsExposed(x0
, y
, x0
+ 7*m_widthCol
, m_heightRow
- 1) )
1068 date
+= wxDateSpan::Week();
1074 wxLogDebug("painting week %d at y = %d\n", nWeek
, y
);
1077 for ( int wd
= 0; wd
< 7; wd
++ )
1079 dc
.SetTextBackground(m_colBackground
);
1080 if ( IsDateShown(date
) )
1082 // don't use wxDate::Format() which prepends 0s
1083 unsigned int day
= date
.GetDay();
1084 wxString dayStr
= wxString::Format(_T("%u"), day
);
1086 dc
.GetTextExtent(dayStr
, &width
, NULL
);
1088 bool changedColours
= false,
1089 changedFont
= false;
1092 wxCalendarDateAttr
*attr
= NULL
;
1094 if ( date
.GetMonth() != m_date
.GetMonth() || !IsDateInRange(date
) )
1096 // draw the days of adjacent months in different colour
1097 dc
.SetTextForeground(m_colSurrounding
);
1098 changedColours
= true;
1102 isSel
= date
.IsSameDate(m_date
);
1103 attr
= m_attrs
[day
- 1];
1107 dc
.SetTextForeground(m_colHighlightFg
);
1108 dc
.SetTextBackground(m_colHighlightBg
);
1110 changedColours
= true;
1114 wxColour colFg
, colBg
;
1116 if ( attr
->IsHoliday() )
1118 colFg
= m_colHolidayFg
;
1119 colBg
= m_colHolidayBg
;
1123 colFg
= attr
->GetTextColour();
1124 colBg
= attr
->GetBackgroundColour();
1129 dc
.SetTextForeground(colFg
);
1130 changedColours
= true;
1135 dc
.SetTextBackground(colBg
);
1136 changedColours
= true;
1139 if ( attr
->HasFont() )
1141 dc
.SetFont(attr
->GetFont());
1147 wxCoord x
= wd
*m_widthCol
+ (m_widthCol
- width
) / 2 + x0
;
1148 dc
.DrawText(dayStr
, x
, y
+ 1);
1150 if ( !isSel
&& attr
&& attr
->HasBorder() )
1153 if ( attr
->HasBorderColour() )
1155 colBorder
= attr
->GetBorderColour();
1159 colBorder
= GetForegroundColour();
1162 wxPen
pen(colBorder
, 1, wxPENSTYLE_SOLID
);
1164 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1166 switch ( attr
->GetBorder() )
1168 case wxCAL_BORDER_SQUARE
:
1169 dc
.DrawRectangle(x
- 2, y
,
1170 width
+ 4, m_heightRow
);
1173 case wxCAL_BORDER_ROUND
:
1174 dc
.DrawEllipse(x
- 2, y
,
1175 width
+ 4, m_heightRow
);
1179 wxFAIL_MSG(_T("unknown border type"));
1183 if ( changedColours
)
1185 dc
.SetTextForeground(GetForegroundColour());
1186 dc
.SetTextBackground(GetBackgroundColour());
1191 dc
.SetFont(GetFont());
1194 //else: just don't draw it
1196 date
+= wxDateSpan::Day();
1200 // Greying out out-of-range background
1201 bool showSurrounding
= (GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS
) != 0;
1203 date
= ( showSurrounding
) ? GetStartDate() : wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear());
1204 if ( !IsDateInRange(date
) )
1206 wxDateTime firstOOR
= GetLowerDateLimit() - wxDateSpan::Day(); // first out-of-range
1208 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1209 oorbrush
.SetStyle(wxBRUSHSTYLE_FDIAGONAL_HATCH
);
1211 HighlightRange(&dc
, date
, firstOOR
, wxTRANSPARENT_PEN
, &oorbrush
);
1214 date
= ( showSurrounding
) ? GetStartDate() + wxDateSpan::Weeks(6) - wxDateSpan::Day() : wxDateTime().SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear());
1215 if ( !IsDateInRange(date
) )
1217 wxDateTime firstOOR
= GetUpperDateLimit() + wxDateSpan::Day(); // first out-of-range
1219 wxBrush oorbrush
= *wxLIGHT_GREY_BRUSH
;
1220 oorbrush
.SetStyle(wxBRUSHSTYLE_FDIAGONAL_HATCH
);
1222 HighlightRange(&dc
, firstOOR
, date
, wxTRANSPARENT_PEN
, &oorbrush
);
1226 wxLogDebug("+++ finished painting");
1230 void wxGenericCalendarCtrl::RefreshDate(const wxDateTime
& date
)
1236 // always refresh the whole row at once because our OnPaint() will draw
1237 // the whole row anyhow - and this allows the small optimization in
1238 // OnClick() below to work
1239 rect
.x
= m_calendarWeekWidth
;
1241 rect
.y
= (m_heightRow
* GetWeek(date
)) + m_rowOffset
;
1243 rect
.width
= 7*m_widthCol
;
1244 rect
.height
= m_heightRow
;
1247 // VZ: for some reason, the selected date seems to occupy more space under
1248 // MSW - this is probably some bug in the font size calculations, but I
1249 // don't know where exactly. This fix is ugly and leads to more
1250 // refreshes than really needed, but without it the selected days
1251 // leaves even more ugly underscores on screen.
1256 wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
1259 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1262 Refresh(true, &rect
);
1265 void wxGenericCalendarCtrl::HighlightRange(wxPaintDC
* pDC
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, const wxPen
* pPen
, const wxBrush
* pBrush
)
1267 // Highlights the given range using pen and brush
1268 // Does nothing if todate < fromdate
1272 wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate
.Format("%d %m %Y"), todate
.Format("%d %m %Y"));
1275 if ( todate
>= fromdate
)
1282 // implicit: both dates must be currently shown - checked by GetDateCoord
1283 if ( GetDateCoord(fromdate
, &fd
, &fw
) && GetDateCoord(todate
, &td
, &tw
) )
1286 wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd
, fw
, td
, tw
);
1288 if ( ( (tw
- fw
) == 1 ) && ( td
< fd
) )
1290 // special case: interval 7 days or less not in same week
1291 // split in two separate intervals
1292 wxDateTime tfd
= fromdate
+ wxDateSpan::Days(7-fd
);
1293 wxDateTime ftd
= tfd
+ wxDateSpan::Day();
1295 wxLogDebug("Highlight: Separate segments");
1298 HighlightRange(pDC
, fromdate
, tfd
, pPen
, pBrush
);
1299 HighlightRange(pDC
, ftd
, todate
, pPen
, pBrush
);
1304 wxPoint corners
[8]; // potentially 8 corners in polygon
1305 wxCoord x0
= m_calendarWeekWidth
;
1309 // simple case: same week
1311 corners
[0] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
);
1312 corners
[1] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, ((fw
+ 1 ) * m_heightRow
) + m_rowOffset
);
1313 corners
[2] = wxPoint(x0
+ td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
);
1314 corners
[3] = wxPoint(x0
+ td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
);
1319 // "complex" polygon
1320 corners
[cidx
] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1324 corners
[cidx
] = wxPoint(x0
+ (fd
- 1) * m_widthCol
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1325 corners
[cidx
] = wxPoint(x0
, ((fw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1328 corners
[cidx
] = wxPoint(x0
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1329 corners
[cidx
] = wxPoint(x0
+ td
* m_widthCol
, ((tw
+ 1) * m_heightRow
) + m_rowOffset
); cidx
++;
1333 corners
[cidx
] = wxPoint(x0
+ td
* m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1334 corners
[cidx
] = wxPoint(x0
+ 7 * m_widthCol
, (tw
* m_heightRow
) + m_rowOffset
); cidx
++;
1337 corners
[cidx
] = wxPoint(x0
+ 7 * m_widthCol
, (fw
* m_heightRow
) + m_rowOffset
); cidx
++;
1343 pDC
->SetBrush(*pBrush
);
1345 pDC
->DrawPolygon(numpoints
, corners
);
1351 wxLogDebug("--- HighlightRange ---");
1355 bool wxGenericCalendarCtrl::GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const
1360 wxLogDebug("+++ GetDateCoord: (%s) +++", date
.Format("%d %m %Y"));
1363 if ( IsDateShown(date
) )
1365 bool startOnMonday
= ( GetWindowStyle() & wxCAL_MONDAY_FIRST
) != 0;
1368 *day
= date
.GetWeekDay();
1370 if ( *day
== 0 ) // sunday
1372 *day
= ( startOnMonday
) ? 7 : 1;
1376 *day
+= ( startOnMonday
) ? 0 : 1;
1379 int targetmonth
= date
.GetMonth() + (12 * date
.GetYear());
1380 int thismonth
= m_date
.GetMonth() + (12 * m_date
.GetYear());
1383 if ( targetmonth
== thismonth
)
1385 *week
= GetWeek(date
);
1389 if ( targetmonth
< thismonth
)
1391 *week
= 1; // trivial
1393 else // targetmonth > thismonth
1399 // get the datecoord of the last day in the month currently shown
1401 wxLogDebug(" +++ LDOM +++");
1403 GetDateCoord(ldcm
.SetToLastMonthDay(m_date
.GetMonth(), m_date
.GetYear()), &lastday
, &lastweek
);
1405 wxLogDebug(" --- LDOM ---");
1408 wxTimeSpan span
= date
- ldcm
;
1410 int daysfromlast
= span
.GetDays();
1412 wxLogDebug("daysfromlast: %i", daysfromlast
);
1414 if ( daysfromlast
+ lastday
> 7 ) // past week boundary
1416 int wholeweeks
= (daysfromlast
/ 7);
1417 *week
= wholeweeks
+ lastweek
;
1418 if ( (daysfromlast
- (7 * wholeweeks
) + lastday
) > 7 )
1438 wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date
.Format("%d %m %Y"), *day
, *week
);
1444 // ----------------------------------------------------------------------------
1446 // ----------------------------------------------------------------------------
1448 void wxGenericCalendarCtrl::OnDClick(wxMouseEvent
& event
)
1450 if ( HitTest(event
.GetPosition()) != wxCAL_HITTEST_DAY
)
1456 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1460 void wxGenericCalendarCtrl::OnClick(wxMouseEvent
& event
)
1463 wxDateTime::WeekDay wday
;
1464 switch ( HitTest(event
.GetPosition(), &date
, &wday
) )
1466 case wxCAL_HITTEST_DAY
:
1467 if ( IsDateInRange(date
) )
1471 GenerateEvent(wxEVT_CALENDAR_SEL_CHANGED
);
1473 // we know that the month/year never change when the user
1474 // clicks on the control so there is no need to call
1475 // GenerateAllChangeEvents() here, we know which event to send
1476 GenerateEvent(wxEVT_CALENDAR_DAY_CHANGED
);
1480 case wxCAL_HITTEST_WEEK
:
1482 wxCalendarEvent
send( this, date
, wxEVT_CALENDAR_WEEK_CLICKED
);
1483 HandleWindowEvent( send
);
1487 case wxCAL_HITTEST_HEADER
:
1489 wxCalendarEvent
eventWd(this, GetDate(),
1490 wxEVT_CALENDAR_WEEKDAY_CLICKED
);
1491 eventWd
.SetWeekDay(wday
);
1492 (void)GetEventHandler()->ProcessEvent(eventWd
);
1496 case wxCAL_HITTEST_DECMONTH
:
1497 case wxCAL_HITTEST_INCMONTH
:
1498 case wxCAL_HITTEST_SURROUNDING_WEEK
:
1499 SetDateAndNotify(date
); // we probably only want to refresh the control. No notification.. (maybe as an option?)
1503 wxFAIL_MSG(_T("unknown hittest code"));
1506 case wxCAL_HITTEST_NOWHERE
:
1511 // as we don't (always) skip the message, we're not going to receive the
1512 // focus on click by default if we don't do it ourselves
1516 wxCalendarHitTestResult
wxGenericCalendarCtrl::HitTest(const wxPoint
& pos
,
1518 wxDateTime::WeekDay
*wd
)
1522 // the position where the calendar really begins
1523 wxCoord x0
= m_calendarWeekWidth
;
1525 if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION
) )
1529 // we need to find out if the hit is on left arrow, on month or on right arrow
1531 if ( m_leftArrowRect
.Contains(pos
) )
1535 if ( IsDateInRange(m_date
- wxDateSpan::Month()) )
1537 *date
= m_date
- wxDateSpan::Month();
1541 *date
= GetLowerDateLimit();
1545 return wxCAL_HITTEST_DECMONTH
;
1548 if ( m_rightArrowRect
.Contains(pos
) )
1552 if ( IsDateInRange(m_date
+ wxDateSpan::Month()) )
1554 *date
= m_date
+ wxDateSpan::Month();
1558 *date
= GetUpperDateLimit();
1562 return wxCAL_HITTEST_INCMONTH
;
1566 if ( pos
.x
- x0
< 0 )
1568 if ( pos
.x
>= 0 && pos
.y
> m_rowOffset
+ m_heightRow
&& pos
.y
<= m_rowOffset
+ m_heightRow
* 7 )
1572 *date
= GetStartDate();
1573 *date
+= wxDateSpan::Week() * (( pos
.y
- m_rowOffset
) / m_heightRow
- 1 );
1576 *wd
= ( GetWindowStyle() & wxCAL_MONDAY_FIRST
) ? wxDateTime::Mon
: wxDateTime::Sun
;
1577 return wxCAL_HITTEST_WEEK
;
1579 else // early exit -> the rest of the function checks for clicks on days
1580 return wxCAL_HITTEST_NOWHERE
;
1583 // header: week days
1584 int wday
= (pos
.x
- x0
) / m_widthCol
;
1586 return wxCAL_HITTEST_NOWHERE
;
1587 if ( pos
.y
< (m_heightRow
+ m_rowOffset
))
1589 if ( pos
.y
> m_rowOffset
)
1593 if ( GetWindowStyle() & wxCAL_MONDAY_FIRST
)
1595 wday
= wday
== 6 ? 0 : wday
+ 1;
1598 *wd
= (wxDateTime::WeekDay
)wday
;
1601 return wxCAL_HITTEST_HEADER
;
1605 return wxCAL_HITTEST_NOWHERE
;
1609 int week
= (pos
.y
- (m_heightRow
+ m_rowOffset
)) / m_heightRow
;
1610 if ( week
>= 6 || wday
>= 7 )
1612 return wxCAL_HITTEST_NOWHERE
;
1615 wxDateTime dt
= GetStartDate() + wxDateSpan::Days(7*week
+ wday
);
1617 if ( IsDateShown(dt
) )
1622 if ( dt
.GetMonth() == m_date
.GetMonth() )
1625 return wxCAL_HITTEST_DAY
;
1629 return wxCAL_HITTEST_SURROUNDING_WEEK
;
1634 return wxCAL_HITTEST_NOWHERE
;
1638 // ----------------------------------------------------------------------------
1639 // subcontrols events handling
1640 // ----------------------------------------------------------------------------
1642 void wxGenericCalendarCtrl::OnMonthChange(wxCommandEvent
& event
)
1644 wxDateTime::Tm tm
= m_date
.GetTm();
1646 wxDateTime::Month mon
= (wxDateTime::Month
)event
.GetInt();
1647 if ( tm
.mday
> wxDateTime::GetNumberOfDays(mon
, tm
.year
) )
1649 tm
.mday
= wxDateTime::GetNumberOfDays(mon
, tm
.year
);
1652 wxDateTime target
= wxDateTime(tm
.mday
, mon
, tm
.year
);
1654 ChangeMonth(&target
);
1655 SetDateAndNotify(target
);
1658 void wxGenericCalendarCtrl::HandleYearChange(wxCommandEvent
& event
)
1660 int year
= (int)event
.GetInt();
1661 if ( year
== INT_MIN
)
1663 // invalid year in the spin control, ignore it
1667 wxDateTime::Tm tm
= m_date
.GetTm();
1669 if ( tm
.mday
> wxDateTime::GetNumberOfDays(tm
.mon
, year
) )
1671 tm
.mday
= wxDateTime::GetNumberOfDays(tm
.mon
, year
);
1674 wxDateTime target
= wxDateTime(tm
.mday
, tm
.mon
, year
);
1676 if ( ChangeYear(&target
) )
1678 SetDateAndNotify(target
);
1682 // In this case we don't want to change the date. That would put us
1683 // inside the same year but a strange number of months forward/back..
1684 m_spinYear
->SetValue(target
.GetYear());
1688 void wxGenericCalendarCtrl::OnYearChange(wxSpinEvent
& event
)
1690 HandleYearChange( event
);
1693 void wxGenericCalendarCtrl::OnYearTextChange(wxCommandEvent
& event
)
1695 SetUserChangedYear();
1696 HandleYearChange(event
);
1699 // Responds to colour changes, and passes event on to children.
1700 void wxGenericCalendarCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1705 // Propagate the event to the children
1706 wxControl::OnSysColourChanged(event
);
1708 // Redraw control area
1709 SetBackgroundColour(m_colBackground
);
1713 // ----------------------------------------------------------------------------
1714 // keyboard interface
1715 // ----------------------------------------------------------------------------
1717 void wxGenericCalendarCtrl::OnChar(wxKeyEvent
& event
)
1720 switch ( event
.GetKeyCode() )
1724 target
= m_date
+ wxDateSpan::Year();
1725 if ( ChangeYear(&target
) )
1727 SetDateAndNotify(target
);
1733 target
= m_date
- wxDateSpan::Year();
1734 if ( ChangeYear(&target
) )
1736 SetDateAndNotify(target
);
1741 target
= m_date
- wxDateSpan::Month();
1742 ChangeMonth(&target
);
1743 SetDateAndNotify(target
); // always
1747 target
= m_date
+ wxDateSpan::Month();
1748 ChangeMonth(&target
);
1749 SetDateAndNotify(target
); // always
1753 if ( event
.ControlDown() )
1755 target
= wxDateTime(m_date
).SetToNextWeekDay(
1756 GetWindowStyle() & wxCAL_MONDAY_FIRST
1757 ? wxDateTime::Sun
: wxDateTime::Sat
);
1758 if ( !IsDateInRange(target
) )
1760 target
= GetUpperDateLimit();
1762 SetDateAndNotify(target
);
1765 SetDateAndNotify(m_date
+ wxDateSpan::Day());
1769 if ( event
.ControlDown() )
1771 target
= wxDateTime(m_date
).SetToPrevWeekDay(
1772 GetWindowStyle() & wxCAL_MONDAY_FIRST
1773 ? wxDateTime::Mon
: wxDateTime::Sun
);
1774 if ( !IsDateInRange(target
) )
1776 target
= GetLowerDateLimit();
1778 SetDateAndNotify(target
);
1781 SetDateAndNotify(m_date
- wxDateSpan::Day());
1785 SetDateAndNotify(m_date
- wxDateSpan::Week());
1789 SetDateAndNotify(m_date
+ wxDateSpan::Week());
1793 if ( event
.ControlDown() )
1794 SetDateAndNotify(wxDateTime::Today());
1796 SetDateAndNotify(wxDateTime(1, m_date
.GetMonth(), m_date
.GetYear()));
1800 SetDateAndNotify(wxDateTime(m_date
).SetToLastMonthDay());
1804 GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
);
1812 // ----------------------------------------------------------------------------
1813 // holidays handling
1814 // ----------------------------------------------------------------------------
1816 void wxGenericCalendarCtrl::SetHoliday(size_t day
)
1818 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in SetHoliday") );
1820 wxCalendarDateAttr
*attr
= GetAttr(day
);
1823 attr
= new wxCalendarDateAttr
;
1826 attr
->SetHoliday(true);
1828 // can't use SetAttr() because it would delete this pointer
1829 m_attrs
[day
- 1] = attr
;
1832 void wxGenericCalendarCtrl::ResetHolidayAttrs()
1834 for ( size_t day
= 0; day
< 31; day
++ )
1838 m_attrs
[day
]->SetHoliday(false);
1843 void wxGenericCalendarCtrl::Mark(size_t day
, bool mark
)
1845 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day in Mark") );
1847 const wxCalendarDateAttr
& m
= wxCalendarDateAttr::GetMark();
1849 if ( m_attrs
[day
- 1] )
1850 AddAttr(m_attrs
[day
- 1], m
);
1852 SetAttr(day
, new wxCalendarDateAttr(m
));
1855 if ( m_attrs
[day
- 1] )
1856 DelAttr(m_attrs
[day
- 1], m
);
1862 wxGenericCalendarCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
1864 // Use the same color scheme as wxListBox
1865 return wxListBox::GetClassDefaultAttributes(variant
);
1868 #endif // wxUSE_CALENDARCTRL