Tuesday, November 29, 2005, 1:42:12 PM, Werner F. Bruhin wrote:
(...) I was surprised to see that you use mainly box sizers. In the
past I used mostly flexgridsizer in a toplevel boxsizer, currently I
am switching a lot over to gridbagsizer and more then halved the
number of sizers needed on some of screens and I think they get
better aligned. Although there are some exceptions were things get
spaced more then I want and I haven't figured it out yet. What took
me a while to figure out that with the gridbagsizer I had to use not
only wx.EXPAND, but also use the Span(n, n) and grow column and rows
appropriatly.
Gridbagsizers may space controls more than we want for a couple of
non-obvious reasons.
The first thing you might wish to check is the empty cell size.
Gridbagsizers have a default empty cell size value of (10, 20). It
means that:
- If you add a small control to a gridbagsizer, say, a 5x5 button,
the sizer cell will always occupy at least 10x20.
- If you add controls to non-adjacent rows, say, to rows 5 and 7,
and don't add anything to row #6, the row #6 will be a 20px-height
empty space. In an analogous way, empty cols will always have a
width of 10px.
- You can change the default empty cell size value using the
SetEmptyCellSize(width, height) method of the GridbagSizer. Passing
a width, height of 0, 0, for example, would make the rows 5 and 7 I
mentioned above look like they are adjacent (and the 5x5 button
would be placed in a 5x5 cell).
The second thing is related to spanning. Consider the following
gridbagsizer (the cell/empty cell sizes now won't make difference):
···
+--------+--------+--------+--------+--------+
> > > > >
+--------+--------+--------+--------+--------+
> > > > >
+--------+--------+--------+--------+--------+
> > > > >
+--------+--------+--------+--------+--------+
Now if you add something with a width of 100px to pos (0, 0) with a
span of (1, 2), the gridbagsizer will use 100px/2 = 50px as the
minimum width of all cells below the spanned cell:
+--------+--------+--------+--------+--------+
<-100 px-> | | | |
+--------+--------+--------+--------+--------+
<50px> | <50px> | | | |
+--------+--------+--------+--------+--------+
<50px> | <50px> | | | |
+--------+--------+--------+--------+--------+
Now for the tricky thing. If you add something with a width of 75px to
pos (1, 0), the gridbagsizer will still use at least 50px for the
other cells, no matter if you add something to them or not:
+---------------------+--------+--------+--------+
<-125 px-> | | | |
+-------------------- +--------+--------+--------+
<-75 px -> | <50px> | | | |
+------------+--------+--------+--------+--------+
<50px> | <50px> | | | |
+------------+--------+--------+--------+--------+
A way to work around this would be to "split" cells; Since the
standard gridbagsizer hasn't a Split() method, in this example you'll
need to increase the span of (0, 0):
+--------+--------+--------+--------+--------+
<-100 px-> | |
+--------+--------+--------+--------+--------+
<25px> | <25px> | <25px> | <25px> | |
+--------+--------+--------+--------+--------+
<25px> | <25px> | <25px> | <25px> | |
+--------+--------+--------+--------+--------+
And now if you add something with a width of 75px to pos (1, 0), the
gridbagsizer will use at least 25px for the other cells. (And things
will look closer).
And yes, the pictures above refer only to horizontal spanning, but
vertical spanning behaves the same way.
-- tacao
No bits were harmed during the making of this e-mail.