gotk3 / gotk3

Go bindings for GTK3
ISC License
2.12k stars 231 forks source link

[Project question] How to pass data to handlers connected via builder.ConnectSIgnals? #620

Open graynk opened 4 years ago

graynk commented 4 years ago

Hi!

So I know how to pass data if I connect the handler manually. Suppose I have a .glade file with a layout that has a button, a handler and a filechooser that I want to pass to the handler. Then I'd do it like this:

func showFileChooser(button *gtk.Button, filechooser *gtk.FileChooserDialog) {
    filechooser.Show()
}

builder, err := gtk.BuilderNewFromFile("main.glade")
obj := builder.getObject("filechooser")
fileChooser := obj.(*gtk.FileChooserDialog)
obj = builder.getObject("button")
button.Connect("clicked", showFileChooser, filechooser)

However, I would really like to use "User data" column in Glade to show which objects I want to pass to the handler. I know how to connect handlers that are described in .glade file:

signals := make(map[string]interface{})
signals["showFileChooser"] = showFileChooser
builder.ConnectSignals(signals)

But that way it does not pass the object specified in .glade file, all I get is an error that says too many closure args: have 2, max allowed 1. Is there a way to pass data when connecting signals like that? I know in C there's a function called g_object_set_qdata that sets userdata on an object using key, is there something like that in gotk?

MJacred commented 4 years ago

Right now only possible by using closures

application.Connect("activate", func() { onActivate(application) })

You can try extending builder.ConnectSignals() yourself, take a look in gotk3 at code where sync.RWMutex is used in maps.

ArthurFleischman commented 4 years ago

make it a TODO to the next release so...