Because of all this flexibility, packing boxes in GTK can be confusing at first. There are a lot of options, and it's not immediately obvious how they all fit together. In the end, however, there are basically five different styles. Figure 4.3, “Packing: Five Variations” illustrates the result of running the program packbox.st with an argument of 1:
Each line contains one horizontal box (hbox) with several
buttons. The call to pack is shorthand for the call to pack each of the
buttons into the hbox. Each of the buttons is packed into the hbox the same
way (i.e., same arguments to the packStart:expand:fill:padding:
method).
This is an example of the packStart:expand:fill:padding: method.
box packStart: child expand: expand fill: fill padding: padding
box is the box you are packing the object
into; the first argument is the child object to be
packed. The objects will all be buttons for now, so we'll be packing buttons
into boxes.
As previously noted, the expand argument to
packStart and
packEnd controls whether the widgets are laid
out in the box to fill in all the extra space in the box so the box is
expanded to fill the area allotted to it (true); or the
box is shrunk to just fit the widgets (false). Setting
expand to false will allow you to do right and left
justification of your widgets. Otherwise, they will all expand to fit into
the box, and the same effect could be achieved by using only one of
packStart or
packEnd.
As previously noted, the fill
argument to the pack methods control whether the extra space is
allocated to the objects themselves (true), or as
extra padding in the box around these objects
(false). It only has an effect if the expand
argument is also true.
When creating a new box, the function looks like this:
hbox = GTK.GtkHBox new: false spacing: 0 vbox = GTK.GtkVBox new: false spacing: 0
The homogeneous argument to
GTK.GtkHBox and GTK.GtkVBox controls
whether each object in the box has the same size (i.e., the same width in an
hbox, or the same height in a vbox). If it is set, the pack routines
function essentially as if the expand argument was always turned on.
What's the difference between spacing
(set when the box is created) and padding (set when
elements are packed)? Spacing is added between objects, and padding is added
on either side of an object. Figure 4.4, “Packing with Spacing and Padding” illustrates the
difference; pass an argument of 2 to packbox.st :
Figure 4.5, “Packing with packEnd” illustrates the use of the
packEnd method (pass an argument of 3 to packbox.st). The label
"end" is packed with the packEnd method. It will
stick to the right edge of the window when the window is resized.