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.
Hmm. Assuming the GTK::Button class is type registered as well, why is the parameter not a hash in the first place?
Janne, GTK::Button class is type registered by GTK, rather than the bindings. I’m not sure if this makes a difference though.
But in this case that would affect calling super from inside GTK::Button, rather than affect a subclass.
I’m just basically confused as to why calling super is different from calling initialize; I thought super is just referring to the same method.