Apr 20, 2008

wxRuby

In my random programming escapades, I've recently started using wxRuby. It is a port of the wxWidgets library to Ruby. wxWidgets is a cross-platform GUI library for C++ similar to MFC (Microsoft Foundation Classes) that has a much more object-oriented way of doing things than other libraries like GTK. It also uses native rendering, so that the widgets look like normal widgets. Compare this to Swing or GTK which draw their own widgets, the user interfaces look and feel different than the host OS.

EDIT(24/11/09): Looks like wxruby was added to the rubygems repo. The issue is that it crashes horribly on 64-bit Linux. I'm not sure why it does this but if you follow my installation instructions below you should be ok (I just tested this tonight and the compile went smoothly, and the test code I have worked fine).
EDIT: wxRuby works differently on Hardy. I don't think it's in the repositories (if it is, it has a weird name and I can't find it). You'll need to either get the binaries or the source, the source is more likely to work.
I'm not sure exactly which libraries it requires, since I already have a whole whack of them installed. Try ruby1.8-dev, build-essential, libwxbase2.8-dev, libwxgtk2.8-dev, wx2.8-headers, libgstreamer0.10-dev, and libgstreamer0.10-ruby. I don't know if all of these are required, and I don't know if this is all the ones that you'll need, I'm just looking at the list of packages that I have installed.
Here's how to get it installed (command-line way):
wget http://rubyforge.org/frs/download.php/63379/wxruby-2.0.1.tar.gz
tar -zxvf wxruby-2.0.1.tar.gz
cd wxruby-2.0.1
rake
sudo rake install

wxRuby is a fairly young port, especially compared to the Python ports of wxWidgets. This means that some functionality is not there, or the documentation is incomplete (or both). Fortunately though, this is Ruby and we can always go
puts myObj.methods.sort.join(", ")
and see all the methods that the object has.

The newness to the library aside, it is an excellent windowing library. With Ruby's rapid development speed and some nice WYSIWYG editors like DialogBlocks, you're sure to have a nice looking GUI up in no time.

Why use wxWidgets over other libraries? Why not GTK/qt/Swing/MFC/etc.? Here's my analysis:
  • MFC is out of the question if you want to develop for non-Windows platforms.

  • GTK is out of the question for non-Linux platforms. While GTK is ported to other platforms, it looks like garbage compared to natively drawn components and basically feels like it doesn't belong - just take a look at Pidgin. Also, in wxWidgets you can go
    @myWidget.set_size( Size.new(100, 100) )
    GTK doesn't have this luxury.

  • Swing's layout managers are a pain when you want to do absolute positioning. While Netbeans has a GUI editor, I find it to be a pain in the ass as the GUI never looks the same when you execute the program as it is in the GUI designer.

  • qt - No reason not to try this. I tried wxWidgets first. This might be another good option.

  • Tk. Oh Tk. Download aMSN and you will see why I don't want to use Tk.

The documentation issues aside (I usually find anything Ruby suffers from documentation problems, especially compared to PHP or C++), the library is a pleasure to use. Ruby's blocks make event handling a breeze, you just attach a block to an object and off you go. I'd recommend this to anybody who wants to make GUI applications with Ruby.

6 comments:

Anonymous said...

got wxruby running on ubuntu-8.04 ?

Rob Britton said...

Whoops, I haven't actually tried to use wxRuby since I posted that edit. I got it up and running now.

Anonymous said...

spent my day figuring out how to setup wxwidgets/swig/wxruby properly.
still compiling at the moment...

Rob Britton said...

Heh sorry to hear that...well that's computers for you (and especially open-source). Did my instructions work for you?

Anonymous said...

finally - got it working on 64bit. so had to add some -fPIC to cppflags as well as to ldflags in wxruby-1.9.8/rake/rakeconfigure.rb. now it works. phew...

Rob Britton said...

That's pretty strange. I'm running on 64-bit and it worked just fine.