



(4 ratings)
Class Names
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).
Constant Names
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).
Creating the Widgets
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; |
.Widget creation in Ruby/Gtk.
window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL) |
Calling Methods
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; |
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) |
signal_connect
In C, you can connect the signal on the objects by the function gtk_signal_connect.
gint gtk_signal_connect( GtkObject *object, |
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.
20 Random Tutorials from the same category :
Hashes In RUBY
Example: Addressbook RUBY
Sorting the addressbook RUBY
Difference of Gtk+ and Ruby/Gtk
What can ruby arrays do?
Iterators In RUBY
The Scalability of Ruby
Progress Bars with GD2 and Ruby
Arrays In RUBY
Classes and methods in RUBY
Implementing AddressBook In RUBY
More features IN RUBY
Printing the addressbook RUBY
Writing iterators In RUBY













