Type registering your own widgets in Ruby/GTK+
I ran into a slight problem when I tried to create a type registered subclass in Ruby/GTK+. This is done by adding type_register in your class definition. This worked fine until I tried to pass arguments to the super class constructor through the super() call.
In my previous example, I passed the arguments like usual to the super call. It turns out that when you have registered your class with the GObject type system (which I didn’t do in that example) it overloads the super() call and you need to pass the arguments as a hash instead.
Here is an example subclassing Gtk::Button that sets the button up defaulting to underline mnemonics.
require 'gtk2'
class MyButton < Gtk::Button
type_register
def initialize(label)
super({:label => label, :use_underline => true})
end
def signal_do_clicked(*args)
puts “Clicked”
end
end
w = Gtk::Window.new
b = MyButton.new(”My _Button”)
w.add(b)
w.signal_connect(:delete_event) do
Gtk.main_quit
end
w.show_all
Gtk.main
Kudos to Kou for showing how to do it.