Add support for editing dates (without time) to wxGrid

Add wxGridCellDateRenderer and wxGridCellDateRenderer which can be used
for the grid cells containing only dates, without times.

Also add wxGrid::SetColFormatDate() convenience function.

Refactor wxGridCellDateTimeRenderer slightly to reuse its code.

Closes https://github.com/wxWidgets/wxWidgets/pull/1101
This commit is contained in:
Pavel Kalugin
2018-09-06 05:49:58 +03:00
committed by Vadim Zeitlin
parent ee352d79c8
commit 659ab78c6d
10 changed files with 416 additions and 46 deletions

View File

@@ -48,6 +48,7 @@ extern WXDLLIMPEXP_DATA_CORE(const char) wxGridNameStr[];
#define wxGRID_VALUE_NUMBER wxT("long")
#define wxGRID_VALUE_FLOAT wxT("double")
#define wxGRID_VALUE_CHOICE wxT("choice")
#define wxGRID_VALUE_DATE wxT("date")
#define wxGRID_VALUE_TEXT wxGRID_VALUE_STRING
#define wxGRID_VALUE_LONG wxGRID_VALUE_NUMBER
@@ -99,6 +100,9 @@ class WXDLLIMPEXP_FWD_CORE wxTextCtrl;
#if wxUSE_SPINCTRL
class WXDLLIMPEXP_FWD_CORE wxSpinCtrl;
#endif
#if wxUSE_DATEPICKCTRL
class WXDLLIMPEXP_FWD_CORE wxDatePickerCtrl;
#endif
class wxGridFixedIndicesSet;
@@ -1354,6 +1358,7 @@ public:
void SetColFormatBool(int col);
void SetColFormatNumber(int col);
void SetColFormatFloat(int col, int width = -1, int precision = -1);
void SetColFormatDate(int col, const wxString& format = wxString());
void SetColFormatCustom(int col, const wxString& typeName);
// ------ row and col formatting

View File

@@ -153,12 +153,17 @@ private:
#include "wx/datetime.h"
// the default renderer for the cells containing times and dates
class WXDLLIMPEXP_ADV wxGridCellDateTimeRenderer : public wxGridCellStringRenderer
// renderer for the cells containing dates only, without time component
class WXDLLIMPEXP_ADV wxGridCellDateRenderer : public wxGridCellStringRenderer
{
public:
wxGridCellDateTimeRenderer(const wxString& outformat = wxDefaultDateTimeFormat,
const wxString& informat = wxDefaultDateTimeFormat);
explicit wxGridCellDateRenderer(const wxString& outformat = wxString());
wxGridCellDateRenderer(const wxGridCellDateRenderer& other)
: m_oformat(other.m_oformat),
m_tz(other.m_tz)
{
}
// draw the string right aligned
virtual void Draw(wxGrid& grid,
@@ -180,11 +185,33 @@ public:
protected:
wxString GetString(const wxGrid& grid, int row, int col);
virtual bool Parse(const wxString& text, wxDateTime& result);
wxString m_oformat;
wxDateTime::TimeZone m_tz;
};
// the default renderer for the cells containing times and dates
class WXDLLIMPEXP_ADV wxGridCellDateTimeRenderer : public wxGridCellDateRenderer
{
public:
wxGridCellDateTimeRenderer(const wxString& outformat = wxDefaultDateTimeFormat,
const wxString& informat = wxDefaultDateTimeFormat);
wxGridCellDateTimeRenderer(const wxGridCellDateTimeRenderer& other)
: wxGridCellDateRenderer(other),
m_iformat(other.m_iformat),
m_dateDef(other.m_dateDef)
{
}
virtual wxGridCellRenderer *Clone() const wxOVERRIDE;
protected:
virtual bool Parse(const wxString& text, wxDateTime& result) wxOVERRIDE;
wxString m_iformat;
wxString m_oformat;
wxDateTime m_dateDef;
wxDateTime::TimeZone m_tz;
};
#endif // wxUSE_DATETIME

View File

@@ -373,6 +373,41 @@ public:
wxDECLARE_NO_COPY_CLASS(wxGridCellAutoWrapStringEditor);
};
#if wxUSE_DATEPICKCTRL
class WXDLLIMPEXP_ADV wxGridCellDateEditor : public wxGridCellEditor
{
public:
wxGridCellDateEditor() { }
virtual void Create(wxWindow* parent,
wxWindowID id,
wxEvtHandler* evtHandler) wxOVERRIDE;
virtual void SetSize(const wxRect& rect) wxOVERRIDE;
virtual void BeginEdit(int row, int col, wxGrid* grid) wxOVERRIDE;
virtual bool EndEdit(int row, int col, const wxGrid* grid,
const wxString& oldval, wxString *newval) wxOVERRIDE;
virtual void ApplyEdit(int row, int col, wxGrid* grid) wxOVERRIDE;
virtual void Reset() wxOVERRIDE;
virtual wxGridCellEditor *Clone() const wxOVERRIDE;
virtual wxString GetValue() const wxOVERRIDE;
protected:
wxDatePickerCtrl* DatePicker() const;
private:
wxDateTime m_value;
wxDECLARE_NO_COPY_CLASS(wxGridCellDateEditor);
};
#endif // wxUSE_DATEPICKCTRL
#endif // wxUSE_GRID
#endif // _WX_GENERIC_GRID_EDITORS_H_