GridBagSizer

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Kocsonya
In need of some credit
In need of some credit
Posts: 9
Joined: Fri May 18, 2018 6:45 am

GridBagSizer

Post by Kocsonya »

Given a wxFrame, I'd like to populate the client area with the following:
On the left, an image.
Right of the image 5 buttons, in 3 rows, the first 2 rows have 2 buttons each, the third only one.
In addition, since the image is relatively tall compared to buttons, I'd like the buttons spaced in the vertical direction. I will use the 'code' mode to create some ASCII art (P is the picture, B is a button):

Code: Select all

PPPPPPP
PPPPPPP  BBBB  BBBB
PPPPPPP
PPPPPPP  BBBB  BBBB
PPPPPPP
PPPPPPP     BBBB
PPPPPPP
Now, the GridBagSizer should be able to do that very easily. The grid is 3x3 cells. Add the picture to (0,0) and set it to span 3 rows. Add the first four buttons to (0,1), (0,2), (1,1), (1,2), respectively and then add the 5th button to (2,1) with a 2-column span. All buttons should be centred both vertically and horizontally. Then tell the sizer that all 3 rows are growable by an equal factor. Here's the code that I believe should do that (all buttons and the image are already created and 'this' is a wxFrame):

Code: Select all

sizer->Add( image, wxGBPosition( 0,0 ), wxGBSpan( 3,1 ) );
sizer->Add( butt1, wxGBPosition( 0,1 ), wxGBSpan( 1,1 ), wxALIGN_CENTRE );
sizer->Add( butt2, wxGBPosition( 0,2 ), wxGBSpan( 1,1 ), wxALIGN_CENTRE );
sizer->Add( butt3, wxGBPosition( 1,1 ), wxGBSpan( 1,1 ), wxALIGN_CENTRE );
sizer->Add( butt4, wxGBPosition( 1,2 ), wxGBSpan( 1,1 ), wxALIGN_CENTRE );
sizer->Add( butt5, wxGBPosition( 2,1 ), wxGBSpan( 1,2 ), wxALIGN_CENTRE );
sizer->AddGrowableRow( 0, 1 );
sizer->AddGrowableRow( 1, 1 );
sizer->AddGrowableRow( 2, 1 );
sizer->Fit( this );
sizer->SetSizeHints( this );
The problem is, the result is very different from the expected. The first two rows remain in their original size and the third gets all the extra space. So the end result, in the same code-for-ASCII-art looks like this:

Code: Select all

PPPPPPP  BBBB  BBBB
PPPPPPP  BBBB  BBBB
PPPPPPP
PPPPPPP
PPPPPPP     BBBB
PPPPPPP
PPPPPPP
So it seems that row 0 and row 1 are only as high as the buttons require and row 2 was expanded to accommodate all the space forced on the sizer by the height of the image.

Now the very strange thing is, if I then resize the frame to be taller (pulling down the bottom by the mouse), then the extra space given by that action is evenly distributed between the rows - on top of their not equal original height. So the equal distribution of additional space works, but only after the initial layout was created. I could not find anything that would specifically control the initial layout and force it to make the rows equal height.

Can someone tell me what's wrong with the code? What do I miss which would set the initial height of the rows equal (and all larger than the height of the button they contain)? Or is my understanding of the GridBagSizer completely wrong? That is entirely possible. I'm quite familiar with the grid sizer of Tk (that is used as the default GUI engine for Tcl, Python, Perl and possibly other scripting languages) which, it seems to me, is very similar to the GridBagSizer apart from the API details and a few features that GridBagSizer seemingly lacks. But it's entirely possible that they are actually very different animals and thinking Tk grid way is a very big no-no with the GridBagSizer and that causes me to miss the obvious.

In any case, any information that would help me understand the actual behavior of the sizer (and achieve my target layout) would be much appreciated.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: GridBagSizer

Post by doublemax »

That looks like a bug in GridBagSizer. I played with it a bit and it seems the effect is determined by the height of the image. If that is less than the combined height of the 3 buttons, the space distribution works as expected.

You can open a bug report here: http://trac.wxwidgets.org/

If you need a fast workaround, put only the buttons in the GridBagSizer and use an additional horizontal wxBoxSizer to place the image next to the buttons.
Use the source, Luke!
Post Reply