Raspberry BASIC > Nim

Nim IUP

(1/1)

John Spikowski:
This is a Nim version of the ScriptBasic iup3buttons example.


--- Code: Text ---# IUP Button / Event Example import iup proc Btn1_clicked(ih:PIhandle):cint {.cdecl.}=  echo "BUTTON 1 Event" proc Btn2_clicked(ih:PIhandle):cint {.cdecl.}=  echo "BUTTON 2 Event" proc Btn3_clicked(ih:PIhandle):cint {.cdecl.}=  echo "BUTTON 3 Event" discard iup.open(nil, nil)var win = iup.dialog(nil)discard iup.setAttributes(win, "TITLE=\"Test Dialog\", SIZE=300x")var horzbox = iup.hbox(nil)discard iup.setAttributes(horzbox, "GAP=5")var btn1 = iup.button("", nil)discard iup.setAttributes(btn1, "TITLE=Button1, EXPAND=HORIZONTAL")var btn2 = iup.button("", nil)discard iup.setAttributes(btn2, "TITLE=Button2, EXPAND=HORIZONTAL")var btn3 = iup.button("", nil)discard iup.setAttributes(btn3, "TITLE=Button3, EXPAND=HORIZONTAL")iup.append(horzbox, btn1)iup.append(horzbox, btn2)iup.append(horzbox, btn3)iup.append(win, horzbox)discard iup.setCallback(btn1, "ACTION", cast[ICallback](Btn1_clicked))discard iup.setCallback(btn2, "ACTION", cast[ICallback](Btn2_clicked))discard iup.setCallback(btn3, "ACTION", cast[ICallback](Btn3_clicked))discard iup.show(win)discard iup.mainLoop()iup.close() 

ubuntu@rpi4b:~/repo/Nim/examples$ ./iup3buttons
BUTTON 1 Event
BUTTON 2 Event
BUTTON 3 Event
ubuntu@rpi4b:~/repo/Nim/examples$

John Spikowski:
I wanted to try a more direct approach to interfacing with an external library. I'm using the definitions from the niup interface file.


I might just roll my own IUP import file using niup as a guide.


--- Code: Text ---# Nim IUP Direct const  libiupSONAME = "libiup.so"  IUP_CLOSE* = -3  IUP_CENTER* = cint(0x0000FFFF) type  Ihandle = object  PIhandle* = ptr Ihandle  Icallback* = proc (a1: PIhandle): cint {.cdecl.} proc btn_exit_cb(ih:PIhandle):cint {.cdecl.}=  return IUP_CLOSE proc Open*(argc: var cint; argv: ptr cstringArray): cint {.cdecl, importc: "IupOpen", dynlib: libiupSONAME, discardable.}proc Button*(title: cstring; action: cstring): PIhandle {.cdecl, importc: "IupButton", dynlib: libiupSONAME.}proc Label*(title: cstring): PIhandle {.cdecl, importc: "IupLabel", dynlib: libiupSONAME.}proc Vbox*(child: PIhandle): PIhandle {.varargs, cdecl, importc: "IupVbox", dynlib: libiupSONAME.}proc Dialog*(child: PIhandle): PIhandle {.cdecl, importc: "IupDialog", dynlib: libiupSONAME.}proc SetAttribute*(ih: PIhandle; name: cstring; value: cstring) {.cdecl, importc: "IupSetAttribute", dynlib: libiupSONAME.}proc SetCallback*(ih: PIhandle; name: cstring; `func`: Icallback): Icallback {.cdecl, importc: "IupSetCallback", dynlib: libiupSONAME, discardable.}proc ShowXY*(ih: PIhandle; x: cint; y: cint): cint {.cdecl, importc: "IupShowXY", dynlib: libiupSONAME, discardable.}proc MainLoop*(): cint {.cdecl, importc: "IupMainLoop", dynlib: libiupSONAME, discardable.}proc Close*() {.cdecl, importc: "IupClose", dynlib: libiupSONAME.}  var dlg, button, label, vbox: PIhandlevar argc:cintvar argv:ptr cstringArray Open(argc, argv) label =  Label("Hello world from IUP.")button = Button("OK", nil) vbox = Vbox(label, button, nil)SetAttribute(vbox, "ALIGNMENT", "ACENTER")SetAttribute(vbox, "GAP", "10")SetAttribute(vbox, "MARGIN", "10x10") dlg = Dialog(vbox)SetAttribute(dlg, "TITLE", "Ubuntu RPi") SetCallback(button, "ACTION", btn_exit_cb) ShowXY(dlg, IUP_CENTER, IUP_CENTER) MainLoop() Close() 

AIR:
Not meant to hijack this thread, but I wanted to show your three-button demo using a different GUI package based on libui:


--- Code: Text ---import ui proc b1_clicked() = echo "Button 1 Event"proc b2_clicked() = echo "Button 2 Event"proc b3_clicked() = echo "Button 3 Event"    ui.init() let mainwin = newWindow("Window",1,1,false)let b1 = newButton("Button 1",b1_clicked)let b2 = newButton("Button 2",b2_clicked)let b3 = newButton("Button 3",b3_clicked)let hbox = newHorizontalBox(true)   mainwin.margined = truemainwin.onClosing = (proc (): bool = return true)mainwin.setChild(hbox) hbox.add b1; hbox.add b2; hbox.add b3 mainwin.showui.mainLoop() 
AIR.

John Spikowski:
You will be able to show off your GUI tricks with the next round of the language challenge. I'll post a ScriptBasic / IUP login dialog as a template for the challenge.

Navigation

[0] Message Index

Go to full version