The C code
int setCallback(Fl_Widget* wgt, Fl_Callback *cb, void *pdata=NULL, int num=1);
can be translated into
proc setCallback*(wgt: ptr Fl_Widget; cb: ptr Fl_Callback;
pdata: pointer = nil; num: cint = 1): cint
Instead of
setCallback(wgt, cb, nil, 2)
I want to call the function as
setCallback(wgt, cb, 0, 2)
I have tried use
converter toNil(x: int=0): type(nil) = nil
but
Error: invalid type: 'nil' in this context: 'proc (x: int): nil' for proc
so, is it possible to write a convertor which actually convert number zero only to nil? thanks
In Nim you can define procs with same name but different parameter list, so this compiles:
type
Fl_Widget = int
Fl_Callback = int
proc setCallback*(wgt: ptr Fl_Widget; cb: ptr Fl_Callback;
pdata: pointer = nil; num: cint = 1): cint = discard
proc setCallback*(wgt: ptr Fl_Widget; cb: ptr Fl_Callback;
pdata: int = 0; num: cint = 1): cint =
var hhh: pointer = cast[pointer](pdata)
#setCallback(wgt, cb, pointer(pdata), num)
setCallback(wgt, cb, hhh, num)
var wgt: ptr Fl_Widget
var cb: ptr Fl_Callback
discard setCallback(wgt, cb, nil, 2)
discard setCallback(wgt, cb, 0, 2)
The problem in this case may be that size of cint is not equal to size of pointer. You may use int as parameter type, as size of int is size of pointer in Nim. Direct type conversion with pointer(mycint) seems not to work, so I used a cast with additional variable. You may test it, maybe you have to use a when statement to decide if size(cint) is smaller than size(pointer) and execute different code. Note that your topmost "C" code seems to be C++, as C generally has no defaults for parameters.
hi, Stefan_Salewski You find the lazy bone in me exactly, thank you for the explaination. Now the original Code( in FreeBASIC )
#include once "fltk-c. bi"
sub QuitCB cdecl (byval self as Fl_Widget ptr,byval userdata as any ptr)
flMessageTitle("QuitCB called !")
if flChoice("Do you really want to exit ?","no","yes") then
Fl_WindowHide Fl_WidgetWindow(self)
end if
end sub
'
' main
'
var win = Fl_WindowNew(640,480)
var bar = Fl_Menu_BarNew(0,0,Fl_WidgetGetW(win),25) ' Fl_ALIGN_TEXT_OVER_IMAGE
Fl_WindowShow win ' <- must be shown for drawing
' params: menu, mnuPath ,mnuLabel , image ,[shortcut],[@MenuCB],IMAGE_ALIGN , LABEL_ALIGN
Fl_Menu_AddImageLabel(bar,"File/Test1","Test" ,"media/8x8. png", , ,Fl_ALIGN_IMAGE_OVER_TEXT, Fl_ALIGN_LEFT)
Fl_Menu_AddImageLabel(bar,"File/Test2","Test" ,"media/8x8. png", , ,Fl_ALIGN_TEXT_OVER_IMAGE, Fl_ALIGN_RIGHT)
Fl_Menu_AddImageLabel(bar,"File/Open1","Open. . . ","media/16x16. png", , ,Fl_ALIGN_IMAGE_NEXT_TO_TEXT, Fl_ALIGN_LEFT or FL_ALIGN_TOP)
Fl_Menu_AddImageLabel(bar,"File/Open2","Open. . . ","media/16x16. png", , ,Fl_ALIGN_IMAGE_NEXT_TO_TEXT, Fl_ALIGN_CENTER)
Fl_Menu_AddImageLabel(bar,"File/Open3","Open. . . ","media/16x16. png", , ,Fl_ALIGN_IMAGE_NEXT_TO_TEXT, Fl_ALIGN_RIGHT or FL_ALIGN_BOTTOM)
Fl_Menu_AddImageLabel(bar,"File/Exit3","Exit" ,"media/exit-2_32x32. png" ,,@QuitCB,Fl_ALIGN_TEXT_NEXT_TO_IMAGE, Fl_ALIGN_RIGHT or FL_ALIGN_BOTTOM)
Fl_Menu_AddImageLabel(bar,"File/Exit2","Exit" ,"media/application-exit-4_32x32. png",,@QuitCB,Fl_ALIGN_TEXT_NEXT_TO_IMAGE, Fl_ALIGN_CENTER)
Fl_Menu_AddImageLabel(bar,"File/Exit1","Exit" ,"media/exit-2_32x32. png" ,,@QuitCB,Fl_ALIGN_TEXT_NEXT_TO_IMAGE, Fl_ALIGN_LEFT or FL_ALIGN_TOP)
Fl_Run
can be written with my nim binding as
import fltk_main
import fltk_tools
proc QuitCB (self: ptr Fl_Widget, userdata: pointer) {.cdecl.} =
flMessageTitle("QuitCB called !")
if flChoice("Do you really want to exit ?","no","yes") > 0 :
Fl_WindowHide Fl_WidgetWindow(self)
var win = Fl_WindowNew(640,480)
var bar = Fl_Menu_BarNew(0,0,Fl_WidgetGetW(win),25) #' Fl_ALIGN_TEXT_OVER_IMAGE
Fl_WindowShow win #' <- must be shown for drawing
#~ ' params: menu, mnuPath,mnuLabel, image,[shortcut],[MenuCB],IMAGE_ALIGN, LABEL_ALIGN
# you can use 0 or nil for the dummy MenuCB
Fl_Menu_AddImageLabel(bar, "File/Test1", "Test", "media/8x8.png", 0, 0, Fl_ALIGN_IMAGE_OVER_TEXT, Fl_ALIGN_LEFT)
Fl_Menu_AddImageLabel(bar, "File/Test2", "Test", "media/8x8.png", 0, nil, Fl_ALIGN_TEXT_OVER_IMAGE, Fl_ALIGN_RIGHT)
Fl_Menu_AddImageLabel(bar, "File/Open1", "Open...", "media/16x16.png", 0, 0, Fl_ALIGN_IMAGE_NEXT_TO_TEXT, Fl_ALIGN_LEFT or FL_ALIGN_TOP)
Fl_Menu_AddImageLabel(bar, "File/Open2", "Open...", "media/16x16.png", 0, 0, Fl_ALIGN_IMAGE_NEXT_TO_TEXT, Fl_ALIGN_CENTER)
Fl_Menu_AddImageLabel(bar, "File/Open3", "Open...", "media/16x16.png", 0, 0, Fl_ALIGN_IMAGE_NEXT_TO_TEXT, Fl_ALIGN_RIGHT or FL_ALIGN_BOTTOM)
Fl_Menu_AddImageLabel(bar, "File/Exit3", "Exit", "media/exit-2_32x32.png", 0, QuitCB, Fl_ALIGN_TEXT_NEXT_TO_IMAGE, Fl_ALIGN_RIGHT or FL_ALIGN_BOTTOM)
Fl_Menu_AddImageLabel(bar, "File/Exit2", "Exit", "media/application-exit-4_32x32.png", 0, QuitCB, Fl_ALIGN_TEXT_NEXT_TO_IMAGE, Fl_ALIGN_CENTER)
Fl_Menu_AddImageLabel(bar, "File/Exit1", "Exit", "media/exit-2_32x32.png", 0, QuitCB, Fl_ALIGN_TEXT_NEXT_TO_IMAGE, Fl_ALIGN_LEFT or FL_ALIGN_TOP)
Fl_Run()