Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Login


Mechanism of signals and callbacks

Before looking at the "Hello World" program in details, we should study a bit how GTK handles signals and callbacks.

In the same way as your system delivers a signal to processes upon shutdown *1, GTK sends a signal to the main loop (Gtk.main) when a special event occured. The main loop will therefore call back the appropriate function of the widget.

Until a signal is received, the main loop will sleep.

In order to tell a GTK widget that it has to catch a specific signal, and then execute appropriate code, we need to set a signal handler.

This can be done with the GLib::Instantiatable#signal_connect method, which is part of the Ruby/GLib library (do not forget that GTK is built on GLib):

GLib::Instantiatable#signal_connect("signal name") do
    # Code to execute when "signal name" is caught.

GLib::Instantiatable#signal_connect needs 2 things:

  • the name of the signal which will be caught;
  • a block code that will be executed upon reception of the given signal.

The block code can take an optional parameter as follows:

GLib::Instantiatable#signal_connect("signal name") do |w|
    # ...

The widget which issues the signal will be therefore substituted by the parameter w.

*1Note that GTK signals are not related in any way with UNIX signals. The GTK toolkit is independent of the underlying system.

Last modified:2009/01/08 10:31:52