Posts Tagged ‘connect’

Glade auto connect in JRuby

Sunday, November 15th, 2009

I have been playing a lot around with JRuby lately (since I was scared away from IronRuby by another lame Microsoft-patent). I have been really disappointed by the Gtk-implementation in Java – it really misses a lot of the good stuff from PHP-GTK – like the auto-connect functionality.

I decided I wanted to write my own Glade Auto Connect functionality to learn some more about JRuby – it turned out a bit harder than I expected. In the end I got it working with some of the basic widgets (GtkButton::Clicked, GtkWindow::destroy, GtkTreeViewSelection::changed and so on). I am adding support for more events as I continue my adventure into JRuby.

It works like this – when you want to spawn a new Glade-object, you do it through my function:

class MyWindow
  def initialize
    @glade = GladeAutoConnect("gladefile.glade", "window", self)
  end

  def on_window_destroy
    print "The window was closed!\n"
  end
end

No more connecting events manually – just write the event-names in the Glade-application directly. This functionality really shortens the code – which is what I like about Ruby: Less code to write. Right now it produces some eval-code though, which I plan to remove, when I learn more about how to dynamically make new classes in Ruby.

The code can bee seen here:
http://knjrbfw.kaspernj.org/jruby_gtk.rb