Ruby/Gtk Tutorial | ||
---|---|---|
<<< Previous | Introduction | Next >>> |
Hereafter, we discuss the difference between using Gtk+ from C and using Ruby/Gtk
Gtk+ is an Object Oriented toolkit. It has a object hierarchy with the GtkObject as its top. If you use the Gtk+ from C, you rarely need to refer to the class name. But in the widget_class statement in 'rc' file, you will see the class name.
In Ruby/Gtk, the class defined in Gtk+ is mapped to the class of Ruby. To avoid using up the namespace, all the classes in Gtk+ are handled under the Gtk module. For each class name, the the 'Gtk' prefix will be mapped 'Gtk::' to cordinate with the Ruby naming scheme. (ex: GtkButton->Gtk::Button).
The name constants are also defined under the Gtk module. For each MACRO name in C, the prefix 'GTK_' is replaced by'Gtk::'. (ex: GTK_WINDOW_TOPLEVEL->Gtk::WINDOW_TOPLEVEL).
In Ruby/Gtk, you can create any widgets by calling 'new' class method that is defined in each widget classes. In C, you need to memorize and use the constructor function names for each widget class.
Widget creation in C.
GtkWidget *window; window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
.Widget creation in Ruby/Gtk.
window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL) |
C is not an OO language, all methods are called by ordinary function calls. When calling the method, you need to specify the pointer to the instance as first argument.
Furthermore, because C is a typed language, you have to cast arguments to the proper type:
GtkWidget *window; window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_container_border_width (GTK_CONTAINER (window), 10); |
In the example above, GTK_CONTAINER is the type casting macro. The class GtkWindow is derived from GtkContainer, so you can use the functions gtk_conatier_*. But you need to cast it.
Here is the the equivalent in Ruby/Gtk.
window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL) window.border_width = 10 |
In C, you can connect the signal on the objects by the function gtk_signal_connect.
gint gtk_signal_connect( GtkObject *object, gchar *name, GtkSignalFunc func, gpointer func_data ); |
These are the arguments.
The object on which the signal is connected.
A string representing the singal.
A pointer to the signal handler.
Finally, an argument for the signal handler.
In Ruby/Gtk, the signal_connect is defined as an iterator. Therefore, the signal handler must be given as block paramater.
As for the return value of gtk_signal_connect , the tag (integer) will be returned for the handler identification. This tag can be used to detach the handler with gtk_signal_disconnect. Currently, this counter part is not supplied by Ruby/Gtk.
<<< Previous | Home | Next >>> |
About Ruby/Gtk | Up | Getting Started |