Author Topic: ScriptBasic / IUP  (Read 7902 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
Re: ScriptBasic / IUP
« Reply #15 on: February 05, 2013, 07:04:39 PM »
This buttons example is using the DYC FFI runtime dynamic shared library interface method but using the same SBx_buttons main script that was used on Linux with the ScriptBasic IUP extension module. (see page one)



SBx_buttons
Code: [Select]
INCLUDE "sbx.inc"

SUB Btn1_clicked
  PRINT "BUTTON 1 Event\n"
END SUB

SUB Btn2_clicked
  PRINT "BUTTON 2 Event\n"
END SUB

SUB Btn3_clicked
  PRINT "BUTTON 3 Event\n"
END SUB

SUB Win_exit
  ExitLoop = TRUE
END SUB

win = DIALOG()
SETPROPERTY(win, "TITLE=\"SBx Buttons\", SIZE=300x")
horzbox = HBOX()
SETPROPERTY(horzbox, "GAP=5")
btn1 = BUTTON()
SETPROPERTY(btn1, "TITLE=Button1, EXPAND=HORIZONTAL")
btn2 = BUTTON()
SETPROPERTY(btn2, "TITLE=Button2, EXPAND=HORIZONTAL")
btn3 = BUTTON()
SETPROPERTY(btn3, "TITLE=Button3, EXPAND=HORIZONTAL")
APPEND(horzbox, btn1)
APPEND(horzbox, btn2)
APPEND(horzbox, btn3)
APPEND(win, horzbox)
EVENT(win,"CLOSE_CB",ADDRESS(Win_exit()))
EVENT(btn1,"ACTION",ADDRESS(Btn1_clicked()))
EVENT(btn2,"ACTION",ADDRESS(Btn2_clicked()))
EVENT(btn3,"ACTION",ADDRESS(Btn3_clicked()))
SHOW(win)

sbx.inc
Code: [Select]
' ScriptBasic IUP Interface

DECLARE SUB DLL ALIAS "dyc" LIB "dyc"

DLL("mc,i,iup.dll,IupOpen,pp",0,0)

FUNCTION DIALOG(ih)
  DIALOG = DLL("mc,p,iup.dll,IupDialog,p",ih)
END FUNCTION

SUB SETPROPERTY(ih, propstr)
  DLL("mc,i,iup.dll,IupSetAttributes,pz", ih, propstr)
END SUB

FUNCTION HBOX
  HBOX = DLL("mc,p,iup.dll,IupCreate,z", "hbox")
END FUNCTION

FUNCTION BUTTON
  BUTTON = DLL("mc,p,iup.dll,IupCreate,z", "button")
END FUNCTION

SUB APPEND(ih_to, ih_from)
  DLL("mc,i,iup.dll,IupAppend,pp", ih_to, ih_from)
END SUB

SUB EVENT(ih, aname, faddr)
  callback{ih}[0] = ih
  callback{ih}[1] = faddr
  callback{ih}[2] = aname
  DLL("mc,i,sbx.dll,SBxSetCallback,pz",ih, aname)
END SUB

FUNCTION SHOW(ih)
  DLL("mc,i,iup.dll,IupShow,p",ih)
  ExitLoop = 0
  REPEAT
    DLL("mc,i,iup.dll,IupLoopStepWait,p",0)
    this_event = DLL("mc,p,sbx.dll,GetEvent,p",0)
    IF this_event <> undef THEN
      IF this_event = callback{this_event}[0] THEN
        ICALL(callback{this_event}[1])
      END IF
    END IF
  UNTIL ExitLoop
  DLL("mc,i,iup.dll,IupClose,p",0)  
END FUNCTION  

C:\scriptbasic\SBx>sbiup SBx_buttons
BUTTON 1 Event
BUTTON 2 Event
BUTTON 3 Event

C:\scriptbasic\SBx>
« Last Edit: February 07, 2013, 12:01:14 AM by JRS »

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
Re: ScriptBasic / IUP
« Reply #16 on: February 05, 2013, 09:58:25 PM »
The goal of the Sbx IUP wrapper functions is to provide an easy to use form based GUI set of tools. I'm using IupCreate() for the controls so that prevents cascading them. You will still be able to use the IUP extension module directly for more granularity and control. SBx GUI created scripts will run on either Windows or Linux untouched. A HTML version of SBx in conjunction with the ScriptBasic multi-threaded web server is a planned project once the desktop version is released.


Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
Re: ScriptBasic / IUP
« Reply #17 on: February 08, 2013, 11:52:51 PM »
I was able to sync my Linux ScriptBasic IUP extension module with the Windows version of ScriptBasic. Notice the difference using the ScriptBasic IUP extension module verses the DYC (FFI) method of IUP access.




SBx_butttons
Code: [Select]
' SBx_buttons Example

INCLUDE "SBx"

SUB Btn1_clicked
  PRINT "BUTTON 1 Event\n"
END SUB

SUB Btn2_clicked
  PRINT "BUTTON 2 Event\n"
END SUB

SUB Btn3_clicked
  PRINT "BUTTON 3 Event\n"
END SUB

SUB Win_exit
  Iup::ExitLoop = TRUE
END SUB


win = WINDOW()
SETPROPERTY(win, "TITLE=\"SBx Buttons\", SIZE=300x")
horzbox = HBOX()
SETPROPERTY(horzbox, "GAP=5")
btn1 = BUTTON()
SETPROPERTY(btn1, "TITLE=Button1, EXPAND=HORIZONTAL")
btn2 = BUTTON()
SETPROPERTY(btn2, "TITLE=Button2, EXPAND=HORIZONTAL")
btn3 = BUTTON()
SETPROPERTY(btn3, "TITLE=Button3, EXPAND=HORIZONTAL")
APPEND(horzbox, btn1)
APPEND(horzbox, btn2)
APPEND(horzbox, btn3)
APPEND(win, horzbox)
EVENT(win,"CLOSE_CB",ADDRESS(Win_exit()))
EVENT(btn1,"ACTION",ADDRESS(Btn1_clicked()))
EVENT(btn2,"ACTION",ADDRESS(Btn2_clicked()))
EVENT(btn3,"ACTION",ADDRESS(Btn3_clicked()))
SHOW(win)

Sbx
Code: [Select]
' ScriptBasic IUP Interface

IMPORT iup.bas

Iup::Open()

FUNCTION WINDOW
  WINDOW = Iup::Create("dialog")
END FUNCTION

SUB SETPROPERTY(ih, propstr)
  Iup::SetAttributes(ih, propstr)
END SUB

FUNCTION HBOX
  HBOX = Iup::Create("hbox")
END FUNCTION

FUNCTION BUTTON
  BUTTON = Iup::Create("button")
END FUNCTION

SUB APPEND(ih_to, ih_from)
  Iup::Append(ih_to, ih_from)
END SUB

SUB EVENT(ih, class, funcaddr)
  Iup::SetCallback(ih, class,  funcaddr)
END SUB

FUNCTION SHOW(ih)
  Iup::Show(ih)
  Iup::MainLoop
  Iup::Close
END FUNCTION  

C:\scriptbasic\test>sbiup sbx_buttons
BUTTON 1 Event
BUTTON 2 Event
BUTTON 3 Event

C:\scriptbasic\test>
« Last Edit: February 08, 2013, 11:55:01 PM by JRS »

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
Re: ScriptBasic / IUP
« Reply #18 on: February 09, 2013, 01:04:50 AM »
This is a custom function I created to return the IUP system information in a ScriptBasic associative array.   8)

