Hello Ruby-Gnome2 users and developers!
I’ve been making an application using Ruby-Gtk2 for the past few days
or so, and now I’ve run into a problem. I’m trying to pass user data
into a signal callback function, but I can’t figure out how to do
that.
I have a loop with object.signal_connect( “clicked” ) { puts variable
}. The variable' changes with every cycle of the loop. The problem is, my signal handler function's
variable’ seems to be the wrong one;
it’s the value that was last stored into it. This, of course, produces
really bad results.
Here’s a simple test case I made:
START
#!/usr/bin/ruby
require ‘gtk2’
Gtk.init()
vbox = Gtk::VBox.new()
for i in 0…5
button = Gtk::Button.new( “Button #” + i.to_s )
button.signal_connect( “clicked” ) { puts "clicked " + i.to_s }
vbox.pack_start( button, true, true, 0 )
end
window = Gtk::Window.new()
window.signal_connect( “destroy” ) { Gtk.main_quit }
window.add( vbox )
window.show_all()
Gtk.main()
END
Alternatively you can wget that from http://tohveli.net/example.rb
The C version of GTK+ offers a chance to pass the user data as a
parameter in signal_connect
(http://developer.gnome.org/doc/API/2.0/gobject/gobject-Signals.html#g-signal-connect),
but this doesn’t seem to be the case with Ruby-Gnome2.
Is there any way to make this work, to pass user data into a callback
function? It seems I can’t even use the user-data property in
Gtk::Object, as the documentation says it’s unaccessible from Ruby!
(http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3AObject#user-data%3A+GLib%3A%3APointer)
Any help is greatly appreciated.
Thanks,
– Juha Varkki
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net’s Techsay panel and you’ll get the chance to share
your
opinions on IT & business topics through brief surveys – and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV