Discussion:
Expanding wxGrid to exactly fit
(too old to reply)
Phillip Seaver
2009-05-21 20:54:47 UTC
Permalink
I have a grid with a few columns. I'd like to autosize all except one
column, which would be expanded so that the grid fills the width of the
space available to it.

I've tried autosizing the columns manually and then calling
GetGridWindow().GetSize().GetWidth() to try to get the width of the
space the grid is in, but that returns a much smaller number than the
actual window width. I've also tried
GetGridWindow()->GetParent()->GetSize().GetWidth() but the number is
similar.

Has anyone done something like this?

Thanks,

Phillip
Vadim Zeitlin
2009-05-22 13:33:55 UTC
Permalink
On Thu, 21 May 2009 16:54:47 -0400 Phillip Seaver <***@apago.com> wrote:

PS> I have a grid with a few columns. I'd like to autosize all except one
PS> column, which would be expanded so that the grid fills the width of the
PS> space available to it.
...
PS> Has anyone done something like this?

The grid sample does it with its "bugs grid":

BugsGridFrame::BugsGridFrame()
{
wxGrid *grid = new wxGrid(this, wxID_ANY);
...
grid->Fit();
SetClientSize(grid->GetSize());
}

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Phillip Seaver
2009-05-22 15:52:20 UTC
Permalink
Post by Vadim Zeitlin
PS> I have a grid with a few columns. I'd like to autosize all except one
PS> column, which would be expanded so that the grid fills the width of the
PS> space available to it.
...
PS> Has anyone done something like this?
BugsGridFrame::BugsGridFrame()
{
wxGrid *grid = new wxGrid(this, wxID_ANY);
...
grid->Fit();
SetClientSize(grid->GetSize());
}
Regards,
VZ
Sorry, I meant expanding a column so it fits. The grid has 2 columns
now that take up about 10% of the width. I'd like to change the width
of one column so that it takes up the remainder of the width. It looks
nicer that way.

I don't know if I'll first need to do something to grid so that it takes
up the whole space, but I already have it in a sizer and set to expand.

Thanks again,

Phillip
Vadim Zeitlin
2009-05-22 16:22:01 UTC
Permalink
On Fri, 22 May 2009 11:52:20 -0400 Phillip Seaver <***@apago.com> wrote:

PS> Sorry, I meant expanding a column so it fits.

Ah, sorry for misunderstanding you. In this case you will need to resize
the column itself to take up the difference between the total grid size and
the sum of sizes of the other columns as well as the row labels width, i.e.
there is no way to do it automatically but it's still doable.

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Milan Babuskov
2009-05-22 16:32:38 UTC
Permalink
Post by Phillip Seaver
Post by Vadim Zeitlin
PS> I have a grid with a few columns. I'd like to autosize all except one
PS> column, which would be expanded so that the grid fills the width of the
PS> space available to it.
PS> Has anyone done something like this?
Sorry, I meant expanding a column so it fits. The grid has 2 columns
now that take up about 10% of the width. I'd like to change the width
of one column so that it takes up the remainder of the width. It looks
nicer that way.
I did this a few times. First, autosize the columns to get minimal
widths. Then, calculate the width of row label + columns + scrollbar and
then set the column's width. You'll need these:

GetScrollThumb(wxVERTICAL)
GetRowLabelSize()

The main problem with this approach is when user can change the size of
the grid by changing the size of the window it is in. I do this by
handling the wxSizeEvent, however it seems to be sometimes called before
the sizing actually happens, so you get the "old" value in event.GetSize().

I'd be happy to learn about some good way to work around that.
--
Milan Babuskov
http://www.flamerobin.org
http://www.guacosoft.com
Milan Babuskov
2009-05-22 17:07:30 UTC
Permalink
Post by Milan Babuskov
GetScrollThumb(wxVERTICAL)
GetRowLabelSize()
The main problem with this approach is when user can change the size of
the grid by changing the size of the window it is in. I do this by
handling the wxSizeEvent, however it seems to be sometimes called before
the sizing actually happens, so you get the "old" value in event.GetSize().
I'd be happy to learn about some good way to work around that.
I just tested, and that is not the real problem. The reported size is
correct. Problem is the horizontal scrollbar. Apparently, the decision
whether to show horizontal scrollbar or not is made before the Size
event, so any changes you make to grid column widths are not accounted.

Any ideas how to work around that?
--
Milan Babuskov
http://www.flamerobin.org
http://www.guacosoft.com
Phillip Seaver
2009-05-22 17:13:35 UTC
Permalink
Post by Milan Babuskov
Post by Phillip Seaver
Post by Vadim Zeitlin
PS> I have a grid with a few columns. I'd like to autosize all
except one
PS> column, which would be expanded so that the grid fills the width
of the
PS> space available to it.
PS> Has anyone done something like this?
Sorry, I meant expanding a column so it fits. The grid has 2 columns
now that take up about 10% of the width. I'd like to change the width
of one column so that it takes up the remainder of the width. It looks
nicer that way.
I did this a few times. First, autosize the columns to get minimal
widths. Then, calculate the width of row label + columns + scrollbar
GetScrollThumb(wxVERTICAL)
GetRowLabelSize()
The main problem with this approach is when user can change the size
of the grid by changing the size of the window it is in. I do this by
handling the wxSizeEvent, however it seems to be sometimes called
before the sizing actually happens, so you get the "old" value in
event.GetSize().
I'd be happy to learn about some good way to work around that.
That's what I'm doing, except for the GetScrollThumb() -- I hadn't
thought of that. Thanks. :-)

The problem I'm running into is that I can't figure out how wide the
grid area is. The number I get is about the size the sum of the column
sizes, so I don't know if I'm calling the wrong thing to get the total
width or if I need to change something to get the grid to really
expand. As I said, I tried calling GetGridWindow().GetSize().GetWidth()
and GetGridWindow()->GetParent()->GetSize().GetWidth() on the wxGrid
object, but the number is too small.

I'll mess around with it more later, and, if necessary, I'll create a
small example program.

Thanks,

Phillip
Vadim Zeitlin
2009-05-22 18:04:18 UTC
Permalink
On Fri, 22 May 2009 19:07:30 +0200 Milan Babuskov <***@panonnet.net> wrote:

MB> Milan Babuskov wrote:
MB> > GetScrollThumb(wxVERTICAL)
MB> > GetRowLabelSize()
MB> >
MB> > The main problem with this approach is when user can change the size of
MB> > the grid by changing the size of the window it is in. I do this by
MB> > handling the wxSizeEvent, however it seems to be sometimes called before
MB> > the sizing actually happens, so you get the "old" value in event.GetSize().
MB> >
MB> > I'd be happy to learn about some good way to work around that.
MB>
MB> I just tested, and that is not the real problem. The reported size is
MB> correct. Problem is the horizontal scrollbar. Apparently, the decision
MB> whether to show horizontal scrollbar or not is made before the Size
MB> event, so any changes you make to grid column widths are not accounted.
MB>
MB> Any ideas how to work around that?

You can use SendSizeEvent() to force a relayout but this is a dirty
solution and it would be better to avoid it. To be honest I don't really
understand where does the problem come from: if you process the size event
before wxGrid (don't you?) then it shouldn't be able to change its
scrollbars visibility yet. So if the width of the columns is adjusted in
such a way that scrollbars are never shown at all I don't understand why do
they appear.

If anybody wants to debug this it would be interesting to check how do you
get into the "need scrolling" branch of wxScrollHelper::DoAdjustScrollbar().

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Milan Babuskov
2009-05-22 18:10:56 UTC
Permalink
Post by Phillip Seaver
The problem I'm running into is that I can't figure out how wide the
grid area is. The number I get is about the size the sum of the column
sizes, so I don't know if I'm calling the wrong thing to get the total
width or if I need to change something to get the grid to really
expand. As I said, I tried calling GetGridWindow().GetSize().GetWidth()
and GetGridWindow()->GetParent()->GetSize().GetWidth() on the wxGrid
object, but the number is too small.
Just handle the wxSizeEvent for the grid (you'll need this anyway if
user can maximize the window and grid grows), and use event.GetSize() to
read it.
--
Milan Babuskov
http://www.flamerobin.org
http://www.guacosoft.com
Phillip Seaver
2009-05-26 15:23:28 UTC
Permalink
Post by Milan Babuskov
Post by Phillip Seaver
The problem I'm running into is that I can't figure out how wide the
grid area is. The number I get is about the size the sum of the column
sizes, so I don't know if I'm calling the wrong thing to get the total
width or if I need to change something to get the grid to really
expand. As I said, I tried calling GetGridWindow().GetSize().GetWidth()
and GetGridWindow()->GetParent()->GetSize().GetWidth() on the wxGrid
object, but the number is too small.
Just handle the wxSizeEvent for the grid (you'll need this anyway if
user can maximize the window and grid grows), and use event.GetSize()
to read it.
That solved my problem. Obvious once you mentioned it. ;-)

I also called SetScrollLineX(1) on my grid so that it wouldn't create a
scrollbar when I made it fit.

Thanks!

Phillip
Milan Babuskov
2009-06-01 13:59:58 UTC
Permalink
Post by Vadim Zeitlin
MB> I just tested, and that is not the real problem. The reported size is
MB> correct. Problem is the horizontal scrollbar. Apparently, the decision
MB> whether to show horizontal scrollbar or not is made before the Size
MB> event, so any changes you make to grid column widths are not accounted.
MB>
MB> Any ideas how to work around that?
You can use SendSizeEvent() to force a relayout but this is a dirty
solution and it would be better to avoid it. To be honest I don't really
understand where does the problem come from: if you process the size event
before wxGrid (don't you?)
I sure hope so. I did Connect for size event. The grid I'm using has a
single column which I resize dynamically. This is the whole code:

wxGrid *propGrid;
...
propGrid->Connect( wxEVT_SIZE,
wxSizeEventHandler(MainFrame::OnGridSize), NULL, this);
//-----------------------------------------------------------------------------
void MainFrame::OnGridSize(wxSizeEvent& event)
{
resizeGrid(propGrid, event.GetSize());
}
//-----------------------------------------------------------------------------
void resizeGrid(wxGrid *grid, const wxSize& size)
{
grid->SetRowLabelSize(50);
int wc = size.GetWidth()-50-grid->GetScrollThumb(wxVERTICAL)-2;
grid->SetColSize(0, wc);
}
//-----------------------------------------------------------------------------

Please note that this issue does not happen on each resize, but only
about 60-70% of the time. Sometimes it does not show the horizontal
scrollbar.
Post by Vadim Zeitlin
If anybody wants to debug this it would be interesting to check how do you
get into the "need scrolling" branch of wxScrollHelper::DoAdjustScrollbar().
I'm not sure I understood this. Could you tell me how to modify the
above code to try that?

Thanks,
--
Milan Babuskov
http://www.flamerobin.org
http://www.guacosoft.com
Loading...