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.
This commit is contained in:
Vadim Zeitlin 2019-11-04 00:35:32 +01:00
parent fe35ddd34b
commit 19844d27ac
2 changed files with 46 additions and 2 deletions

View File

@ -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;

View File

@ -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