wxGrid sizing issue

James Bigler bigler at cs.utah.edu
Mon Mar 5 06:55:33 PST 2007


I'm trying to use a wxGrid to display some tabular data.  There are two
columns that contain key/value pairs.  Interspersed between the rows are
a few cells that span both columns (think of them as section headers):

Here's some ASCII art:

+---------------------+
| Section A           |
+---------+-----------+
| Key 1   | Value 1   |
+---------+-----------+
| Key 2   | Value 2   |
+---------------------+
| Section B           |
+---------+-----------+
| Key 3   | Value 3   |
+---------+-----------+
| Key 4   | Value 4   |
+---------+-----------+


The problem I'm having is getting the grid to be the right size.  I need
all the cells to display their entire contents, so that no fiddling of
the grid lines is needed to display the cell contents.

I want the whole grid to be a certain size, say 400x400, and for the
cells to fill the space as much as possible.  I would like the first
column to have a minimum width, and the second column to fill the
remaining space.  If the text is too long for a given cell, I would like
word wrapping to put on the next line.  I would like the row height to
be expanded to accommodate the wrapped text.

Here's basically what I'm doing:

     wxGrid* grid = new wxGrid(this, wxID_ANY);
     wxSize gridSize(400,400);
     int key_width = 120;
     grid->SetColMinimalWidth(0, key_width);
     grid->SetColMinimalWidth(1, gridSize.x - key_width);
     grid->EnableScrolling(false, true);
     grid->CreateGrid(0, 2);
     grid->SetColumnWidth(0, key_width);
     grid->SetColumnWidth(1, gridSize.x - key_width);
     grid->EnableEditing(false);
     grid->SetDefaultRenderer( new wxGridCellAutoWrapStringRenderer );
     grid->SetRowLabelSize(0);
     grid->SetColLabelSize(0);

     vector<ThreatInfo::InfoGroup>& groups = gui.info.groups;
     for(size_t group_idx = 0; group_idx < groups.size(); ++group_idx) {
       // Here's the start
       int row_start = grid->GetNumberRows();
       // Figure out how many rows to append
       int numAttributes =
static_cast<int>(groups[group_idx].attributes.size());
       grid->AppendRows(1+numAttributes, 2);

       // Add the name, and set the cell properties
       grid->SetCellSize(row_start, 0, 1, 2); // Cell that spans two colums
       grid->SetCellValue(row_start, 0, groups[group_idx].name);

       for(int attr_idx = 0; attr_idx < numAttributes; ++attr_idx) {
         ThreatInfo::Attribute& attribute =
groups[group_idx].attributes[attr_idx];
         int current_row = attr_idx+row_start+1;
         grid->SetCellValue(current_row, 0, attribute.name);
         grid->SetCellValue(current_row, 1, attribute.value);
       } // end for (attr_idx)
     } // end for (group_idx)

     // To get word wrapping to happen
     grid->AutoSizeRows();
     grid->SetMaxSize(gridSize);
     left_margin->Add(grid);


============================================================
I have two problems with the existing code.

1. The grid is larger than the specified MaxSize (gridSize = (400,400)).
2. The horizontal scroll bar is always present even if I turn it off 
with EnableScrolling(false,true).

Any help would be very much appreciated.

Thanks,
James




More information about the wx-users mailing list