Chapter 3. Getting Started

From this point on, I make a few assumptions which are:

Note to Windows users: I know that Ruby-Gnome2 will run under Cygwin. I am currently testing to see if it can be compilled nativly on Windows. I know GTK+ 2.0 can, so I assume that Ruby-GNOME2 will (just a guess at this juncture though) See my installing GTK+ 2.0 on Windows section in the appendix for instructions.

Onwards to the fun stuff!! To begin our introduction to Ruby-GNOME2, we will start with the simplest program possible. This program will create a 200x200 pixel window and has no way of exiting except to be killed by using the shell.



require 'gtk2'

Gtk.init
window = Gtk::Window.new(Gtk::Window::TOPLEVEL)
window.show
Gtk.main

Save the program as base.rb. You can then run it by typing in 'ruby base.rb' (without the quotes) at the command line. You should now see a lovely little window in front of you. You will have to terminate the program from the shell.

Lets go ahead and step through the above program.

require 'gtk2'

Loads the Ruby-GNOME2 libraries.

Gtk.init

This line must be present in all Ruby-GNOME2 applications. It calls the init method in Gtk, which in the API level of things, sets up a few things for us such as the default visual and color map and then proceeds to call gdk_init (in GTK/GDK). This method initalizes the library for use, and sets up the default signal handlers.

The next two lines of code create and display a window.

window = Gtk::Window.new(Gtk::Window::TOPLEVEL)
window.show

The Gtk::Window::TOPLEVEL argument specifies that we want the window to undergo window manager decoration and placement. Rather than create a window of 0x0 pixels in size, a window without children is set to 200x200 by default so you can still manipulate it. If you forget to add the argument, it defaults to a toplevel window.

The window.show line lets GTK know that we are done setting the attributes of the widget, and that it can now display it.

The last line enters the GTK main processing loop.

Gtk.main

Gtk.main is another call you will see in every GTK application. When control reaches this point, GTK will sleep, waiting for X events (such as a button or key press), timeouts, or file IO notifications to occur. In our simple example, however, events are ignored.


Prev Next