Raspberry BASIC

Author Topic: Nim IUP  (Read 1331 times)

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 160
    • View Profile
    • ScriptBasic
Nim IUP
« on: December 23, 2019, 08:51:56 PM »
This is a Nim version of the ScriptBasic iup3buttons example.

Code: Text
  1. # IUP Button / Event Example
  2.  
  3. import iup
  4.  
  5. proc Btn1_clicked(ih:PIhandle):cint {.cdecl.}=
  6.   echo "BUTTON 1 Event"
  7.  
  8. proc Btn2_clicked(ih:PIhandle):cint {.cdecl.}=
  9.   echo "BUTTON 2 Event"
  10.  
  11. proc Btn3_clicked(ih:PIhandle):cint {.cdecl.}=
  12.   echo "BUTTON 3 Event"
  13.  
  14. discard iup.open(nil, nil)
  15. var win = iup.dialog(nil)
  16. discard iup.setAttributes(win, "TITLE=\"Test Dialog\", SIZE=300x")
  17. var horzbox = iup.hbox(nil)
  18. discard iup.setAttributes(horzbox, "GAP=5")
  19. var btn1 = iup.button("", nil)
  20. discard iup.setAttributes(btn1, "TITLE=Button1, EXPAND=HORIZONTAL")
  21. var btn2 = iup.button("", nil)
  22. discard iup.setAttributes(btn2, "TITLE=Button2, EXPAND=HORIZONTAL")
  23. var btn3 = iup.button("", nil)
  24. discard iup.setAttributes(btn3, "TITLE=Button3, EXPAND=HORIZONTAL")
  25. iup.append(horzbox, btn1)
  26. iup.append(horzbox, btn2)
  27. iup.append(horzbox, btn3)
  28. iup.append(win, horzbox)
  29. discard iup.setCallback(btn1, "ACTION", cast[ICallback](Btn1_clicked))
  30. discard iup.setCallback(btn2, "ACTION", cast[ICallback](Btn2_clicked))
  31. discard iup.setCallback(btn3, "ACTION", cast[ICallback](Btn3_clicked))
  32. discard iup.show(win)
  33. discard iup.mainLoop()
  34. iup.close()
  35.  


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

« Last Edit: December 23, 2019, 08:55:48 PM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 160
    • View Profile
    • ScriptBasic
Re: Nim IUP
« Reply #1 on: December 24, 2019, 10:51:20 AM »
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
  1. # Nim IUP Direct
  2.  
  3. const
  4.   libiupSONAME = "libiup.so"
  5.   IUP_CLOSE* = -3
  6.   IUP_CENTER* = cint(0x0000FFFF)
  7.  
  8. type
  9.   Ihandle = object
  10.   PIhandle* = ptr Ihandle
  11.   Icallback* = proc (a1: PIhandle): cint {.cdecl.}
  12.  
  13. proc btn_exit_cb(ih:PIhandle):cint {.cdecl.}=
  14.   return IUP_CLOSE
  15.  
  16. proc Open*(argc: var cint; argv: ptr cstringArray): cint {.cdecl, importc: "IupOpen", dynlib: libiupSONAME, discardable.}
  17. proc Button*(title: cstring; action: cstring): PIhandle {.cdecl, importc: "IupButton", dynlib: libiupSONAME.}
  18. proc Label*(title: cstring): PIhandle {.cdecl, importc: "IupLabel", dynlib: libiupSONAME.}
  19. proc Vbox*(child: PIhandle): PIhandle {.varargs, cdecl, importc: "IupVbox", dynlib: libiupSONAME.}
  20. proc Dialog*(child: PIhandle): PIhandle {.cdecl, importc: "IupDialog", dynlib: libiupSONAME.}
  21. proc SetAttribute*(ih: PIhandle; name: cstring; value: cstring) {.cdecl, importc: "IupSetAttribute", dynlib: libiupSONAME.}
  22. proc SetCallback*(ih: PIhandle; name: cstring; `func`: Icallback): Icallback {.cdecl, importc: "IupSetCallback", dynlib: libiupSONAME, discardable.}
  23. proc ShowXY*(ih: PIhandle; x: cint; y: cint): cint {.cdecl, importc: "IupShowXY", dynlib: libiupSONAME, discardable.}
  24. proc MainLoop*(): cint {.cdecl, importc: "IupMainLoop", dynlib: libiupSONAME, discardable.}
  25. proc Close*() {.cdecl, importc: "IupClose", dynlib: libiupSONAME.}
  26.  
  27.  
  28. var dlg, button, label, vbox: PIhandle
  29. var argc:cint
  30. var argv:ptr cstringArray
  31.  
  32. Open(argc, argv)
  33.  
  34. label =  Label("Hello world from IUP.")
  35. button = Button("OK", nil)
  36.  
  37. vbox = Vbox(label, button, nil)
  38. SetAttribute(vbox, "ALIGNMENT", "ACENTER")
  39. SetAttribute(vbox, "GAP", "10")
  40. SetAttribute(vbox, "MARGIN", "10x10")
  41.  
  42. dlg = Dialog(vbox)
  43. SetAttribute(dlg, "TITLE", "Ubuntu RPi")
  44.  
  45. SetCallback(button, "ACTION", btn_exit_cb)
  46.  
  47. ShowXY(dlg, IUP_CENTER, IUP_CENTER)
  48.  
  49. MainLoop()
  50.  
  51. Close()
  52.  

« Last Edit: December 24, 2019, 10:53:29 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

AIR

  • BASIC Developer
  • *
  • Posts: 16
  • Code Jockey
    • View Profile
Re: Nim IUP
« Reply #2 on: December 24, 2019, 11:36:12 PM »
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
  1. import ui
  2.  
  3. proc b1_clicked() = echo "Button 1 Event"
  4. proc b2_clicked() = echo "Button 2 Event"
  5. proc b3_clicked() = echo "Button 3 Event"  
  6.  
  7. ui.init()
  8.  
  9. let mainwin = newWindow("Window",1,1,false)
  10. let b1 = newButton("Button 1",b1_clicked)
  11. let b2 = newButton("Button 2",b2_clicked)
  12. let b3 = newButton("Button 3",b3_clicked)
  13. let hbox = newHorizontalBox(true)
  14.    
  15. mainwin.margined = true
  16. mainwin.onClosing = (proc (): bool = return true)
  17. mainwin.setChild(hbox)
  18.  
  19. hbox.add b1; hbox.add b2; hbox.add b3
  20.  
  21. mainwin.show
  22. ui.mainLoop()
  23.  

AIR.

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 160
    • View Profile
    • ScriptBasic
Re: Nim IUP
« Reply #3 on: December 25, 2019, 12:19:01 AM »
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.
ScriptBasic Project Manager/Facilitator