Signal


Basic of signal

In Gtk, various evens such as ``a button was pressed'' or ``a window was redisplayed'' are notified to a widget via `signal'. One can define an event handler which invokes the action corresponding to each events. Generally speaking, we can regard Gtk programming as setting event handler. Signal management is so important that someone says ``Who controls the signal controls Gtk''.

A signal handler is defined as follows:

object.signal_connect('signal_name') {|*args|
  corresponding action
}

singnal_name is a string representing the kind of signal. The first option in args must be specified widget on which the signal is raise, though, the rest options depend on the type of signal.

`signal_name' is chosen from constants of each widget class (e.g., Gtk::Widget::SIGNAL_SHOW, its value is "show"). One can obtain signal constants being defined in each classes by gtkbrows.

According to the class hierarchy, a signal constant in a class is defined in also its sub classes. The following lists all signal constants in Gtk::Widget, which is placed on the top of the other widget classes. These signals are defined in all widget classes too.

Signals defined for Gtk::Window
< /tr>
name of signal arguments
destroy widget
show widget
hide widget
map widget
unmap widget
realize widget
unrealize widget
draw widget, area
draw_focus widget
draw_default widget
size_request widget, requisition
size_allocate widget, allocation
state_changed widget
install_accelerator widget, signal_name, key, modifiers
remove_accelerator widget, signal_name
event widget, event
button_press_event widget, event
button_release_event widget, event
motion_notify_event widget, event
delete_event widget, event
destroy_event widget, event
expose_event widget, event
key_press_event widget, event
key_release_event widget, event
enter_notify_event widget, event
leave_notify_event widget, event
configure_event widget, event
focus_in_event widget, event
focus_out_event widget, event
map_event widget, event
unmap_event widget, event
property_notify_event widget, event
selection_clear_event widget, event
selection_request_event widget, event
selection_notify_event widget, event
drop_event widget, event
drag_begin_event widget, event
other_event widget, event

Related methods

All methods relating to signal are defined in Gtk::Object.

singal_connect(`singal_name') { block }

singal_connect connects a given block as signal handler of `signal_name' to the reciever. `signal_name' is defined as constant which has name started by `SIGNAL_'. Both of the following means same:

button.signal_connect('clicked') {...}
button.signal_connect(Gtk::Button::SIGNAL_CLICKED) {...}

One can set more than one signal to an object or an signal. In such cases, the callback will be executed sequentially in the other of being connection. The following will cause displaying "Hello World" if the button is clicked.

button.signal_connect('clicked') {
  print "Hello "
}

button.signal_connect('clicked') {
  print "World\n"
}

Note that signal_connect returns a unique id number for a handler.

signal_connect_after(signal) { block }

Similar to `gtk_signal_connect' except the signal handler is connected in the "after" slot. This allows a signal handler to be guaranteed to run after other signal handlers connected to the same signal on the same object and after the class function associated with the signal.

signal_emit_stop('signal_name')

Stop the emission of the signal `signal_name' on the object. Attempting to stop the emission of a signal that isn't being emitted does nothing.

Next
[TOP]
Written by akaishi@ruby.freak.ne.jp