2. Details of Boxes

Because of 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.

Each line contains one horizontal box (hbox) with several buttons. The call to Gtk2::Box::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 Gtk2::Box::pack_start() function).

This is the declaration of the gtk_box_pack_start() function:

Gtk2::Box->pack_start ($instance, $child, $expand, $fill, $padding);

The first argument is the box you are packing the object into, the second is the object. The objects will all be buttons for now, so we'll be packing buttons into boxes.

The $expand argumento to Gtk2::Box::pack_start() and Gtk2::Box::pack_end() 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 aread 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 Gtk2::Box::pack_start() or Gtk2::Box::pack_end().

The $fill argument to the Gtk2::Box::pack functions 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:

$box = Gtk2->HBox->new($homogenous, $spacing);

The $homogeneous argument to Gtk2::HBox::new() (and the same for Gtk2::VBox::new()) 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 Gtk2::Box::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. The following figure should make it clearer:

Here is the code used to create the above images. I've commented it fairly heavily so I hope you won't have any problems following it. Run it yourself and play with it.