Merge branch 'grid-date-format'

Fixes for using custom wxGrid date format.

See https://github.com/wxWidgets/wxWidgets/pull/2108

Closes #18876.
This commit is contained in:
Vadim Zeitlin
2020-11-05 16:12:28 +01:00
6 changed files with 145 additions and 49 deletions

View File

@@ -76,6 +76,47 @@ void wxGridCellRenderer::Draw(wxGrid& grid,
#if wxUSE_DATETIME
bool
wxGridPrivate::TryGetValueAsDate(wxDateTime& result,
const DateParseParams& params,
const wxGrid& grid,
int row, int col)
{
wxGridTableBase *table = grid.GetTable();
if ( table->CanGetValueAs(row, col, wxGRID_VALUE_DATETIME) )
{
void * tempval = table->GetValueAsCustom(row, col,wxGRID_VALUE_DATETIME);
if (tempval)
{
result = *((wxDateTime *)tempval);
delete (wxDateTime *)tempval;
return true;
}
}
const wxString text = table->GetValue(row, col);
wxString::const_iterator end;
if ( result.ParseFormat(text, params.format, &end) && end == text.end() )
return true;
// Check if we can 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().
if ( params.fallbackParseDate &&
result.ParseDate(text, &end) && end == text.end() )
return true;
return false;
}
using namespace wxGridPrivate;
// Enables a grid cell to display a formatted date
wxGridCellDateRenderer::wxGridCellDateRenderer(const wxString& outformat)
@@ -98,41 +139,23 @@ wxGridCellRenderer *wxGridCellDateRenderer::Clone() const
wxString wxGridCellDateRenderer::GetString(const wxGrid& grid, int row, int col)
{
wxGridTableBase *table = grid.GetTable();
bool hasDatetime = false;
wxDateTime val;
wxString text;
if ( table->CanGetValueAs(row, col, wxGRID_VALUE_DATETIME) )
{
void * tempval = table->GetValueAsCustom(row, col,wxGRID_VALUE_DATETIME);
if (tempval)
{
val = *((wxDateTime *)tempval);
hasDatetime = true;
delete (wxDateTime *)tempval;
}
DateParseParams params;
GetDateParseParams(params);
}
if (!hasDatetime )
{
text = table->GetValue(row, col);
hasDatetime = Parse(text, val);
}
if ( hasDatetime )
wxDateTime val;
if ( TryGetValueAsDate(val, params, grid, row, col) )
text = val.Format(m_oformat, m_tz );
// If we failed to parse string just show what we where given?
return text;
}
bool wxGridCellDateRenderer::Parse(const wxString& text, wxDateTime& result)
void
wxGridCellDateRenderer::GetDateParseParams(DateParseParams& params) const
{
wxString::const_iterator end;
return result.ParseDate(text, &end) && end == text.end();
params = DateParseParams::WithFallback(m_oformat);
}
void wxGridCellDateRenderer::Draw(wxGrid& grid,
@@ -192,7 +215,6 @@ void wxGridCellDateRenderer::SetParameters(const wxString& params)
wxGridCellDateTimeRenderer::wxGridCellDateTimeRenderer(const wxString& outformat, const wxString& informat)
: wxGridCellDateRenderer(outformat)
, m_iformat(informat)
, m_dateDef(wxDefaultDateTime)
{
}
@@ -201,10 +223,10 @@ wxGridCellRenderer *wxGridCellDateTimeRenderer::Clone() const
return new wxGridCellDateTimeRenderer(*this);
}
bool wxGridCellDateTimeRenderer::Parse(const wxString& text, wxDateTime& result)
void
wxGridCellDateTimeRenderer::GetDateParseParams(DateParseParams& params) const
{
const char * const end = result.ParseFormat(text, m_iformat, m_dateDef);
return end && !*end;
params = DateParseParams::WithoutFallback(m_iformat);
}
#endif // wxUSE_DATETIME

View File

@@ -1861,6 +1861,19 @@ struct wxGridCellDateEditorKeyHandler
};
#endif // __WXGTK__
wxGridCellDateEditor::wxGridCellDateEditor(const wxString& format)
{
SetParameters(format);
}
void wxGridCellDateEditor::SetParameters(const wxString& params)
{
if ( params.empty() )
m_format = "%x";
else
m_format = params;
}
void wxGridCellDateEditor::Create(wxWindow* parent, wxWindowID id,
wxEvtHandler* evtHandler)
{
@@ -1907,8 +1920,10 @@ void wxGridCellDateEditor::BeginEdit(int row, int col, wxGrid* grid)
{
wxASSERT_MSG(m_control, "The wxGridCellDateEditor must be created first!");
const wxString dateStr = grid->GetTable()->GetValue(row, col);
if ( !m_value.ParseDate(dateStr) )
using namespace wxGridPrivate;
if ( !TryGetValueAsDate(m_value, DateParseParams::WithFallback(m_format),
*grid, row, col) )
{
// Invalidate m_value, so that it always compares different
// to any value returned from DatePicker()->GetValue().
@@ -1960,7 +1975,7 @@ void wxGridCellDateEditor::Reset()
wxGridCellEditor *wxGridCellDateEditor::Clone() const
{
return new wxGridCellDateEditor();
return new wxGridCellDateEditor(m_format);
}
wxString wxGridCellDateEditor::GetValue() const