User data in callback functions

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

try this one:

Juha Varkki wrote:

it’s the value that was last stored into it. This, of course, produces
for i in 0…5
button = Gtk::Button.new( “Button #” + i.to_s )
button.signal_connect( “clicked” ) { puts "clicked " + i.to_s }

button.signal_connect( “clicked” ) {|me| puts "clicked " + me.label.to_s
}

Hi,

Umm, no. The variable I need is not stored as a label or anything in
the widget. It’s totally separate data. That’s why I mentioned about
the user-data property in Gtk::Object, which I can’t use for some
reason.

– Juha Varkki

2006/10/7, Dobai-Pataky B. [email protected]:

I have a loop with object.signal_connect( “clicked” ) { puts variable
Gtk.init()
end

Any help is greatly appreciated.
ruby-gnome2-devel-en mailing list


ruby-gnome2-devel-en mailing list
[email protected]
ruby-gnome2-devel-en List Signup and Options


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


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


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

Hi,

In [email protected]
“[ruby-gnome2-devel-en] User data in callback functions” on Fri, 6 Oct
2006 23:59:29 +0300,
“Juha Varkki” [email protected] wrote:

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.

What about the following:

button.signal_connect( “clicked”, i ) do |widget, var|
puts "clicked " + var.to_s
end

end

window = Gtk::Window.new()
window.signal_connect( “destroy” ) { Gtk.main_quit }
window.add( vbox )
window.show_all()

Gtk.main()

Thanks,

kou


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

Ah! Thank you Kouhei! That’s exactly what I was looking for. I wonder
why that isn’t documented.

e$B$“$j$,$H$&$4$6$$$^$7$?!”$3$&$X$$$5$s!*e(B

– Juha Varkki

2006/10/7, Kouhei S. [email protected]:

really bad results.

window.signal_connect( “destroy” ) { Gtk.main_quit }
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


ruby-gnome2-devel-en mailing list
[email protected]
ruby-gnome2-devel-en List Signup and Options


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

I see. Ruby-Gnome2 definitely doesn’t have all the necessary functions
to do GTK+ programming in Ruby yet.

Is it planned to add (an optional) user data parameter to the
signal_connect function in Ruby-Gnome2? That’d make everything whole
lot of easier for everyone.

– Juha Varkki

2006/10/7, Dobai-Pataky B. [email protected]:

reason.

it’s the value that was last stored into it. This, of course, produces
for i in 0…5

The C version of GTK+ offers a chance to pass the user data as a

[email protected]
ruby-gnome2-devel-en mailing list
opinions on IT & business topics through brief surveys – and earn cash
opinions on IT & business topics through brief surveys – and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV


ruby-gnome2-devel-en mailing list
[email protected]
ruby-gnome2-devel-en List Signup and Options


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

Hi,

Ah! Thank you Kouhei! That’s exactly what I was looking for. I wonder
why that isn’t documented.

I updated the document.

end

    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()

Thanks,

kou


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