From 56ec476a3a54e59ae1e0407dc77e1279481254b2 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 3 Nov 2020 02:50:31 +0100 Subject: [PATCH] Refactor wxGridCellDateRenderer::Parse() to make it reusable No real changes, just create TryParseDate() helper in order to allow reusing it from wxGridCellDateEditor too in the upcoming commit. --- include/wx/generic/private/grid.h | 15 +++++++++++++++ src/generic/gridctrl.cpp | 28 ++++++++++++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/include/wx/generic/private/grid.h b/include/wx/generic/private/grid.h index 1330bb1044..19df9d4c76 100644 --- a/include/wx/generic/private/grid.h +++ b/include/wx/generic/private/grid.h @@ -1084,5 +1084,20 @@ wxGetContentRect(wxSize contentSize, int hAlign, int vAlign); +namespace wxGridPrivate +{ + +#if wxUSE_DATETIME + +// Helper function trying to parse the given string using the specified date +// format and then using ParseDate() as a fallback if it failed. If this still +// fails, returns false. +bool +TryParseDate(wxDateTime& result, const wxString& text, const wxString& format); + +#endif // wxUSE_DATETIME + +} // namespace wxGridPrivate + #endif // wxUSE_GRID #endif // _WX_GENERIC_GRID_PRIVATE_H_ diff --git a/src/generic/gridctrl.cpp b/src/generic/gridctrl.cpp index 38744e2ff3..c4b37fb421 100644 --- a/src/generic/gridctrl.cpp +++ b/src/generic/gridctrl.cpp @@ -76,6 +76,23 @@ void wxGridCellRenderer::Draw(wxGrid& grid, #if wxUSE_DATETIME +bool +wxGridPrivate::TryParseDate(wxDateTime& result, + const wxString& text, + const wxString& format) +{ + wxString::const_iterator end; + + // Try parsing using the same format we use for output first. + if ( result.ParseFormat(text, format, &end) && end == text.end() ) + return true; + + // But fall back to free-form parsing, which notably allows us to parse + // strings such as "today" or "tomorrow" which would be never accepted by + // ParseFormat(). + return result.ParseDate(text, &end) && end == text.end(); +} + // Enables a grid cell to display a formatted date wxGridCellDateRenderer::wxGridCellDateRenderer(const wxString& outformat) @@ -131,16 +148,7 @@ wxString wxGridCellDateRenderer::GetString(const wxGrid& grid, int row, int col) bool wxGridCellDateRenderer::Parse(const wxString& text, wxDateTime& result) { - wxString::const_iterator end; - - // Try parsing using the same format we use for output first. - if ( result.ParseFormat(text, m_oformat, &end) && end == text.end() ) - return true; - - // But fall back to free-form parsing, which notably allows us to parse - // strings such as "today" or "tomorrow" which would be never accepted by - // ParseFormat(). - return result.ParseDate(text, &end) && end == text.end(); + return wxGridPrivate::TryParseDate(result, text, m_oformat); } void wxGridCellDateRenderer::Draw(wxGrid& grid,