Code: [Select]
IMPORT iup.bas
IUP::Open()

IUP::Info(INFO)

PRINT "SYSTEMLANGUAGE: ",INFO{"SYSTEMLANGUAGE"},"\n"
PRINT "DRIVER: ",INFO{"DRIVER"},"\n"
PRINT "SYSTEM: ",INFO{"SYSTEM"},"\n"
PRINT "SYSTEMLOCALE: ",INFO{"SYSTEMLOCALE"},"\n"
PRINT "COMPUTERNAME: ",INFO{"COMPUTERNAME"},"\n"
PRINT "USERNAME: ", INFO{"USERNAME"},"\n"
PRINT "MONITORSINFO: ",INFO{"MONITORSINFO"},"\n"
PRINT "SCREENSIZE: ",INFO{"SCREENSIZE"},"\n"
PRINT "SCREENDEPTH: ",INFO{"SCREENDEPTH"},"\n"
PRINT "VIRTUALSCREEN: ",INFO{"VIRTUALSCREEN"},"\n"
PRINT "DLGFGCOLOR: ",INFO{"DLGFGCOLOR"},"\n"
PRINT "DLGBGCOLOR: ",INFO{"DLGBGCOLOR"},"\n"
PRINT "DEFAULTFONT: ",INFO{"DEFAULTFONT"},"\n"
PRINT "DEFAULTFONTSIZE: ",INFO{"DEFAULTFONTSIZE"},"\n"
PRINT "TXTFGCOLOR: ",INFO{"TXTFGCOLOR"},"\n"
PRINT "TXTBGCOLOR: ",INFO{"TXTBGCOLOR"},"\n"

Linux
Code: [Select]
jrs@laptop:~/sb/test$ scriba iupinfo.sb
SYSTEMLANGUAGE: en-us
DRIVER: GTK
SYSTEM: Linux
SYSTEMLOCALE: UTF-8
COMPUTERNAME: laptop
USERNAME: jrs
MONITORSINFO: 0 0 1440 900

SCREENSIZE: 1440x852
SCREENDEPTH: 24
VIRTUALSCREEN: 0 0 1440 900
DLGFGCOLOR:  76  76  76
DLGBGCOLOR: 242 241 240
DEFAULTFONT: Ubuntu 11
DEFAULTFONTSIZE: 11
TXTFGCOLOR:  60  60  60
TXTBGCOLOR: 255 255 255
jrs@laptop:~/sb/test$

Windows
Code: [Select]
C:\scriptbasic\test>scriba iupinfo.sb
SYSTEMLANGUAGE: English (United States)
DRIVER: Win32
SYSTEM: WinXP
SYSTEMLOCALE: 1252  (ANSI - Latin I)
COMPUTERNAME: XPVM
USERNAME: John
MONITORSINFO: 0 0 1436 775

SCREENSIZE: 1436x741
SCREENDEPTH: 32
VIRTUALSCREEN: 0 0 1436 775
DLGFGCOLOR:   0   0   0
DLGBGCOLOR: 236 233 216
DEFAULTFONT: Tahoma,  8
DEFAULTFONTSIZE: 8
TXTFGCOLOR:   0   0   0
TXTBGCOLOR: 255 255 255

C:\scriptbasic\test>
« Last Edit: February 09, 2013, 01:07:16 AM by JRS »

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
Re: ScriptBasic / IUP
« Reply #19 on: February 09, 2013, 09:27:59 AM »
If you would like to try some of the examples I have posted, (or some of your own) I have attached the ScriptBasic IUP extension modules for both Windows 32 and Ubuntu 64. I'll add a Ubuntu 32 version shortly.

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
Re: ScriptBasic / IUP
« Reply #20 on: February 12, 2013, 07:14:42 PM »
I expanded on the SBx IUP wrapper library enough to convert the online dictionary example. (see attached - running on Ubuntu 64) This same code ran untouched on Windows.

SBx_dict.sb
Code: [Select]
' SBx Online Dictionary

servers[0]="dict.org"
servers[1]="dict1.us.dict.org"
servers[2]="all.dict.org"

about="""This is a Demo
of the IUP GUI Binding
for Scriptbasic"""

INCLUDE "SBx"

' Create main window
win = DIALOG()
  SETPROPERTIES win, "TITLE=\"SBx Dictionary\", SIZE = 500x300"
  SETEVENT win, "CLOSE_CB", ADDRESS(Win_exit())

' Create container to house ALL GUI objects
vbx = VBOX()
  SETPROPERTIES vbx, "MARGIN=10x10"

' Create server panel
topBox = HBOX()
  SETPROPERTIES topBox, "GAP=10"
  APPEND vbx, topBox
serverFrame = FRAME()
  SETPROPERTIES serverFrame, "TITLE=Servers, EXPAND=YES"
  APPEND topBox, serverFrame
serverBox = HBOX()
  SETPROPERTIES serverBox, "GAP=5"
  APPEND serverFrame, serverBox
serverCombo = LIST()
  SETPROPERTIES serverCombo, "DROPDOWN=YES, SIZE=120x, EXPAND=HORIZONTAL, VALUE=1"
  APPEND serverBox, serverCombo
  SETEVENT serverCombo, "ACTION", ADDRESS(serverCombo_selected())
btnFetch = BUTTON()
  SETPROPERTIES btnFetch, "TITLE=Fetch, SIZE = 50x"
  APPEND serverBox, btnFetch
  SETEVENT btnFetch, "ACTION", ADDRESS(btnFetch_clicked())

' Create control panel
controlFrame = FRAME()
  SETPROPERTIES controlFrame, "TITLE=Controls"
  APPEND topBox, controlFrame
