add
.
This page describes box widget and table widget as
how to arrange double or more widgets in the window.
Box is a kind of containter widgets and there are two type box, horizontal box (HBox) and vertiacal box (VBox). Box is like a transparent frame to place widgets and its entity is invisible.
Let's take a look at the example to place button widgets into a box. This example inserts two buttons into a horizontal box.
Program box.rb:
require 'gtk' window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL) hbox = Gtk::HBox.new(false, 0) button1 = Gtk::Button.new('Hello World') button2 = Gtk::Button.new('button2') hbox.pack_start button1, true, true, 0 hbox.pack_start button2, true, true, 0 window.add hbox button1.show button2.show hbox.show window.show Gtk.main
Result of execution
Description of the example program
hbox = Gtk::HBox.new(false, 0)
button1 = Gtk::Button.new('Hello World')
button2 = Gtk::Button.new('button2')
hbox.pack_start button1, true, true, 0
hbox.pack_start button2, true, true, 0
window.add hbox
button1.show
button2.show
hbox.show
window.show
show
for each widget to show them.
example
In the case Gtk::Hbox.new(false, 0)
In the case Gtk::Hbox.new(true, 0)
,
the buttons are same width.
In the case Gtk::Hbox.new(true, 10)
,
gap between the buttons is 10 pixels.
Horizontal/Vertical box have methods for packing,
pack_start
and pack_end
.
pack_start child_widget, expand, fill, padding
If expand is true, box stretches as wide as possible.
If expand is false, box shrinks to fit to the inner widgets.
If fill is true and there are gaps around the inner widgets,
the widgets fill gaps.
If fill is false, the gaps remain.
fill is effective only if expand is true.
padding specify the size (number of pixels) of space
allocated around the each inner widget.
Example
In the above example, for there is only box inside the window,
the window looks the same at started time
irrespective of whether expand, fill are true or false.
but the behavior is different when the window streached
by function of a window manager.
In the following images,
the left one is a startup image, and the right is window-stretched one.
If expand is true, fill is true
and padding is 0,
the HBox fills the window and the button widgets fills the HBox.
=>If expand is true, fill is false and padding is 0,
=>
=>
pack_end child_widget, expand, fill, padding
While box is used to place widgets in vertical/horizontal direction, table is used to align widgets with a grid.
Let's take a look at the example to place button widgets into a table.
Program table.rb:
require 'gtk' window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL) table = Gtk::Table.new(2, 2, false) button1 = Gtk::Button.new('Hello World') button2 = Gtk::Button.new('button2') button3 = Gtk::Button.new('button3') table.attach button1, 0, 1, 0, 1 table.attach button2, 1, 2, 0, 1 table.attach button3, 0, 2, 1, 2 window.add table button1.show button2.show button3.show table.show window.show Gtk.main
Result of execution
Description of the example program
table = Gtk::Table.new(2, 2, false)
table.attach button1, 0, 1, 0, 1
table.attach button2, 1, 2, 0, 1
table.attach button3, 0, 2, 1, 2
For example, a table is laid out as the following figure for rows and columns are specified with 2.
0 1 2 0+----------+----------+ | | | 1+----------+----------+ | | | 2+----------+----------+
Table has the method attach
for placing widgets into a table.
table.attach child_widget, left_attach, right_attach, top_attach, bottom_attach, xoptions, yoptions, xpadding, ypadding
Arguments xoptions, yoptions, xpadding, ypadding are optional. xoptions, yoptions is option how to place a widget. You can give multiple options with bit-wise operator `or'(|). Available option values is defined under Gtk module as constants and shown in the following list.
xpadding, ypadding specify how large space to make sure of the space arround the placed widget in number of pixels.
Exmaple
For xoptions and yoptions
aren't specified in previous example,
it is same as Gtk::FILL | Gtk::EXPAND is specified.
button3 is placed over 2 blocks and
the widget fills the blocks horizontally
If Gtk::FILL and Gtk::EXPAND aren't specified,
for exmple, table.attach button3, 0, 2, 1, 2, 0, 0,
button3 widget doesn't expand.
If moreover the window is expanded by the WM function,
Gtk::FILL and Gtk::EXPAND are specified (implicitly) in the upper row,
the block expands and the widget in the block also expands.
The lower row doesn't expand.
There are the following methods to specify gaps between rows and between columns.
table.set_row_spacing row, spacing
table.set_column_spacing column, spacing
table.set_row_spacings spacing
table.set_column_spacings spacing