When the date was constrained to a range in wxGenericCalendarCtrl, the display
of the month in the month combobox could get out of sync with its real value.
Ensure that the correct month is always displayed and also simplify the code
by removing the apparently unnecessarily complex logic in ChangeYear() and
ChangeMonth() functions.
Closes #11060.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65901
c3d73ce0-8a6f-49c7-b76d-
6d57e0e08775
// is this date shown?
bool IsDateShown(const wxDateTime& date) const;
// is this date shown?
bool IsDateShown(const wxDateTime& date) const;
- // is this date in the given range?
+ // is this date in the currently allowed range?
bool IsDateInRange(const wxDateTime& date) const;
bool IsDateInRange(const wxDateTime& date) const;
- // range helpers
- bool ChangeYear(wxDateTime* target) const;
- bool ChangeMonth(wxDateTime* target) const;
+ // adjust the date to the currently allowed range, return true if it was
+ // changed
+ bool AdjustDateToRange(wxDateTime *date) const;
// redraw the given date
void RefreshDate(const wxDateTime& date);
// redraw the given date
void RefreshDate(const wxDateTime& date);
&& ( ( m_highdate.IsValid() ) ? ( date <= m_highdate ) : true ) );
}
&& ( ( m_highdate.IsValid() ) ? ( date <= m_highdate ) : true ) );
}
-bool wxGenericCalendarCtrl::ChangeYear(wxDateTime* target) const
+bool wxGenericCalendarCtrl::AdjustDateToRange(wxDateTime *date) const
- bool retval = false;
-
- if ( !(IsDateInRange(*target)) )
- {
- if ( target->GetYear() < m_date.GetYear() )
- {
- if ( target->GetYear() >= GetLowerDateLimit().GetYear() )
- {
- *target = GetLowerDateLimit();
- retval = true;
- }
- else
- {
- *target = m_date;
- }
- }
- else
- {
- if ( target->GetYear() <= GetUpperDateLimit().GetYear() )
- {
- *target = GetUpperDateLimit();
- retval = true;
- }
- else
- {
- *target = m_date;
- }
- }
- }
- else
+ if ( m_lowdate.IsValid() && *date < m_lowdate )
+ *date = m_lowdate;
+ return true;
- return retval;
-}
-
-bool wxGenericCalendarCtrl::ChangeMonth(wxDateTime* target) const
-{
- bool retval = true;
-
- if ( !(IsDateInRange(*target)) )
+ if ( m_highdate.IsValid() && *date > m_highdate )
- retval = false;
-
- if ( target->GetMonth() < m_date.GetMonth() )
- {
- *target = GetLowerDateLimit();
- }
- else
- {
- *target = GetUpperDateLimit();
- }
+ *date = m_highdate;
+ return true;
}
size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime& date) const
}
size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime& date) const
tm.mday = wxDateTime::GetNumberOfDays(mon, tm.year);
}
tm.mday = wxDateTime::GetNumberOfDays(mon, tm.year);
}
- wxDateTime target = wxDateTime(tm.mday, mon, tm.year);
+ wxDateTime dt(tm.mday, mon, tm.year);
+ if ( AdjustDateToRange(&dt) )
+ {
+ // The date must have been changed to ensure it's in valid range,
+ // reflect this in the month choice control.
+ m_comboMonth->SetSelection(dt.GetMonth());
+ }
- ChangeMonth(&target);
- SetDateAndNotify(target);
}
void wxGenericCalendarCtrl::HandleYearChange(wxCommandEvent& event)
}
void wxGenericCalendarCtrl::HandleYearChange(wxCommandEvent& event)
tm.mday = wxDateTime::GetNumberOfDays(tm.mon, year);
}
tm.mday = wxDateTime::GetNumberOfDays(tm.mon, year);
}
- wxDateTime target = wxDateTime(tm.mday, tm.mon, year);
-
- if ( ChangeYear(&target) )
- {
- SetDateAndNotify(target);
- }
- else
+ wxDateTime dt(tm.mday, tm.mon, year);
+ if ( AdjustDateToRange(&dt) )
- // In this case we don't want to change the date. That would put us
- // inside the same year but a strange number of months forward/back..
- m_spinYear->SetValue(target.GetYear());
+ // As above, if the date was changed to keep it in valid range, its
+ // possibly changed year must be shown in the GUI.
+ m_spinYear->SetValue(dt.GetYear());
+
+ SetDateAndNotify(dt);
}
void wxGenericCalendarCtrl::OnYearChange(wxSpinEvent& event)
}
void wxGenericCalendarCtrl::OnYearChange(wxSpinEvent& event)
void wxGenericCalendarCtrl::OnChar(wxKeyEvent& event)
{
void wxGenericCalendarCtrl::OnChar(wxKeyEvent& event)
{
switch ( event.GetKeyCode() )
{
case wxT('+'):
case WXK_ADD:
switch ( event.GetKeyCode() )
{
case wxT('+'):
case WXK_ADD:
- target = m_date + wxDateSpan::Year();
- if ( ChangeYear(&target) )
- {
- SetDateAndNotify(target);
- }
+ SetDateAndNotify(m_date + wxDateSpan::Year());
break;
case wxT('-'):
case WXK_SUBTRACT:
break;
case wxT('-'):
case WXK_SUBTRACT:
- target = m_date - wxDateSpan::Year();
- if ( ChangeYear(&target) )
- {
- SetDateAndNotify(target);
- }
+ SetDateAndNotify(m_date - wxDateSpan::Year());
- target = m_date - wxDateSpan::Month();
- ChangeMonth(&target);
- SetDateAndNotify(target); // always
+ SetDateAndNotify(m_date - wxDateSpan::Month());
break;
case WXK_PAGEDOWN:
break;
case WXK_PAGEDOWN:
- target = m_date + wxDateSpan::Month();
- ChangeMonth(&target);
- SetDateAndNotify(target); // always
+ SetDateAndNotify(m_date + wxDateSpan::Month());
break;
case WXK_RIGHT:
if ( event.ControlDown() )
{
break;
case WXK_RIGHT:
if ( event.ControlDown() )
{
- target = wxDateTime(m_date).SetToNextWeekDay(
+ wxDateTime
+ target = m_date.SetToNextWeekDay(
GetWindowStyle() & wxCAL_MONDAY_FIRST
? wxDateTime::Sun : wxDateTime::Sat);
if ( !IsDateInRange(target) )
GetWindowStyle() & wxCAL_MONDAY_FIRST
? wxDateTime::Sun : wxDateTime::Sat);
if ( !IsDateInRange(target) )
case WXK_LEFT:
if ( event.ControlDown() )
{
case WXK_LEFT:
if ( event.ControlDown() )
{
- target = wxDateTime(m_date).SetToPrevWeekDay(
+ wxDateTime
+ target = m_date.SetToPrevWeekDay(
GetWindowStyle() & wxCAL_MONDAY_FIRST
? wxDateTime::Mon : wxDateTime::Sun);
if ( !IsDateInRange(target) )
GetWindowStyle() & wxCAL_MONDAY_FIRST
? wxDateTime::Mon : wxDateTime::Sun);
if ( !IsDateInRange(target) )