Hey folks,
inspired by some topics in the forum i now test the gui-module for IUP: "niup" from "dariolah".
My question is, how can i get access to an object-variable, if a button is pressed and the "action" is fired. Does anyone know a solution for this which is not with a global variable?
Here's a code snippet, which does not function - but perhaps it could explain what i meen:
type
Mydata = ref object
name*: string
...snip...
proc cb_btn(self: PIhanlde, md: Mydata): cint {.cdecl. } =
echo md.name
proc initIUP*() =
var md = Mydata(name: "beckx")
...snip...
var btn = Button("Press me")
btn.action = cb_btn(md)
...snip...
How can i get access to my md-Variable in the procedure cb_btn... My solution does not work :(
Can anyone pls help me?
txh and bests
beckx
You'd have to preserve md until the button is really pressed, then pass it to btn.action.
Also see this thread.
First of all, cb_btn(self: PIhanlde, md: Mydata) the procedure has two arguments but in the invocation cb_btn(md) only has one. So that's something you may check out.
Secondly, what you are doing in the btn.action = cb_btn(md) line is invoking the function and the result of the function is stored in the action field of the btn object.
Something you could do depending on the definition of the action field is something like:
var md = Mydata(name: "beckx")
# ... some code
var btn = Button("Press me")
btn.action = proc() = #this signature depends on the proc type of the action field
cb_btn(arg1, md)
# at this point the action isn't executed yet, the callback is only stored
btn.action() #cb_btn proc called
of course you have to adapt it to the proc type of action. I hope this is useful.