[clue-cert] multiple widgets in Ruby-Gtk

Dennis J Perkins dennisjperkins at comcast.net
Sun Oct 28 20:51:01 MDT 2007


Multiple Widets
===============

The Gtk::Window class is a subclass of Gtk::Bin.  Gtk::Bin is a
container class that can only hold one widget.  A window with only one
object is not very useful, so why can't a window hold more widgets?

The reason is due to how Gtk organizes widgets in a window.  Instead of
specifying screen coordinates for each widget, a packing manager is used
to pack widgets into boxes.  For more complex layouts, boxes are packed
inside other boxes.

There are two types of boxes:  Gtk::HBox and Gtk::VBox.  Horizontal
boxes put everything in a single horizontal line and vertical boxes put
everything in a single vertical line.  Two optional parameters can be
passed when the object is created.

   hbox = Gtk::HBox.new(homogeneous = false, spacing = nil)

   vbox = Gtk::VBox.new(homogeneous = false, spacing = nil)

Widgets can be packed into either the start or end of the free space of
a box.  The start and end locations depend on the type of box.  In an
hbox, the start is on the left of the free space area.  In a vbox, the
start is at the top of the free space area.

   Gtk::Box#pack_start button

   Gtk::Box#pack_end button2

The actual size of the box and widgets are calculated when they are made
visible.  The packing manager asks the toplevel window what size it
needs, and it asks its child widgets.  They in turn ask their children
until the lowest level widgets are reached.  As the requested 

The spacing between widgets in a box can be adjusted by passing the
desired spacing in pixels when the box is created.

sizes are returned to the parents, the parents make their own requests,
until finally the toplevel window tells the packing manager how big it
wants to be.  The packing manager or parent widgets might not honor a
request, in which case the widget must accept the size that is allocated
for it.

The programmer can request additional size, if necessary.

By default, each widget is only as large as necessary.  At times this
can result in unbalanced window layouts.  If you want every widget in a
box to be the same size, you can set the homegenous property to true
when the box is created.

++++++++++++++++++++++++++++++++++++++++++

#! /bin/usr/env ruby

require "gtk2"

Gtk.init

win = Gtk::Window.new
win.border_width = 10

# Create a vertical box to hold everything.
box = Gtk::VBox.new
win.add box

# Create a horizontal box.
# These widgets in this box have different widths.  If you want them
# to be the same width, add the argument "false" to Gtk::HBox.new.
hbox = Gtk::HBox.new
win.add hbox

# Create buttons and add them to the hbox.
b1 = Gtk::Button.new "_Left"
b1.signal_connect("clicked") { puts "Left" }
hbox.pack_start b1

b2 = Gtk::Button.new "_Center"
b2.signal_connect("clicked") { puts "Center" }
hbox.pack_start b2

b3 = Gtk::Button.new "_Right"
b3.signal_connect("clicked") { puts "Right" }
hbox.pack_start b3

# Create a vertidal box.
box = Gtk::VBox.new
win.add box

# Create buttons and add them to the vbox.
b4 = Gtk::Button.new "R_ed"
b4.signal_connect("clicked") { puts "Red" }
vbox.pack_start b4

b5 = Gtk::Button.new "_Green"
b5.signal_connect("clicked") { puts "Green" }
vbox.pack_start b5

b6 = Gtk::Button.new "_Blue"
b6.signal_connect("clicked") { puts "Blue" }
vbox.pack_start b6

win.signal_connect("destroy") {
   Gtk.main_quit
}

win.show_all

Gtk.main






More information about the clue-cert mailing list