Obviously, you need to install Ruby-GNOME2 on your machine. Please consult one of our Install Guide. If your system is not yet covered, just download manually the latest package from SourceForge, decompress the tarball and read the README file for further instructions.
(More recently, installation process has been significantly improved - see the Installation documentation).
To begin our introduction to GTK, we'll 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' window = Gtk::Window.new window.show Gtk.main
Save this program in a file named base.rb, and call it with:
We will comment now each step of the program.
The first line links the Ruby/GTK2 library in the program.
The second line uses Gtk::Window.new to create a new GTK window with default parameters, as follows:
- size: 200x200
- style: Gtk::Window::TOPLEVEL
- style: Gtk::Window::POPUP (*** does not work ***)
- title: same as your program name (here: base.rb)
window = Gtk::Window.new
The third line calls Gtk::Window#show to display the window we just created:
The last line enters the GTK main processing loop:
Gtk.main is another call you will see in every Ruby/GTK2 application. When control reaches this point, GTK will sleep waiting for X events (such as button or key presses), timeouts, or file IO notifications to occur. In our simple example, however, events are ignored.
- Ideally all the features available in original C language GTK library should be found also in Ruby, however, where it does not make sense or is not absolutely necessary, they were omitted. For completion I'd like to point out to such omissions or any other relevant differences in the section, under the heading Caveat.
As you've seen window type (style) can be either Gtk::Window::TOPLEVEL or Gtk::Window::POPUP. While you have some control of the size and placement of top-level windows on the display, the top-level window is ultimately controlled by window manager. However, the pop-up window is different. They are normally used for things that are often not considered windows, such tooltips and menus. They are ignored by the window manager, and therefor have no decorations or border frame. In Ruby the second POPUP style is mostly ignored, hence, there really is only one style of window to be considered, namely the default Gtk::Window::TOPLEVEL.
This may be the reason that Window instance methods that allow querying and setting Window type/style (Gtk::Window::type, ...) at the time of this writing are not supported, though you will find them in the documentation.
window = Gtk::Window.new(type = Gtk::Window::TOPLEVEL) window.decorated = false