5.2. Details of Boxes

Because of this flexibility, packing boxes in GTK can confussing 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 five different styles

Each line contains one horizontal box(hbox) with several buttons. The call to Gtk::Box#pack_start is shorthand for the call to pack each of the buttons into the hbox. Each of the buttons are packed into the hbox in the same way, i.e same arguments to the Gtk::Box#pack_start method.

This is the declaration fo teh Gtk::Box#pack_start method.

Gtk::Box#pack_start( child, expand, fill, padding )

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

The expand argument to Gtk::Box#pack_start and Gtk::Box#pack_end controls whether the widgets are laid out in the box to fill 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 the first argument of Gtk::Box#pack_start or Gtk::Box#pack_end.

The fill argument to the Gtk::Box#pack methodscontrol 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 method looks like this:

Gtk::HBox.new( homogeneous, spacing )
Gtk::VBox.new( homogeneous, spacing )

The homogeneous argument 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 methods function essentially as if the expand argument was always turned on.

What's the difference between spacing (set when 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:

The code used to create the images on this page, is avialable on the next page.

Run the program by doing "ruby packbox.rb num", where num is a number between 1 and 3.


Prev Next