Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Login


What Is In This Chapter


If you have worked with GTK+ in either its native C, or in its close relative C++ (gtkmm) environments, you may very well be aware of the two different ways graphic imaging and drawing is handled in GTK+. One, the older and original is the plain GTK approach, mostly utilizing the Gdk::Drawable class, which has gradually been replaced by the newer approach utilizing a third party Cairo 2D graphics library, though it has to be added, that since GTK version 2.8, Cairo is part of the GTK system.

Contents At the Glance:

1200-img01-gtk-backgrounds-s2.png Let's look at the images here to get an idea what is going to be the starting point of our study in this chapter.

Here on the left you see a few examples of plain original GTK approach, utilizing the Gdk::Drawable class. We manage the background image of the drawable widget. In one case the single stretchable image is used, in the other multiple tailed images appear as the background. In either case you are able to use a graphic pen/brush like feature to draw, as well as 'undo' your drawing by pressing delete key.

1200-setsrcrgb-s1.png1200-img02-ruby-cairo-is-comming-s2.png On either side of this paragraph you can see images generated by a few very simple Cairo sample programs, that a novice to Ruby GTK/Cairo programming may find in different 'non-Ruby' tutorials on the Internet, and that may seem to be very hard if not impossible to implement in Ruby. Yet, as you can see there is a way!

In this chapter I will attempt to highlight both of these two environments, point out how they cooperate, as well as discuss the circumstances when it looks that either one may be a better choice over the other, and even when due to some, as of yet, unexplained limitations of Ruby GTK/Cairo marriage we need to resort to some peculiar tricks not needed in straight C, or C++ (gtkmm) environments.

Most of those familiar with GTK+ 2D graphics know that Cairo 2D library is preferable over the original GTK solutions utilizing Gdk::Drawable. Also the emphasis in this chapter will be on Cairo, however, the two, especially in Ruby are tightly related, in fact some of what can be considered stand alone Cairo features in native C GTK+ are not available in Ruby. Most often when some native GTK/Cairo functionality is missing, one believes, we should be able to find another 'Ruby-GTK way' of accomplishing those things. As the matter of fact, at the beginning, when I started to learn how to manage 2D graphics in Ruby, I often studied examples native C GTK, only to discover that there were no Cairo or GTK primitives available in Ruby, when trying to port the examples from those tutorials in other programming languages to Ruby.

For some reason, not all nonessential parts of Cairo Graphics library are ported to Ruby in its entirety. Unfortunately, almost all reasonably complete Cairo tutorials to date, including the tutorials found on the official Cairo documentation site (Cairo Tutorial), begin explaining the library using these parts inexplicable and almost 'devilishly' missing from the Ruby incarnation of Cairo library. The good news is, that all the essential Cairo functionality in Ruby, nevertheless, is there. However, unless you are proficient in GTK/Cairo, you will have hard time ever coming to such a conclusion, never mind, getting sufficiently comfortable with Ruby graphics libraries for any serious GUI programming. It is, as if by omitting the simplest console related Cairo features used in all the Cairo tutorials, should keep many potential Ruby-graphic programmers away, not only from embracing the Cairo but also from using Ruby/GTK in general.

By expanding this Ruby/Gtk tutorial and, here, in particular attempting to convert all the examples from currently prominent Cairo Graphics tutorial wherever possible, I wish to brake through the 'devilish' barrier, preventing the flourishing of Ruby graphics program development and healthy growth of Ruby GUI community.

Last modified:2013/03/31 00:03:49
References:[tut-gtk2-dancr] [tut-gtk]