Gtk2::Box
and Gtk2::Table
classes can contain more than one child.
The Gtk2::Box
class is further divided into Gtk2::HBox
and Gtk2::VBox
sub classes. In later sections we will do more advanced containers like Gtk2::TreeView
and Gtk2::TextView
.
Gtk2::Box
A horizontal or vertical Gtk2::Box
can be used to contain a row or column of widgets.
Various options can be set to modify the spacing between the widgets, the alignment of the widgets in the Gtk2::Box
, or the behaviors of the Gtk2::Box
when the user resizes the parent widget. Boxes work only in one direction. The widgets inside a horizontal box always have the height of the box, and widgets in a vertical box always have the width of the vertical box.
You can create a Gtk2::HBox instance by calling:
widget = Gtk2::HBox->new ($homogeneous=0, $spacing=5)
$homogeneous
is a boolean, telling Gtk+ whether the children should each have the same space in the box.
$spacing
is an integer, which tells Gtk+ how much space to leave between the widgets in the box.
To add widgets to the Gtk2::Box, use the following method:
$box->pack_end ($child, $expand, $fill, $padding)or
$box->pack_start ($child, $expand, $fill, $padding)
The illustrations below show how it will be packed in the Gtk2::HBox and Gtk2::VBox respectively.
When packing a widget into a Gtk2::Box
class widget using the pack_end
or pack_start
methods, you need to specify these values.
$expand
is of type boolean, which will cause the area surrounding the widget to expand when the window gets resized.
The picture below shows three buttons packed into a Gtk2::VBox
instance. The top button is packed using $vbox->pack_start($btn_start,TRUE,FALSE,4);
.The bottom two was packed using $vbox->pack_start($btn_start,FALSE,FALSE,4);
Notice that the area around the top button got expanded when the window was stretched.
$fill
is of type boolean, which will cause the packed widget to expand within its area that it is packed in, when the window gets resized. Thus for this to have an effect, the value of $expand
has to be set to TRUE
, else the area that it is packed in will not expand in the first place!
The picture below shows three buttons packed into a Gtk2::VBox
instance. The top button is packed using $vbox->pack_start($btn_start,TRUE,TRUE,4);
.The bottom two was packed using $vbox->pack_start($btn_start,FALSE,FALSE,4);
Notice that the top button expanded within the area around it when the window was stretched.
$padding
is the value on both sides that will be padded. This sides differ depending on the type of Gtk2::Box
.
[1]
A Gtk2::VBox
will have it on the top and bottom of the widget.
A Gtk2::HBox
will have it on the left and right side of the widget.
When creating an instance of Gtk2::HBox
or Gtk2::VBox
you need to specify the value of $homogeneous
.
$homogeneous
is of type boolean. Setting this to "TRUE" will have the effect of setting the $expand
value to "TRUE" on ALL the packed widgets. This will then give equal space to all widgets packed into the box.
Most of the times this is not desirable, and as a rule of thumb, should be set to FALSE #widget = |
Gtk2::Table
The Gtk2::Table
widget is used to set widgets in a grid inside the parent widget. It acts like a grid with cells, in which you can hang your widgets. If the user resizes the parent widget, then the size of the grid cells changes, and the widgets will change their location and size, based on the size of the new grid cells.
Consider the following drawing:
The perl code to create it will include the following:
To create the Gtk2::Table
object:
#widget =Gtk2::Table
->new ($rows, $columns, $homogeneous=FALSE) my $widget =Gtk2::Table
->new (2, 2, FALSE);
To add the Gtk2::Label
object:
#$table->attach_defaults ($widget, $left_attach, $right_attach, $top_attach, $bottom_attach) $table->attach_defaults ($label, 0, 2, 0, 1);
To add the Gtk2::Button
object:
$table->attach_defaults ($button, 0, 1, 1, 2);
To add the Gtk2::Entry
object:
$table->attach_defaults ($entry, 1, 2, 1, 2);
To fit more than one widget onto a Gtk2::Bin
sub class object, you need to use either the Gtk2::Box
(be it Gtk2::HBox
or Gtk2::VBox
) or Gtk2::Table
class widgets. To overcome the the limitations of Gtk2::Bin
, do the following:
Create a Gtk2::Box
or Gtk2::Table
class object.
Populate it with widgets
Add the Gtk2::Box
or Gtk2::Table
class object to the Gtk2::Bin
sub-class widget ( IE Gtk2::Window
) using the Gtk2::Container
's add
method.
[1] | You may notice that, even when you set the padding to "0", the widgets are not really touching each other, when packed. This is because any widgets that gets packet into |