I have posted days ago https://forum.nim-lang.org/t/4522 in which I try to bind xcgui which is a free 32bits dll for windows
Now I try to bind FLTK(http://www.fltk.org). I can compile FLTK, test, examples and fluid with my MSYS2+MingW64 on Win7 64 bits I know there is already https://github.com/Skrylar/nfltk, but so bad, the test.nim can not be compiled, my msys2 halted for hours on lines
Hint: nimwidget [Processing]
CC: fltk_test
CC: fltk_enumerations
However I am not trying to translated the official FLTK C header by myself again. C's pointer are always beats me, and I found a nice FLTK binding( and tons of examples) for freebasic by D.J.Peters on https://www.freebasic.net/forum/viewtopic.php?f=14&t=24547 In the work of D.J.Peters, there are prebuilt
fltk-c-1.3.3-32.dll
fltk-c-1.3.3-64.dll
libfltk-c-1.3.3-32-arm.so
libfltk-c-1.3.3-32-syslib.so
libfltk-c-1.3.3-32.so
libfltk-c-1.3.3-64-syslib.so
libfltk-c-1.3.3-64.so
so I just mimicked and got the following code
import winim # for the L""
type
long = int16
Fl_Widget = int16
Fl_Window = int16
Fl_Button = int16
#~ Fl_Callback = int16
const
fltk* = "fltk-c-1.3.3-64.dll"
type
Ihandle = object
PIhandle* = ptr Ihandle
Icallback* = proc (arg: PIhandle): cint {.cdecl.}
Fl_Callback* = proc(widget: ptr Fl_Widget, pData: ptr any)
proc Fl_WindowNew(byval: long, h: long, title:cstring): ptr Fl_Window{.
cdecl, importc: "Fl_WindowNew", dynlib: fltk, discardable.}
proc Fl_ButtonNew(x: long, y: long, w: long, h: long, label: cstring) : ptr Fl_Button{.
cdecl, importc: "Fl_ButtonNew", dynlib: fltk, discardable.}
proc Fl_WidgetSetCallback(wgt: ptr Fl_Widget, cb:Fl_Callback){.
cdecl, importc: "Fl_WidgetSetCallback", dynlib: fltk, discardable.}
proc Fl_WindowShow(win: ptr Fl_Window){.
cdecl, importc: "Fl_WindowShow", dynlib: fltk, discardable.}
proc Fl_Run(): long{.
cdecl, importc: "Fl_Run", dynlib: fltk, discardable.}
proc Fl_WidgetCopyLabel(wgt: ptr Fl_Widget; caption: ptr WCHAR){.
cdecl, importc: "Fl_WidgetCopyLabel", dynlib: fltk, discardable.}
proc ButtonClick (button: ptr FL_WIDGET, arg: ptr any):cint {.cdecl.} =
Fl_WidgetCopyLabel(button, "do it again")
var Win = Fl_WindowNew(320, 200, "What a shiny Window :-)")
var Btn = Fl_ButtonNew(10, 10, 120, 32, "click me")
#~ Fl_WidgetSetCallback(Btn, ButtonClick)
#~ Fl_WidgetSetCallback(cast[ptr Fl_Widget](Btn), (ButtonClick))
#~ Fl_WidgetSetCallback(cast[ptr Fl_Widget](Btn), cast[Fl_Callback](ButtonClick))
Fl_WindowShow(Win)
Fl_Run()
this code can run without the Fl_WidgetSetCallback line.
If I use
Fl_WidgetSetCallback(Btn, ButtonClick)
I get
a.nim(45, 21) Error: type mismatch: got <ptr Fl_Button, proc (button: ptr Fl_Widget, arg: ptr GenericParam): cint{.cdecl.}>
but expected one of:
proc Fl_WidgetSetCallback(wgt: ptr Fl_Widget; cb: Fl_Callback)
first type mismatch at position: 2
required type: Fl_Callback
but expression 'ButtonClick' is of type: proc (button: ptr Fl_Widget, arg: ptr GenericParam): cint{.cdecl.}
expression: Fl_WidgetSetCallback(Btn, ButtonClick)
elif I use
Fl_WidgetSetCallback(cast[ptr Fl_Widget](Btn), (ButtonClick))
I get
a.nim(46, 21) Error: type mismatch: got <ptr Fl_Widget, proc (button: ptr Fl_Widget, arg: ptr GenericParam): cint{.cdecl.}>
but expected one of:
proc Fl_WidgetSetCallback(wgt: ptr Fl_Widget; cb: Fl_Callback)
first type mismatch at position: 2
required type: Fl_Callback
but expression 'ButtonClick' is of type: proc (button: ptr Fl_Widget, arg: ptr GenericParam): cint{.cdecl.}
expression: Fl_WidgetSetCallback(cast[ptr Fl_Widget](Btn), ButtonClick)
elif I use
Fl_WidgetSetCallback(cast[ptr Fl_Widget](Btn), cast[Fl_Callback](ButtonClick))
I get
a.nim(47, 53) Error: cannot cast to a non concrete type: 'Fl_Callback'
Please, can anyone give clear help? Thanks
Fl_Callback* = proc(widget: ptr Fl_Widget, pData: ptr any)
# ...
proc ButtonClick (button: ptr FL_WIDGET, arg: ptr any):**cint {.cdecl.}** =
In other words, Fl_Callback is not cdecl and its return type is void while ButtonClick is cdecl and its return type is cint.
Let me ignore the data type first ;) I hope there is a VisualBasic-Freebasic-C-Nim data type comparasion sheet
type
Fl_Callback* = proc(widget: ptr Fl_Widget, pData: ptr any):cint {.cdecl.}
proc ButtonClick (button: ptr FL_WIDGET, arg: ptr any):cint {.cdecl.} =
Fl_WidgetCopyLabel(button, "do it again")
Fl_WidgetSetCallback(Btn, ButtonClick)
says
a.nim(45, 27) template/generic instantiation of `ButtonClick` from here
a.nim(40, 42) Error: cannot instantiate: 'arg:type'