Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Back  Login

tut-gtk2-mnstbs-tb - History

  • Added words are showed as here.
  • Removed words are showed as here.
= Menus and Toolbars
{{link "tut-gtk2-mnub", "tut-gtk2-mnstbs", "tut-gtk", "tut-gtk2-mnstbs-tbi"}}

== Toolbars

A Gtk::Toolbar is a type of container that holds a number of widgets in a horizontal or vertical row. It is meamt to allow easier customization of large number of widgets with very little trouble. Typically toolbars hold tool buttons that can display an image along with a text string. However, toolbars are actually able to hold any type of widget.


In the example program here, a simple toolbar is created that that shows five tool items in a horizontal row. Each toolbar item displays an icon and a label that describes the purpose of the item. The toolbar is also set to display an arrow that will that will provide access to toolbar items that do not fit in the menu.

In this example, a toolbar is used to provide cut, copy paste, and select-all functionality to the Gtk::Entry widget. The main program line (body) creates the toolbar, packing it above the entry widget. It then calls "create_toolbar", which populates the toolbar with tool items and connects the needed signals.

Again, there is a minor problem here, namely the vertical ((*separator*)) is not yet supported in the current Ryby/GNOME2 (2-0.17.0-rc1) release. Nor does the instance method ((*show_arrow=true*)) yield a desired behaviour!? These are only problems in Ruby implementation, and work in C GTK+ implementation. You can see the both the separator and the arrow features on the image above, as displayed by the toolbars.c - C GTK+ version of our example program.


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

# Create a toolbar with Cut, Copy, Paste and Select All
# toolbar items.
def create_toolbar(tb, ent)
   cut =
   copy =
   paste =
   selectall =
#  separator =


   # NOTE: tool-tips are only part of the deprecated interface ?!
   tb.insert(0, cut, "Cut out the text")
   tb.insert(1, copy)
   tb.insert(2, paste)
#  tb.insert(3, separator)
   tb.insert(4, selectall)

   cut.signal_connect('clicked')       { ent.cut_clipboard }
   copy.signal_connect('clicked')      { ent.copy_clipboard }
   paste.signal_connect('clicked')     { ent.paste_clipboard }
   selectall.signal_connect('clicked') { select_all(ent) }

# Select all of the text in the GtkEditable.
def select_all(edit)
   edit.select_region(0, -1)

window =
window.resizable = true
window.title = "Toolbars"
window.border_width = 10
window.signal_connect('delete_event') { Gtk.main_quit }
window.set_size_request(325, -1)

entry   =
toolbar =
create_toolbar(toolbar, entry)
vbox =, 5)


New toolbars are created with This creates an empty toolbar. We created our toolbar before calling our own method create_toolbar(toolbar, entry), which populates the toolbar.

Gtk::Toolbar class provides a number of properties for customizing how it appears and interacts with the user including the orientation, button style, and the ability to give access to items that do not fit in the toolbar. The one property managing instance method worth mentioning here is the Gtk::Toolbar#toolbar_style, because it requires you to be aware of the appropriate constants (((<GtkToolbarStyle|Gtk::Toolbar#GtkToolbarStyle>))). Also note the Gtk::Toolbar#show_arrow method

   tb.show_arrow = true

Just to remind you that we have included a method ((*select_region*)) defined in Gtk::Editable modulemodule.

    The Gtk::Editable is a module for widgets for editing text, such as Gtk::Entry. The editable class contains methods for generically manipulating an editable widget, a large number of action signals used for key bindings, and several signals that an application can connect to modify the behaviour of a widget.
Last modified:2022/01/25 21:00:34