Posts Tagged ‘events’

HowTo: Make a simple GtkWindow with IronRuby, GtkSharp, Glade and events (not Ruby-Gtk!)

Sunday, November 8th, 2009

To make this a lot easier for the ones of us using Ubuntu, I added IronRuby to my Ubuntu-repository. Read more about how to set it up here:

http://wiki.kaspernj.org/index.php/KnjUbuntuRepository

Afterwards do a: “sudo aptitude install ironruby glade libgtk2.0-cil libglade2.0-cil”.

While you are at it, install “glade” (”glade-3″ if you are using Jaunty, I think). Make a new project folder – I have called mine “IronRubyTest”. Make a small Glade-file (not GtkBuilder) with a label and a button. Remember the names – I called mine “window1″, “label1″ and “button1″.

To load the Glade-file, we have to import some modules first. To get the names of the modules, type “gacutil -l | grep gtk” and “gacutil -l | grep glade”. Type this in the beginning of your Ruby-code:

require "gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f"
require "glade-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f"

Be aware you properly have to use the lines, that you got from the “gacutil grep”-command.Its possible that yours can be different. Then type this afterwards in the Ruby-file:

include Gtk
include Glade


We are now ready to begin coding our class:

class MainWindow
  def initialize
    Application.init

    tha_glade = Glade::XML.new("test.glade", "window1", nil)
  end
end

Notice the “Application.init” – I guess this is to init the Gtk-stuff, like you would do in other languages when you are using Gtk.

The GladeXML-object is stored in the “tha_glade”-variable. “window1″ is the name of the Window, which can be set through Glade.

...
    tha_window = tha_glade.GetWidget("window1")
    tha_window.ShowAll
    tha_window.destroyed{|sender, e|
       Application.Quit
    }

    tha_button = tha_glade.GetWidget("button1")
    tha_button.clicked{|sender, e|
       tha_window.destroy
    }
...

We store the GtkWindow in the “tha_window” variable. Afterwards we attach some code to the destroy-event – we actually make the application kill itself, when we close the Window.

Further more we store the GtkButton in the “tha_button” variable. Afterwards we make the button close the window – which calls the other event and kills the application.

main_window = MainWindow.new
Application.run

The code here is placed at the very end of the file – not included in any function or class. This start our MainWindow-class, loads the window and everything and starts a loop, so our application doesnt end itself after loading the window and all.

The entire code can be seen here:

require "gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f"
require "glade-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f"

include Gtk
include Glade

class MainWindow
	def initialize
		Application.init

 		tha_glade = Glade::XML.new("test.glade", "window", nil)

		tha_window = tha_glade.GetWidget("window")
		tha_window.ShowAll
		tha_window.destroyed{|sender, e|
			Application.quit
		}

		tha_button = tha_glade.GetWidget("button1")
		tha_button.clicked{|sender, e|
			tha_window.destroy
		}
	end

	def on_button1_clicked(object, event)
		print "hmm\n"
	end
end

main_window = MainWindow.new
Application.run