Preface of Ruby/GTK2 Tutorial  Hot PDF Print E-mail
Tag it:
Delicious
Furl it!
Digg
NewsVine
Reddit
YahooMyWeb
Technorati
Articles Reviews Ruby
Written by http://www.namaraii.com/hiki/   
Monday, 21 January 2008
Article Index
Preface of Ruby/GTK2 Tutorial  Hot
Ruby/GTK2 Hello World
Ruby/GTK2 Hello World (commented)
More on Signals Handlers
Packing Widgets
Packing Demonstration Program
Packing Using Tables



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 constructor:

Gtk::Table.new(rows, columns, homogeneous)

The first argument is the number of rows to make in the table, while the second, obviously, is the number of columns.

The homogeneous argument has to do with how the table's boxes are sized. If homogeneous is true, the table boxes are resized to the size of the largest widget in the table. If homogeneous 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

Gtk::Table.new.

Note that the coordinate system starts in the upper left hand corner. To place a widget into a box, use the following method:

Gtk::Table#attach(child,
left_attach, right_attach, top_attach, bottom_attach,
xoptions, yoptions,
xpadding, ypadding)


The first argument ("child") is the widget you wish to place in the table.

The left and right attach arguments specify where to place the widget, and how many boxes to use. If you want a button in the lower right table entry of our 2x2 table, and want it to fill that entry only, left_attach would be = 1, right_attach = 2, top_attach = 1, bottom_attach = 2.

Now, if you wanted a widget to take up the whole top row of our 2x2 table, you'd use

left_attach = 0, right_attach = 2, top_attach = 0, bottom_attach = 1.

The xoptions and yoptions are used to specify packing options and may be bitwise OR'ed
together*1 to allow multiple options.

These options are:

Gtk::FILL

If the table box is larger than the widget, then this widget will expand to use all the room available.

Gtk::SHRINK

If the table widget was allocated less space then was requested (usually by the user resizing the window), then the widgets would normally just be pushed off the bottom of the window and disappear. If Gtk::SHRINK is specified, the widgets will shrink with the table.

Gtk::EXPAND

This will cause the table to expand to use up any remaining space in the window.

Padding is just like in Gtk::Box, creating a clear area around the widget specified in pixels.

  • Gtk::Table#attach has a lot of options. So, there's a shortcut:
  • Gtk::Table#attach_defaults(widget, left_attach, right_attach, top_attach, bottom_attach)


The X and Y options default to Gtk::FILL | Gtk::EXPAND, and X and Y padding are set to 0. The rest of the arguments are identical to the previous method.

We also have Gtk::Table#set_row_spacing and Gtk::Table#set_col_spacing. These places spacing between the rows at the specified row or column.

  • Gtk::Table#set_row_spacing(row, spacing)
  • Gtk::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:

  • Gtk::Table#set_row_spacings(spacing)
  • Gtk::Table#set_col_spacings(spacing)


Note that with these calls, the last row and last column do not get any spacing.

Table Packing Example

Here we make a window with three buttons in a 2x2 table.

The first two buttons will be placed in the upper row. A third, quit button, is placed in the lower row, spanning both columns.

Here is the source code:

#!/usr/bin/env ruby
require 'gtk2'

Gtk.init

window = Gtk::Window.new
window.title = "Table"
window.signal_connect("delete_event") do

Gtk.main_quit
false
end

window.border_width = 20

# Creates a 2x2 table.
table = Gtk::Table.new(2, 2, true)
window.add(table)
[1, 2].each do |i|
button = Gtk::Button.new("button #{i}")
button.signal_connect("clicked") do
puts "Hello again - button #{i} was pressed"
end

# Insert button 1 into the upper left quadrant of the table,
# and button 2 into the upper right quadrant of the table.
table.attach_defaults(button, i - 1, i, 0, 1)
end
button = Gtk::Button.new("Quit")
button.signal_connect("clicked") do
Gtk::main_quit
end

# Insert the quit button into the both lower quadrants of the table.
table.attach_defaults(button, 0, 2, 1, 2)
window.show_all

Gtk.main


User reviews

There are no user reviews for this item.

Add new review




Powered by jReviews



Last Updated ( Monday, 21 January 2008 )
 
Next >