Add support for date format to wxGridCellDateEditor too

This is needed to allow editing the cells using wxGridCellDateRenderer
with a custom format, otherwise the editor might parse the contents of
the cell differently from what is actually shown.

In particular, this ensures that using "date:%d.%m.%y" (or any other
custom format) as "cell type" works correctly, which wasn't the case
before.
This commit is contained in:
Vadim Zeitlin
2020-11-03 02:57:22 +01:00
parent 56ec476a3a
commit 705006615f
3 changed files with 26 additions and 4 deletions

View File

@@ -390,7 +390,9 @@ public:
class WXDLLIMPEXP_ADV wxGridCellDateEditor : public wxGridCellEditor class WXDLLIMPEXP_ADV wxGridCellDateEditor : public wxGridCellEditor
{ {
public: public:
wxGridCellDateEditor() { } explicit wxGridCellDateEditor(const wxString& format = wxString());
virtual void SetParameters(const wxString& params) wxOVERRIDE;
virtual void Create(wxWindow* parent, virtual void Create(wxWindow* parent,
wxWindowID id, wxWindowID id,
@@ -414,6 +416,7 @@ protected:
private: private:
wxDateTime m_value; wxDateTime m_value;
wxString m_format;
wxDECLARE_NO_COPY_CLASS(wxGridCellDateEditor); wxDECLARE_NO_COPY_CLASS(wxGridCellDateEditor);
}; };

View File

@@ -1092,8 +1092,14 @@ class wxGridCellDateEditor : public wxGridCellEditor
public: public:
/** /**
Date editor constructor. Date editor constructor.
@param format Optional format for the date displayed in the associated
cell. By default, the locale-specific date format ("%x") is assumed.
You would typically want to specify the same format as the one
used with the cell renderer, if a non-default one is used.
Note that this parameter is only available since wxWidgets 3.1.5.
*/ */
wxGridCellDateEditor(); explicit wxGridCellDateEditor(const wxString& format = wxString());
}; };

View File

@@ -1861,6 +1861,19 @@ struct wxGridCellDateEditorKeyHandler
}; };
#endif // __WXGTK__ #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, void wxGridCellDateEditor::Create(wxWindow* parent, wxWindowID id,
wxEvtHandler* evtHandler) wxEvtHandler* evtHandler)
{ {
@@ -1908,7 +1921,7 @@ void wxGridCellDateEditor::BeginEdit(int row, int col, wxGrid* grid)
wxASSERT_MSG(m_control, "The wxGridCellDateEditor must be created first!"); wxASSERT_MSG(m_control, "The wxGridCellDateEditor must be created first!");
const wxString dateStr = grid->GetTable()->GetValue(row, col); const wxString dateStr = grid->GetTable()->GetValue(row, col);
if ( !m_value.ParseDate(dateStr) ) if ( !wxGridPrivate::TryParseDate(m_value, dateStr, m_format) )
{ {
// Invalidate m_value, so that it always compares different // Invalidate m_value, so that it always compares different
// to any value returned from DatePicker()->GetValue(). // to any value returned from DatePicker()->GetValue().
@@ -1960,7 +1973,7 @@ void wxGridCellDateEditor::Reset()
wxGridCellEditor *wxGridCellDateEditor::Clone() const wxGridCellEditor *wxGridCellDateEditor::Clone() const
{ {
return new wxGridCellDateEditor(); return new wxGridCellDateEditor(m_format);
} }
wxString wxGridCellDateEditor::GetValue() const wxString wxGridCellDateEditor::GetValue() const