controlBox = HBOX()
  SETPROPERTIES controlBox, "GAP=5"
  APPEND controlFrame, controlBox
btnAbout = BUTTON()
  SETPROPERTIES btnAbout, "TITLE=About, SIZE = 50x"
  APPEND controlBox, btnAbout
  SETEVENT btnAbout, "ACTION", ADDRESS(btnAbout_clicked())
btnClear = BUTTON()
  SETPROPERTIES btnClear, "TITLE=Clear, SIZE = 50x"
  APPEND controlBox, btnClear
  SETEVENT btnClear, "ACTION", ADDRESS(btnClear_clicked())
btnExit = BUTTON()
  SETPROPERTIES btnExit, "TITLE=Exit, SIZE = 50x"
  APPEND controlBox, btnExit
  SETEVENT btnExit,"ACTION",ADDRESS(Win_exit())

' Create dictionary panel
dictFrame = FRAME()
  SETPROPERTIES dictFrame, "TITLE=\"Dictionaries\""
  APPEND vbx, dictFrame
serverList = LIST()
  SETPROPERTIES serverList, "EXPAND=YES, VISIBLELINES=1"
  APPEND dictFrame, serverList
  SETEVENT serverList, "ACTION", ADDRESS(serverList_selected())

' Create text part
transFrame = FRAME()
  SETPROPERTIES transFrame, "TITLE=\"Translation\""
  APPEND vbx, transFrame
txt = TEXT()
  SETPROPERTIES txt, "MULTILINE=YES, EXPAND=YES"
  APPEND transFrame, txt


' Create entry and search button
bottomBox = HBOX()
  SETPROPERTIES bottomBox, "GAP=10"
  APPEND vbx, bottomBox
lbl = LABEL()
  SETPROPERTIES lbl, "TITLE=\"Enter Word to Search For:\", SIZE=x12"
  APPEND bottomBox, lbl
entry = TEXT()
  SETPROPERTIES entry, "EXPAND=HORIZONTAL"
  APPEND bottomBox, entry
btnSearch = BUTTON()
  SETPROPERTIES btnSearch,"TITLE=Search, SIZE=50x"
  APPEND bottomBox, btnSearch
  SETEVENT btnSearch, "ACTION", ADDRESS(btnSearch_clicked())
chkAll = TOGGLE()
  SETPROPERTIES chkAll, "TITLE=ALL, SIZE=x12"
  APPEND bottomBox, chkAll
chkUTF = TOGGLE()
  SETPROPERTIES chkUTF, "TITLE=UTF-8, SIZE=x12"
  APPEND bottomBox, chkUTF

' Add the main GUI container to the Window
APPEND win, vbx

' Setup dialog defaults
SHOW win
FOCUS btnFetch
FOR i = 0 TO UBOUND(servers)
  SETPROPERTY serverCombo, "APPENDITEM", servers[i]
NEXT
SETPROPERTY serverCombo, "VALUE", "1"
UPDATE serverCombo
server_selection = servers[0]
GETEVENT()
END


' Callback routines

SUB Win_exit
  Iup::ExitLoop = TRUE
END SUB

SUB btnAbout_clicked
  MESSAGE "ABOUT", about
END SUB

SUB serverCombo_selected
  server_selection = GETITEM()
END SUB

SUB serverList_selected
  whichDictionary = GETITEM()
END SUB

