From: Vadim Zeitlin Date: Sun, 24 Oct 2010 22:40:47 +0000 (+0000) Subject: No real changes, just cleanup week days handling in wxGenericCalendarCtrl. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/0bb84570cea789202aa26409a4879202a9810b70 No real changes, just cleanup week days handling in wxGenericCalendarCtrl. Use helper GetWeek{Start,End}() functions instead of repeating tests for wxCAL_MONDAY_FIRST over and over again. Also replace some occurrences of GetWindowStyle() with shorter and more clear HasFlag(). git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65902 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/generic/calctrlg.h b/include/wx/generic/calctrlg.h index 7e75370ed4..02abe01210 100644 --- a/include/wx/generic/calctrlg.h +++ b/include/wx/generic/calctrlg.h @@ -199,6 +199,20 @@ private: // get the date from which we start drawing days wxDateTime GetStartDate() const; + // get the first/last days of the week corresponding to the current style + wxDateTime::WeekDay GetWeekStart() const + { + return HasFlag(wxCAL_MONDAY_FIRST) ? wxDateTime::Mon + : wxDateTime::Sun; + } + + wxDateTime::WeekDay GetWeekEnd() const + { + return HasFlag(wxCAL_MONDAY_FIRST) ? wxDateTime::Sun + : wxDateTime::Sat; + } + + // is this date shown? bool IsDateShown(const wxDateTime& date) const; diff --git a/src/generic/calctrlg.cpp b/src/generic/calctrlg.cpp index 25ab8cdad0..367de68c36 100644 --- a/src/generic/calctrlg.cpp +++ b/src/generic/calctrlg.cpp @@ -648,8 +648,7 @@ wxDateTime wxGenericCalendarCtrl::GetStartDate() const wxDateTime date = wxDateTime(1, tm.mon, tm.year); // rewind back - date.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST - ? wxDateTime::Mon : wxDateTime::Sun); + date.SetToPrevWeekDay(GetWeekStart()); if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS ) { @@ -701,7 +700,7 @@ bool wxGenericCalendarCtrl::AdjustDateToRange(wxDateTime *date) const size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime& date) const { - size_t retval = date.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST + size_t retval = date.GetWeekOfMonth(HasFlag(wxCAL_MONDAY_FIRST) ? wxDateTime::Monday_First : wxDateTime::Sunday_First); @@ -713,8 +712,7 @@ size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime& date) const wxDateTime datetest = wxDateTime(1, tm.mon, tm.year); // rewind back - datetest.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST - ? wxDateTime::Mon : wxDateTime::Sun); + datetest.SetToPrevWeekDay(GetWeekStart()); if ( datetest.GetDay() == 1 ) { @@ -972,7 +970,7 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) dc.SetPen(wxPen(m_colHeaderBg, 1, wxPENSTYLE_SOLID)); dc.DrawRectangle(0, y, GetClientSize().x, m_heightRow); - bool startOnMonday = (GetWindowStyle() & wxCAL_MONDAY_FIRST) != 0; + bool startOnMonday = HasFlag(wxCAL_MONDAY_FIRST); for ( int wd = 0; wd < 7; wd++ ) { size_t n; @@ -1319,7 +1317,7 @@ bool wxGenericCalendarCtrl::GetDateCoord(const wxDateTime& date, int *day, int * if ( IsDateShown(date) ) { - bool startOnMonday = ( GetWindowStyle() & wxCAL_MONDAY_FIRST ) != 0; + bool startOnMonday = HasFlag(wxCAL_MONDAY_FIRST); // Find day *day = date.GetWeekDay(); @@ -1530,7 +1528,7 @@ wxCalendarHitTestResult wxGenericCalendarCtrl::HitTest(const wxPoint& pos, *date += wxDateSpan::Week() * (( pos.y - m_rowOffset ) / m_heightRow - 1 ); } if ( wd ) - *wd = ( GetWindowStyle() & wxCAL_MONDAY_FIRST ) ? wxDateTime::Mon : wxDateTime::Sun; + *wd = GetWeekStart(); return wxCAL_HITTEST_WEEK; } else // early exit -> the rest of the function checks for clicks on days @@ -1547,7 +1545,7 @@ wxCalendarHitTestResult wxGenericCalendarCtrl::HitTest(const wxPoint& pos, { if ( wd ) { - if ( GetWindowStyle() & wxCAL_MONDAY_FIRST ) + if ( HasFlag(wxCAL_MONDAY_FIRST) ) { wday = wday == 6 ? 0 : wday + 1; } @@ -1698,14 +1696,8 @@ void wxGenericCalendarCtrl::OnChar(wxKeyEvent& event) case WXK_RIGHT: if ( event.ControlDown() ) { - wxDateTime - target = m_date.SetToNextWeekDay( - GetWindowStyle() & wxCAL_MONDAY_FIRST - ? wxDateTime::Sun : wxDateTime::Sat); - if ( !IsDateInRange(target) ) - { - target = GetUpperDateLimit(); - } + wxDateTime target = m_date.SetToNextWeekDay(GetWeekEnd()); + AdjustDateToRange(&target); SetDateAndNotify(target); } else @@ -1715,14 +1707,8 @@ void wxGenericCalendarCtrl::OnChar(wxKeyEvent& event) case WXK_LEFT: if ( event.ControlDown() ) { - wxDateTime - target = m_date.SetToPrevWeekDay( - GetWindowStyle() & wxCAL_MONDAY_FIRST - ? wxDateTime::Mon : wxDateTime::Sun); - if ( !IsDateInRange(target) ) - { - target = GetLowerDateLimit(); - } + wxDateTime target = m_date.SetToPrevWeekDay(GetWeekStart()); + AdjustDateToRange(&target); SetDateAndNotify(target); } else