Get dates directly from table in wxGridCellDateEditor if possible

Unlike wxGridCellDateRenderer, which already did it, the editor class
always got the cell value from the table as a string, even if the table
supported returning the dates directly.

Fix this by using the same code in the editor as in the renderer, which
required a further refactoring in order to make it reusable: the helper
TryParseDate() was replaced with TryGetValueAsDate() and DateParseParams
was added to allow overriding the arguments passed to it in the
overridden wxGridCellDateTimeRenderer::GetDateParseParams().
This commit is contained in:
Vadim Zeitlin
2020-11-03 17:30:54 +01:00
parent 9311dc8ffb
commit ce0f10c377
4 changed files with 100 additions and 44 deletions

View File

@@ -1089,11 +1089,51 @@ 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.
// This is used as TryGetValueAsDate() parameter.
class DateParseParams
{
public:
// Unfortunately we have to provide the default ctor (and also make the
// members non-const) because we use these objects as out-parameters as
// they are not fully declared in the public headers. The factory functions
// below must be used to create a really usable object.
DateParseParams() : fallbackParseDate(false) { }
// Use these functions to really initialize the object.
static DateParseParams WithFallback(const wxString& format)
{
return DateParseParams(format, true);
}
static DateParseParams WithoutFallback(const wxString& format)
{
return DateParseParams(format, false);
}
// The usual format, e.g. "%x" or "%Y-%m-%d".
wxString format;
// Whether fall back to ParseDate() is allowed.
bool fallbackParseDate;
private:
DateParseParams(const wxString& format_, bool fallbackParseDate_)
: format(format_),
fallbackParseDate(fallbackParseDate_)
{
}
};
// Helper function trying to get a date from the given cell: if possible, get
// the date value from the table directly, otherwise get the string value for
// this cell and try to parse it using the specified date format and, if this
// doesn't work and fallbackParseDate is true, try using ParseDate() as a
// fallback. If this still fails, returns false.
bool
TryParseDate(wxDateTime& result, const wxString& text, const wxString& format);
TryGetValueAsDate(wxDateTime& result,
const DateParseParams& params,
const wxGrid& grid,
int row, int col);
#endif // wxUSE_DATETIME