SUB btnFetch_clicked
  LOCAL dat, total, count
  ON ERROR GOTO G_NetError
  OPEN server_selection & ":2628" FOR SOCKET AS #1
  PRINT#1,"SHOW DB\n"
  LINE INPUT#1, dat
  LINE INPUT#1, dat
  count = 0
  WHILE LEFT(dat, 1) <> "."
    LINE INPUT#1, dat
    IF LEFT(dat, 1) <> "." THEN total[count] = TRIM(dat)
    count+=1
  WEND
  PRINT#1,"QUIT\n"
  CLOSE(#1)
  FOR cnt = 0 TO count - 2
    SETPROPERTY serverList, "APPENDITEM", total[cnt]
  NEXT
  SETPROPERTY serverList, "VALUE", "1"
  UPDATE serverCombo
  whichDictionary = total[0]
  EXIT SUB

  G_NetError:
  PRINT "Server ",server_selection," not available. (",ERROR,")\n"
END SUB

SUB btnClear_clicked
  CLEAR serverList
  SETPROPERTY txt, "VALUE", ""
  SETPROPERTY entry, "VALUE", ""
END SUB

SUB btnSearch_clicked
  LOCAL dict, dat, total, info
  SETPROPERTY txt, "VALUE", "Fetching...."
  ON ERROR GOTO L_NetError
  dict = LEFT(whichDictionary, INSTR(whichDictionary, " "))
  OPEN server_selection & ":2628" FOR SOCKET AS 1
  IF GETPROPERTY(chkAll, "VALUE") THEN
    PRINT#1,"DEFINE * " & GETPROPERTY(entry, "VALUE") & "\n"
  ELSE
    PRINT#1,"DEFINE " & dict & " " & GETPROPERTY(entry, "VALUE") & "\n"
  END IF
  REPEAT
    LINE INPUT#1, dat
    IF LEFT(dat, 3) = "151" THEN
      total &= "------------------------------\r\n"
      total &= RIGHT(dat, LEN(dat) - LEN(GETPROPERTY(entry, "VALUE")) - LEN(dict))
      total &= "------------------------------\r\n"
      REPEAT
        LINE INPUT#1, info
        info = REPLACE(info, CHR(34), CHR(92) & CHR(34))
        IF LEFT(info, 1) <> "." THEN total &= TRIM(info) & "\n"
      UNTIL LEFT(info, 1) = "."
      total &= "\n"
    END IF
  UNTIL LEFT(dat, 3) = "250" OR VAL(LEFT(dat, 3)) > 499
  PRINT#1,"QUIT\n"
  CLOSE(#1)
  IF LEFT(dat, 3) = "552" THEN
    total = "No match found."
  ELSE IF LEFT(dat, 3) = "501" THEN
    total = "Select a dictionary first!"
  ELSE IF LEFT(dat, 3) = "550" THEN
    total = "Invalid database!"
  END IF
  SETPROPERTY txt, "VALUE", total
EXIT SUB

L_NetError:
  dat[0] = "Could not lookup word! (" & ERROR & ")"
  SETPROPERTY txt, "VALUE", dat
END SUB

SBx
Code: [Select]
' ScriptBasic IUP Interface

IMPORT iup.bas

Iup::Open()

FUNCTION DIALOG
  DIALOG = Iup::Create("dialog")
END FUNCTION

SUB SETPROPERTIES(ih, propstr)
  Iup::SetAttributes(ih, propstr)
END SUB

SUB SETPROPERTY(ih, typ, value)
  Iup::SetAttribute(ih, typ, value)
END SUB

FUNCTION GETPROPERTY(ih, typ)
  GETPROPERTY = Iup::GetAttribute(ih, typ)
END FUNCTION

FUNCTION VBOX
  VBOX = Iup::Create("vbox")
END FUNCTION

FUNCTION HBOX
  HBOX = Iup::Create("hbox")
END FUNCTION

FUNCTION FRAME
  FRAME = Iup::Create("frame")
END FUNCTION

FUNCTION BUTTON
  BUTTON = Iup::Create("button")
END FUNCTION

FUNCTION LIST
  LIST = Iup::Create("list")
END FUNCTION

FUNCTION TEXT
  TEXT = Iup::Create("text")
END FUNCTION

FUNCTION LABEL
  LABEL = Iup::Create("label")
END FUNCTION

FUNCTION TOGGLE
  TOGGLE = Iup::Create("toggle")
END FUNCTION

SUB MESSAGE(title, body)
  Iup::Message(title, body)
END SUB

FUNCTION GETITEM
  GETITEM = Iup::GetListText()
END FUNCTION
 
SUB APPEND(ih_to, ih_from)
  Iup::Append(ih_to, ih_from)
END SUB

FUNCTION FOCUS(ih)
  FOCUS = Iup::SetFocus(ih)
END FUNCTION

FUNCTION UPDATE(ih)
  UPDATE = Iup::Update(ih)
END FUNCTION

SUB CLEAR(ih)
  Iup::ClearList(ih)
END SUB

SUB SETEVENT(ih, class, funcaddr)
  Iup::SetCallback(ih, class,  funcaddr)
END SUB

SUB SHOW(ih)
  Iup::Show(ih)
END SUB  

SUB GETEVENT
  Iup::MainLoop
  Iup::Close
END SUB
« Last Edit: February 12, 2013, 10:22:37 PM by JRS »