11.11. Toolbar

Toolbars are usually used to group some number of widgets in order to simplify customization of thier look and layout. Typically a toolbar consists of buttons with icons, labels, and tooltips, but any other widget can also be put inside a toolbar. Finally, items can be arranged horizontally or vertically and buttons can be displayed with icons, labels, or both.

Creating a toolbar is (as one may already suspect) done with the following method:

Gtk::Toolbar.new

After creating a toolbar one can append, prepend, and insert items (that means simple text strings) or elements (that means any widget types) into the toolbar. To describe an item we need a label text, a tooltip text, a private tooltip text, an icon for the button and a callback method for it. For example, to append or prepend an item you may use the following methods:

CODE
CODE

If you want to use Gtk::Toolbar#insert_item, the only additional parameter which must be specified is the position in which the item should be inserted, thus:

CODE

To simplify adding spaces between toolbar items, you may use the following methods:

CODE
CODE
CODE

If it's required, the orientation of a toolbar and its style can be changed "on the fly" using the following methods:

CODE
CODE
CODE

Where orientation is one of Gtk::ORIENTATION_HORIZONTAL or Gtk::ORIENTATION_VERTICAL. They style is used to set the appearance of the toolbar items by using one of Gtk::Toolbar::ICONS, Gtk::Toolbar::TEXT, or Gtk::Toolbar::BOTH.

To show some other things that can be done with a toolbar, let's take the following program (we'll interrupt the listing with some additional explanations):

# This method is connected to the close button or
# closing the window from the WM
def delete_event( widget, event )
   Gtk.main_quit
end

The above beginning seems familiar to you if it's not your first Ruby-GNOME2 program. There is one additional thing though, we include a nice XPM picture to serve as an icon for all the buttons.

CODE
CODE
CODE
CODE
CODE
CODE

In fact not all of the above widgets are needed here, but to make things clearer I put them all together.

CODE
CODE
CODE
CODE
CODE

The above are just two callback methods that will be called when one of the buttons on the toolbar is pressed. You should already be familiar with things like this if you've already used toggle buttons (and radio buttons).

CODE
CODE
CODE

The above should be similar to any other Ruby-GNOME2 application. Just initialization of GTK, creating the window, etc. There is only one thing that probably needs some explanation: a handle box. A handle box is just another box that can be used to pack widgets in to. The difference between it and typical boxes is that it can detached from a parent window (or, in fact, the handle box remains in the parent, but is reduced to a very small rectangle, while all of its contents are repaerented to a new freely floating window). It is usually nice to have a detachable toolbar, so these two widgets occur together quite often.

CODE
CODE
CODE

Well, what we do above is just a straightforward initialization of the toolbar widget.

CODE
CODE
CODE

In the above code you see the simplest case: addind a button to the toolbar. Just before appending a new item, we have to construct an image widget to serve as an icon for this item; this step will have ot be repeated for each new item. Just after the item we also add a space, so the following items will not touch each other.


Prev Next