ravenspoint
ravenspoint

Reputation: 20457

wxGrid shows large empty border on right

By default, wxGrid shows a small ( 10 pixels? ) blank border on the right hand side, after the last column. Calling SetMargins() has no effect on it.

It is irritating, but I can live with it.

However, if I set the the row label width to zero then the blank border grows much larger. If I have just one column, the effect is horrible. It looks like wxGrid is leaving room for the non-existent label.

myPatGrid = new wxGrid(panel,IDC_PatGrid,wxPoint(10,10),wxSize(150,300) );
myPatGrid->SetRowLabelSize(0); 
myPatGrid->CreateGrid(200,1);
myPatGrid->SetColLabelValue(0,L"Patient IDs");

Is there a way to remove this border?

enter image description here

Note that if I set the size of the wxgrid window to narrower in the wxGrid constructor, hoping to hide the border, I now get a horizontal scroll bar which is horrible too.

myPatGrid = new wxGrid(panel,IDC_PatGrid,wxPoint(10,10),wxSize(100,300) );
myPatGrid->SetRowLabelSize(0); 
myPatGrid->CreateGrid(200,1);
myPatGrid->SetColLabelValue(0,L"Patient IDs");

Gives me

enter image description here

I just upgraded to wxWidgets v2.8.12 - problem still exists.

Upvotes: 3

Views: 2656

Answers (2)

blech
blech

Reputation: 63

Solving something similar yesterday I would like to contribute with following what does the job for me. Perhaps this is going to help someone else:

void RecalculateGridSize(wxGrid *grid, int cols) {
  if (grid == NULL)
    return;

  grid->AutoSizeColumns();

  float cumulative = 0, param = 0;
  for (int i = 0; i < cols; ++i)
    cumulative += grid->GetColSize(i);

  //not stretching when client size lower then calculated
  if(grid->GetClientSize().x < cumulative)
    return;

  param = (float) grid->GetClientSize().x / cumulative;

  for (int i = 0; i < cols; ++i) {
    if (i != cols - 1)
      grid->SetColSize(i, int(grid->GetColSize(i)*param) - 2); //-2 for each line per column
    else
      grid->SetColSize(i, int(grid->GetColSize(i)*param)); //leaving last column full to fill properly
    }
  }

Note: This is doing particularly well when linked with OnSize() event.

Upvotes: 0

Sga
Sga

Reputation: 3658

I didn't find an "autosize" function to fit columns in the grid space. As a workaround, if you have only one column set its width to

myPatGrid->SetColMinimalWidth(0, grid_width - wxSYS_VSCROLL_X - 10)

otherwise, sum other column's width and adapt the last one to fit the remaining space (minus scrollbar width, minus 10).

EDIT: I have a working example, which produces this:

grid example

int gridSize = 150;
int minSize = gridSize - wxSYS_VSCROLL_X - 2; // scrollbar appear if higher
grid->SetRowLabelSize(0);
grid->SetColMinimalWidth(0, minSize);
grid->SetColSize(0, minSize); // needed, otherwise column will not resize
grid->ForceRefresh();
grid->SetColLabelValue(0, "COORD");

EDIT2: I succeded to remove the remaining margin with this:

int gridSize = 150;
int minSize = gridSize - 16; // trial & error
grid->SetMargins(0 - wxSYS_VSCROLL_X, 0);

enter image description here

Upvotes: 1

Related Questions