Opened 9 months ago

Closed 9 months ago

Last modified 9 months ago

#15809 closed defect (fixed)

wxGridStringTable can't be used without wxGrid

Reported by: eftw Owned by:
Priority: normal Milestone:
Component: wxGrid Version: 2.9.4
Keywords: Cc:
Blocked By: Blocking:
Patch: yes

Description

I expect that the wxGridStringTable class can be used for storing and retrieving strings in a 2D array, but I discover a problem in the implementation.
The following code shows the problem:

wxGridStringTable* dataTable = new wxGridStringTable();
  wxLogMessage( wxT("GetNumberRows %d, GetNumberCols %d"), dataTable->GetNumberRows(), dataTable->GetNumberCols() ); //  report 0  0
  dataTable->AppendCols( 1);
  wxLogMessage( wxT("GetNumberRows %d, GetNumberCols %d"), dataTable->GetNumberRows(), dataTable->GetNumberCols() ); // report 0  1
  dataTable->AppendRows( 1);
  wxLogMessage( wxT("GetNumberRows %d, GetNumberCols %d"), dataTable->GetNumberRows(), dataTable->GetNumberCols() ); // report 1  1
  dataTable->SetValue( 0, 0, wxString("foo"));  // crash: arrstr.h(179) wxArrayString: index out of bounds
  wxDELETE( dataTable);

It seems that the column and row are added, but the added row is an empty one (without colums).
The problem is in the generic/grid.cpp file:

bool wxGridStringTable::AppendRows( size_t numRows )
{
    size_t curNumRows = m_data.GetCount();
    size_t curNumCols = ( curNumRows > 0
                         ? m_data[0].GetCount()
                         : ( GetView() ? GetView()->GetNumberCols() : 0 ) );

If there are no rows the nummer of columns is derived from GetView if exist else 0.
As reported in #10818 (wxGrid sets coloumn count to 0 if grid has no rows) and resolved in changeset 60717 the reported number of columns is correct, unfortunately is m_numCols not used in the code above.
After changing the code into (the same also in InsertRows(), InsertCols() and DeleteCols() )

bool wxGridStringTable::AppendRows( size_t numRows )
{
    size_t curNumRows = m_data.GetCount();
    size_t curNumCols = m_numCols;

It work as expected.
Found in wxWidgets 2.9.4 and 3.0.0
The supplied patch is for 2.9.4

Attachments (1)

wxGridStringTable.patch download (1.6 KB) - added by eftw 9 months ago.

Download all attachments as: .zip

Change History (3)

Changed 9 months ago by eftw

comment:1 Changed 9 months ago by VZ

  • Resolution set to fixed
  • Status changed from new to closed

(In [75551]) Simplify and fix column determination code in wxGridStringTable.

The number of columns in the table is now (since r60717) stored in m_numCols,
so there is no need to get it from wxGrid any more. This is not only simpler
and faster but also more correct as the first row might not have the full
number of values yet.

Closes #15809.

comment:2 Changed 9 months ago by VZ

(In [75552]) Simplify and fix column determination code in wxGridStringTable.

The number of columns in the table is now (since r60717) stored in m_numCols,
so there is no need to get it from wxGrid any more. This is not only simpler
and faster but also more correct as the first row might not have the full
number of values yet.

Closes #15809.

Note: See TracTickets for help on using tickets.