I try to write a template connect for
button1.connect "clicked":
echo "button1 clicked"
button2.connect "clicked":
echo "button2 clicked"
mainquit()
but got an error:
Error: cannot use symbol of kind 'var' as a 'param'
How to fix that?
import gtk3, glib, gobject
type CB = proc (widget: Widget, data: Gpointer)
template connect(w: Widget, ev: expr, cb: stmt) =
block:
let x: CB = (proc(w: Widget, d: Gpointer) = cb)
discard w.gSignalConnect(ev, cast[Gcallback](x), nil)
gtk3.initWithArgv()
var
w = newWindow()
button1 = newButton "test 1"
button2 = newButton "test 2"
box = newBox(Orientation.VERTICAL, 0)
box.packStart(button1, false, true, 0)
box.packStart(button2, false, true, 0)
w.add box
button1.connect("clicked"):
echo "button1 clicked"
button2.connect "clicked":
echo "button2 clicked"
mainquit()
w.showAll
gtk3.main()
I try to write a template connect for
Great! I have some trouble with that myself... Currently I can not investigate that, I just try to fix the application 10 example, hope that I can upload it soon.
For callbacks we may look at the work of Mr jdm from http://forum.nim-lang.org/t/1901#11815 Had no time to look at that myself, sorry.
I think the problem is here:
var
w = newWindow()
button1 = newButton "test 1"
button2 = newButton "test 2"
box = newBox(Orientation.VERTICAL, 0)
button 1 and 2 or maybe w should be declared using let
@ Arrr
You may be right -- but generally var works fine also:
I think the problem is here:
button 1 and 2 or maybe w should be declared using let
thanks, but
same error message
my question may not clear.
Actually, my question is how yo write the template "CONNECTX"
b3.CONNECTX "clicked":
echo "hello"
echo "b3 clicked"
mainquit()
import gtk3, glib, gobject
type CB = proc (widget: Widget, data: Gpointer)
template connect(w: Widget, cmd: expr, f: Gcallback) =
discard w.gSignalConnect(cmd, f, nil)
template connect(w: Widget, cmd: expr, f: CB) =
discard w.gSignalConnect(cmd, cast[Gcallback](f), nil)
template connect(w: Widget, cmd: expr, f: CB, data: expr) =
discard w.gSignalConnect(cmd, cast[Gcallback](f), cast[Gpointer](data))
proc click1(widget: Widget, data: Gpointer) =
echo "clicked"
proc click2(widget: Widget, data: Gpointer) =
echo "data = ", cast[seq[int]](data)
echo "clicked"
proc quit(widget: Widget, data: Gpointer) =
echo "quit clicked"
mainQuit()
gtk3.initWithArgv()
var
window = newWindow()
b1 = newButton "test 1"
b2 = newButton "test 2"
b3 = newButton "quit"
box = newBox(Orientation.VERTICAL, 0)
box.packStart(b1, false, true, 0)
box.packStart(b2, false, true, 0)
box.packStart(b3, false, true, 0)
window.add(box)
var data = @[1, 2, 3]
# signal connect
window.connect "destroy", mainQuit
b1.connect "clicked", click2, data
b2.connect "clicked", quit
# how yo write this template "CONNECTX"
# b3.CONNECTX "clicked":
# echo "hello"
# echo "b3 clicked"
# mainquit()
window.showAll
gtk3.main()
hope that I can upload it soon.
thanks!
it seems to work now...
import gtk3, glib, gobject
type CB = proc (widget: Widget, data: Gpointer)
template connect(w: Widget, cmd: expr, f: CB, data: expr) =
discard w.gSignalConnect(cmd, cast[Gcallback](f), cast[Gpointer](data))
template connect(w: Widget, cmd: expr, p: stmt) =
discard w.gSignalConnect(cmd, cast[Gcallback](proc() = p), nil)
proc click1(widget: Widget, data: Gpointer) =
echo "b0 clicked"
proc click2(widget: Widget, data: Gpointer) =
echo "data = ", cast[seq[int]](data)
echo "b1 clicked"
proc quit1(widget: Widget, data: Gpointer) =
echo "quit clicked"
echo "bye"
mainQuit()
gtk3.initWithArgv()
let
window = newWindow()
b0 = newButton "b 0"
b1 = newButton "b 1"
b2 = newButton "b 2"
b3 = newButton "quit"
box = newBox(Orientation.VERTICAL, 0)
box.packStart(b0, false, true, 0)
box.packStart(b1, false, true, 0)
box.packStart(b2, false, true, 0)
box.packStart(b3, false, true, 0)
window.border_width = 10
window.add box
let data = @[1, 2, 3]
# signal connect
window.connect "destroy": mainquit()
b0.connect "clicked", click1, nil
b1.connect "clicked", click2, data
b3.connect "clicked", quit1, nil
b2.connect "clicked":
echo "hello"
echo "b2 clicked"
window.showAll
gtk3.main()
I believe your callback procs need to use the pragma {.nimcall.} because otherwise they may not work if they are defined in a closure context (for example as nested procs in a app() proc).
EDIT: I was told this by @araq in a similar case while implementing event callbacks for wxnim. I could not notice this making any difference for my system though.
I believe your callback procs need to use the pragma {.nimcall.}
may not work
Thanks, would you mind show me the correct code.
And give me an example for not work
Just adding the pragma to the procs which are called by the callback like:
proc click1(widget: Widget, data: Gpointer) {.nimcall.} =
echo "b0 clicked"
I can't show you what does not work (see my edit in previous post). We need to ask @araq I guess!
Hi OderWat.
Thanks.