Introduction

When you use GridBagSizers, controls may get spaced more than you 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:

  1. If you add a small control to a GridBagSizer, say, a 5x5 button, the sizer cell will always occupy at least 10x20.

  2. 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.
  3. 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) (notice that now we need a span of (1, 3), the GridBagSizer will use at least 25px for the other cells (and things will look closer):

+--------+--------+--------+--------+--------+
|           <-100 px->              |        |
+--------+--------+--------+--------+--------+
|        <- 75px ->        | <25px> |        |
+--------+--------+--------+--------+--------+
| <25px> | <25px> | <25px> | <25px> |        |
+--------+--------+--------+--------+--------+

And yes, the pictures above refer only to horizontal spanning, but vertical spanning behaves the same way.

-- Tacao 2005-11-30 21:17:04

wxGridBagSizer (last edited 2008-03-11 10:50:25 by localhost)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.