From fe35ddd34b9e3b97f064042f16556ca4efe89b2c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 1 Nov 2019 18:46:45 +0100 Subject: [PATCH 1/2] Make date cells in the grid sample more readable Add labels for the columns using date format and labels for the cells using date renderer/editor explicitly to give a better idea about what's going on here. Also use the same rows numbers for date cells as for the column ones for consistency. No real changes. See 659ab78c6d106b6966a5f3f7d202690ad4065a32 --- samples/grid/griddemo.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/samples/grid/griddemo.cpp b/samples/grid/griddemo.cpp index 30617b9ee7..aa6f348e5e 100644 --- a/samples/grid/griddemo.cpp +++ b/samples/grid/griddemo.cpp @@ -565,15 +565,21 @@ GridFrame::GridFrame() grid->SetCellAlignment(3, 9, wxALIGN_CENTRE, wxALIGN_TOP); grid->SetCellValue(3, 10, "<- This numeric cell should be centred"); + grid->SetCellValue(0, 13, "Localized date\ncolumn"); grid->SetColFormatDate(13); // Localized by default. + grid->SetCellValue(1, 13, "Today"); + grid->SetCellValue(0, 14, "ISO 8601 date\ncolumn"); grid->SetColFormatDate(14, "%Y-%m-%d"); // ISO 8601 date format. + grid->SetCellValue(1, 14, "Tomorrow"); - grid->SetCellValue(7, 0, "Today"); - grid->SetCellRenderer(7, 0, new wxGridCellDateRenderer); - grid->SetCellEditor(7, 0, new wxGridCellDateEditor); - grid->SetCellValue(8, 0, "Tomorrow"); - grid->SetCellRenderer(8, 0, new wxGridCellDateRenderer("%Y-%m-%d")); - grid->SetCellEditor(8, 0, new wxGridCellDateEditor); + grid->SetCellValue(13, 0, "Date cell:"); + grid->SetCellValue(13, 1, "Today"); + grid->SetCellRenderer(13, 1, new wxGridCellDateRenderer); + grid->SetCellEditor(13, 1, new wxGridCellDateEditor); + grid->SetCellValue(14, 0, "ISO date cell:"); + grid->SetCellValue(14, 1, "Tomorrow"); + grid->SetCellRenderer(14, 1, new wxGridCellDateRenderer("%Y-%m-%d")); + grid->SetCellEditor(14, 1, new wxGridCellDateEditor); const wxString choices[] = { From 19844d27acf8177b721e77d3ee696286ae6567ab Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 4 Nov 2019 00:35:32 +0100 Subject: [PATCH 2/2] Fix default attribute wxGridCellAttr::GetNonDefaultAlignment() This function is not supposed to overwrite the given alignment values unless the alignment is specifically set for the given cell, but it always overwrote them when called on m_defaultCellAttr, i.e. the attribute used by the cells that don't have any special attribute. This meant that custom attributes had to be set (or, more efficiently, a custom attribute provider returning non-null attributes for all cells had to be used) previously just to make the right alignment used by default by number or date renderers work. Fix this by ignoring the values set in the default attribute in this function. Also add a unit test (which required adding a special helper class just to allow testing wxGrid::GetCellAttr() used by the renderers) that used to fail but passes now. --- src/generic/grid.cpp | 5 +++++ tests/controls/gridtest.cpp | 43 +++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 6bd351dd84..f940fbbd40 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -579,6 +579,11 @@ void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const void wxGridCellAttr::GetNonDefaultAlignment(int *hAlign, int *vAlign) const { + // Default attribute can only have default alignment, so don't return it + // from this function. + if ( this == m_defGridAttr ) + return; + if ( hAlign && m_hAlign != wxALIGN_INVALID ) *hAlign = m_hAlign; diff --git a/tests/controls/gridtest.cpp b/tests/controls/gridtest.cpp index cadc110392..722e34e913 100644 --- a/tests/controls/gridtest.cpp +++ b/tests/controls/gridtest.cpp @@ -31,6 +31,28 @@ #include "waitforpaint.h" +namespace +{ + +// Derive a new class inheriting from wxGrid just to get access to its +// protected GetCellAttr(). This is not pretty, but we don't have any other way +// of testing this function. +class TestableGrid : public wxGrid +{ +public: + explicit TestableGrid(wxWindow* parent) + : wxGrid(parent, wxID_ANY) + { + } + + wxGridCellAttr* CallGetCellAttr(int row, int col) const + { + return GetCellAttr(row, col); + } +}; + +} // anonymous namespace + class GridTestCase : public CppUnit::TestCase { public: @@ -60,6 +82,7 @@ private: CPPUNIT_TEST( Labels ); CPPUNIT_TEST( SelectionMode ); CPPUNIT_TEST( CellFormatting ); + CPPUNIT_TEST( GetNonDefaultAlignment ); WXUISIM_TEST( Editable ); WXUISIM_TEST( ReadOnly ); WXUISIM_TEST( ResizeScrolledHeader ); @@ -100,6 +123,7 @@ private: void Labels(); void SelectionMode(); void CellFormatting(); + void GetNonDefaultAlignment(); void Editable(); void ReadOnly(); void WindowAsEditorControl(); @@ -127,7 +151,7 @@ private: static bool ms_nativeheader; static bool ms_nativelabels; - wxGrid *m_grid; + TestableGrid *m_grid; wxDECLARE_NO_COPY_CLASS(GridTestCase); }; @@ -144,7 +168,7 @@ bool GridTestCase::ms_nativelabels = false; void GridTestCase::setUp() { - m_grid = new wxGrid(wxTheApp->GetTopWindow(), wxID_ANY); + m_grid = new TestableGrid(wxTheApp->GetTopWindow()); m_grid->CreateGrid(10, 2); m_grid->SetSize(400, 200); @@ -805,6 +829,21 @@ void GridTestCase::CellFormatting() CPPUNIT_ASSERT_EQUAL(*wxGREEN, m_grid->GetCellTextColour(0, 0)); } +void GridTestCase::GetNonDefaultAlignment() +{ + // GetNonDefaultAlignment() is used by several renderers having their own + // preferred alignment, so check that if we don't reset the alignment + // explicitly, it doesn't override the alignment used by default. + wxGridCellAttr* attr = NULL; + int align = wxALIGN_RIGHT; + + attr = m_grid->CallGetCellAttr(0, 0); + REQUIRE( attr ); + + attr->GetNonDefaultAlignment(&align, NULL); + CHECK( align == wxALIGN_RIGHT ); +} + void GridTestCase::Editable() { #if wxUSE_UIACTIONSIMULATOR