|
Page 5 of 7
When creating an application, you'll want to put more than one widget inside a window.
Our first helloworld example only used one widget so we could simply use Gtk::Container#add to "pack" the widget into the window. But when you want to put more than one widget into a window, how do you control where that widget is positioned?
This is where packing comes in.
Theory of Packing Boxes
Most packing is done by creating boxes. These are invisible widget containers that w can pack our widgets into which come in two forms, a horizontal box, and a vertical box.
When packing widgets into a horizontal box, the objects are inserted horizontally from left to right or right to left depending on the method used. In a vertical box, widgets are packed from top to bottom or vice versa. You may use any combination of boxes inside or beside other boxes to create the desired effect.
To create a new horizontal box, we use Gtk::HBox.new, and for vertical boxes, Gtk::VBox.new. The Gtk::Box#pack_start and Gtk::Box#pack_end methods are used to place objects inside of these containers. The Gtk::Box#pack_start method will start at the top and work its way down in a vbox, and pack left to right in an hbox.
Gtk::Box#pack_end will do the opposite, packing from bottom to top in a vbox, and right to left in an hbox. Using these methods allows us to right justify or left justify our widgets and may be mixed in any way to achieve the desired effect. We will use Gtk::Box#pack_start in most of our examples. An object may be another container or a widget. In fact, many widgets are actually containers themselves, including the button, but we usually only use a label inside a button.
By using these methods, GTK knows where you want to place your widgets so it can do automatic resizing and other nifty things. There are also a number of options as to how your widgets should be packed. As you can imagine, this method gives us a quite a bit of flexibility when placing and creating widgets.
Details of Boxes
Because of this flexibility, packing boxes in GTK can be confusing at first. There are a lot o options, and it's not immediately obvious how they all fit together. In the end, however, ther are basically five different styles.

Each line contains one horizontal box (hbox) with several buttons. The call of Gtk::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 Gtk::Box#pack_start method).
The Gtk::Box#pack_start method looks like this:
Gtk::Box#pack_start(child, expand, fill, padding)
The first argument is the object you are packing the into the box. The objects will all be buttons for now, so we'll be packing buttons into boxes.
The expand argument to Gtk::Box#pack_start and Gtk::Box#pack_end controls whether the widgets are laid out in the box:
If expand is true widgets will fill in all the extra space in the box so the box is automatically expanded to fill the area allotted to it;
If expand is false the box is shrunk to just fit the widgets.
By 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 Gtk::Box#pack_start or Gtk::Box#pack_end.
The fill argument controls where the space given to the object by the expand option is allocated:
If fill is tru the object is extented to fit the allocated space;
If fill is false the object is left with its original size, and extra padding is added around it.
Note that the fill argument only has an effect if the expand argument is also true.
When creating a new horizontal box, it looks like this:
Gtk::HBox.new(homogeneous, spacing)
The homogeneous argument to Gtk::HBox.new (and the same for Gtk::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 Gtk::Box#pack_start or Gtk::Box#pack_end methods work essentially as if the expand argument was always set to true.
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 screenshot should make it clearer:

Following is the code used to create the above screenshots. It is commented fairly heavil so I hope you won't have any problems following it. Just play with it.
|