Ruby/Gtk Tutorial | ||
---|---|---|
<<< Previous | Packing Widgets | Next >>> |
Let's take a look at another way of packing - Tables. These can be extremely useful in certain situations.
Using tables, we create a grid that we can place widgets in. The widgets may take up as many spaces as we specify. The first thing to look at, of course, is the Gtk::Table.new method:
Gtk::Table.new rows, columns, homogeneous |
rows is an integer. It is the number of rows to make in the table. It turns out that you can still make additional rows regardless, so this number actually behaves like a minimum.
columns is an integer. It is the number of columnss to make in the table. It turns out that you can still make additional columns regardless, so this number actually behaves like a minimum.
homogeneous is a boolean. It has to do with how the table's boxes are sized. If it is true, the table boxes are resized to the size of the largest widget in the table. If it is false , the size of a table boxes is dictated by the tallest widget in its same row, and the widest widget in its column.
The rows and columns are laid out from 0 to n, where n was the number specified in the call to Gtk::Table.new . So, if you specify rows = 2 and columns = 2, the layout would look something like this:
Note that the coordinate system starts in the upper left hand corner. To place a widget into a box, use the following function:
table.attach widget, left_attach, right_attach, top_attach, bottom_attach |
widget is a Gtk::Widget. It is the widget to attach to the table.
*_attach are all integers. They specify where to place the widget, and how many boxes to use. Try the following example.
require 'gtk' window = Gtk::Window.new Gtk::WINDOW_TOPLEVEL window.set_title "Table" window.signal_connect('delete_event') { Gtk.main_quit } window.border_width 10 # Table table = Gtk::Table.new 2, 2, true window.add table # Attach some buttons. button = Gtk::Button.new "top-left" table.attach button, 0, 1, 0, 1 button = Gtk::Button.new "top-right" table.attach button, 1, 2, 0, 1 button = Gtk::Button.new "bottom" table.attach button, 0, 2, 1, 2 # Done window.show_all Gtk.main |
We also have table.set_row_spacing and table.set_col_spacing. These places spacing between the rows at the specified row or column.
table.set_row_spacing row, spacing table.set_col_spacing column, spacing |
Note that for columns, the space goes to the right of the column, and for rows, the space goes below the row. You can also set a consistent spacing of all rows and/or columns with:
table.set_row_spacings spacing table.set_col_spacings spacing |
Note that with these calls, the last row and last column do not get any spacing.
<<< Previous | Home | Next >>> |
Packing Demonstration Program | Up | Widget Overview |