|
Page 2 of 7
Here is a typical "Hello World" style program for Ruby/GTK2.
This program is also available in the file
"gtk/sample/misc/helloworld.rb" in the ruby-gnome2 package.
#!/usr/bin/env ruby
=begin
helloworld.rb - Ruby/GTK first sample script.
Copyright (c) 2002,2003 Ruby-GNOME2 Project Team
This program is licenced under the same licence as Ruby-GNOME2.
$Id: helloworld.rb,v 1.4 2003/02/01 16:46:22 mutoh Exp $
=end
require 'gtk2'
Gtk.init
button = Gtk::Button.new("Hello World")
button.signal_connect("clicked") {
puts "Hello World"
}
window = Gtk::Window.new
window.signal_connect("delete_event") {
puts "delete event occurred"
#true
false
}
window.signal_connect("destroy") {
puts "destroy event occurred"
Gtk.main_quit
}
window.border_width = 10
window.add(button)
window.show_all
Gtk.main
Mechanism of signals and callbacks Before
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" will been catched.
end
GLib::Instantiatable#signal_connect needs 2 things:
- the name of the signal which will be catched;
- 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|
# ...
end
The widget which issues the signal will be therefore substituted by the parameter w.
Events
In addition to the above signal mechanism, events from the X Window Server are also reflected in GTK.
Here is an exhaustive list of catchable events:
- event
- button_press_event
- button_release_event
- scroll_event
- motion_notify_event
- delete_event
- destroy_event
- expose_event
- key_press_event
- key_release_event
- enter_notify_event
- leave_notify_event
- configure_event
- focus_in_event
- focus_out_event
- map_event
- unmap_event
- property_notify_event
- selection_clear_event
- selection_request_event
- selection_notify_event
- proximity_in_event
- proximity_out_event
- visibility_notify_event
- client_event
- no_expose_event
- window_state_event
You can connect some code to a specific X11 event exactly in the same way as we saw previously with GTK signals. Just provide one of the above mentioned "event name" instead of "signal name":
GLib::Instantiatable#signal_connect("event name") do
# Code to execute upon reception of "event name".
end
The callback code can accept 2 parameters, as follows:
GLib::Instantiatable#signal_connect("event name") do |w, e|
# ...
end
(in this case, w will refer the widget, and e the event).
Depending of the return value of the block code, GTK will decide if the event should be spread or not:
- if true, GTK will stop the event processing here;
- if false, GTK will continue to propagate the event [xxx explain more]
Also, GDK selections and drag-and-drop issue several events, but these are reflected
by GTK signals:
- selection_received
- selection_get
- drag_begin_event
- drag_end_event
- drag_data_delete
- drag_motion
- drag_drop
- drag_data_get
- drag_data_received
|