From 705006615f0a8f1389f54add9e3028ad96847355 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 3 Nov 2020 02:57:22 +0100 Subject: [PATCH] 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. --- include/wx/generic/grideditors.h | 5 ++++- interface/wx/grid.h | 8 +++++++- src/generic/grideditors.cpp | 17 +++++++++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/include/wx/generic/grideditors.h b/include/wx/generic/grideditors.h index 4510d31944..025c9faff7 100644 --- a/include/wx/generic/grideditors.h +++ b/include/wx/generic/grideditors.h @@ -390,7 +390,9 @@ public: class WXDLLIMPEXP_ADV wxGridCellDateEditor : public wxGridCellEditor { public: - wxGridCellDateEditor() { } + explicit wxGridCellDateEditor(const wxString& format = wxString()); + + virtual void SetParameters(const wxString& params) wxOVERRIDE; virtual void Create(wxWindow* parent, wxWindowID id, @@ -414,6 +416,7 @@ protected: private: wxDateTime m_value; + wxString m_format; wxDECLARE_NO_COPY_CLASS(wxGridCellDateEditor); }; diff --git a/interface/wx/grid.h b/interface/wx/grid.h index 9e33e41e4d..4ef30b65ce 100644 --- a/interface/wx/grid.h +++ b/interface/wx/grid.h @@ -1092,8 +1092,14 @@ class wxGridCellDateEditor : public wxGridCellEditor public: /** 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()); }; diff --git a/src/generic/grideditors.cpp b/src/generic/grideditors.cpp index 0c7df337e4..3183f628fb 100644 --- a/src/generic/grideditors.cpp +++ b/src/generic/grideditors.cpp @@ -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) { @@ -1908,7 +1921,7 @@ 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) ) + if ( !wxGridPrivate::TryParseDate(m_value, dateStr, m_format) ) { // Invalidate m_value, so that it always compares different // to any value returned from DatePicker()->GetValue(). @@ -1960,7 +1973,7 @@ void wxGridCellDateEditor::Reset() wxGridCellEditor *wxGridCellDateEditor::Clone() const { - return new wxGridCellDateEditor(); + return new wxGridCellDateEditor(m_format); } wxString wxGridCellDateEditor::GetValue() const