Points to note when using the Ruby/GTK (Compared to use the Gtk+ from C)

Hereafter, we discuss the difference between using the Gtk+ from C and using the Ruby/Gtk.


* Class Names

Gtk+ uses the methodology of the Object Oriented Programming, so 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 the Ruby. To avoid using up the namespace, all the classes in Gtk+ are handled under the Gtk module. And for each class in Gtk+, the name will be mapped with slight change; the class name substring of 'Gtk' will be mapped 'Gtk::' to cordinate with the ruby naming scheme. (ex: 'GtkButton' will be mapped to 'Gtk::Button')


* Name Constant

The name constants are also defined under the Gtk module. For each MACRO name in C, the name prefix 'GTK_' will need to change as 'Gtk::' in the Ruby/Gtk. (ex: GTK_WINDOW_TOPLEVEL will be mapped to 'Gtk::WINDOW_TOPLEVEL')


* Creating the Widgets

In Ruby/Gtk, you can create any widgets by calling 'new' class method that is defined in each widget classes.

On the other hand 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)

* Calling Method

The C is not a OOP language, all methods are called by ordinary function call.

When calling the method, you need to specify the pointer to the instance as first argument.

Furthermore, C has type for variable, but for data, arguments are needed to cast to proper type.

Calling instance method in C
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 functions gtk_conatier_*. But you need to cast it.

On the other hand in Ruby/gtk, it is neat like this;

Calling the instance method in Ruby/Gtk
window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL)

window.border_width = 10

* signal_connect

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 );

As you've seen before, the arguments are needed to cast.

Because the signal_connect is defined as iterator in Ruby/Gtk, the signal handler will be given by block paramater.

Notice: signal_connect is a typical example of the iterator that does not iterate.

As for the return value of the 'gtk_signal_connect', the tag (integer) will be returned for the handler identification.

This tag is used to detach the handler with 'gtk_signal_disconnect'. Currently, its counter part is not supplied by Ruby/Gtk.

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