! Online Dictionary - Px* + IUP
BEGIN
lib = DLL(ADDR "/home/jrs/pxbasic/pxbiup.so")
DIM servers$[0]
servers$[0]="dict.org"
about$ = "ProvideX IUP Binding"
! Initialize IUP
ok = DLL(lib, "IupOpen")
! Create main window
win = DLL(lib, "IupCreate", "dialog"+$00$)
ok = DLL(lib, "IupSetAttributes", win, "TITLE="+QUO+"Online Dictionary - ProvideX/IUP"+QUO+", SIZE=500x300"+$00$)
ok = DLL(lib, "PxBSetCallback", win,"CLOSE_CB"+$00$)
! Create container to house ALL GUI objects
vbox = DLL(lib, "IupCreate", "vbox"+$00$)
ok = DLL(lib, "IupSetAttributes", vbox, "MARGIN=10x10"+$00$)
! Create server panel
topBox = DLL(lib, "IupCreate", "hbox"+$00$)
ok = DLL(lib, "IupSetAttributes", topBox, "GAP=10"+$00$)
ok = DLL(lib, "IupAppend", vbox, topBox)
serverFrame = DLL(lib, "IupCreate", "frame"+$00$)
ok = DLL(lib, "IupSetAttributes", serverFrame, "TITLE=Servers, EXPAND=YES"+$00$)
ok = DLL(lib, "IupAppend", topBox, serverFrame)
serverBox = DLL(lib, "IupCreate", "hbox"+$00$)
ok = DLL(lib, "IupSetAttributes", serverBox, "GAP=5"+$00$)
ok = DLL(lib, "IupAppend", serverFrame, serverBox)
serverCombo = DLL(lib, "IupCreate", "list"+$00$)
ok = DLL(lib, "IupSetAttributes", serverCombo, "DROPDOWN=YES, SIZE=120x, EXPAND=HORIZONTAL, VALUE=1"+$00$)
ok = DLL(lib, "IupAppend", serverBox, serverCombo)
ok = DLL(lib, "PxBSetCallback", serverCombo, "ACTION"+$00$)
btnFetch = DLL(lib, "IupCreate", "button"+$00$)
ok = DLL(lib, "IupSetAttributes", btnFetch, "TITLE=Fetch, SIZE = 50x"+$00$)
ok = DLL(lib, "IupAppend", serverBox, btnFetch)
ok = DLL(lib, "PxBSetCallback", btnFetch, "ACTION"+$00$)
! Create control panel
controlFrame = DLL(lib, "IupCreate", "frame"+$00$)
ok = DLL(lib, "IupSetAttributes", controlFrame, "TITLE=Controls"+$00$)
ok = DLL(lib, "IupAppend", topBox, controlFrame)
controlBox = DLL(lib, "IupCreate", "hbox"+$00$)
ok = DLL(lib, "IupSetAttributes", controlBox, "GAP=5"+$00$)
ok = DLL(lib, "IupAppend", controlFrame, controlBox)
btnAbout = DLL(lib, "IupCreate", "button"+$00$)
ok = DLL(lib, "IupSetAttributes", btnAbout, "TITLE=About, SIZE = 50x"+$00$)
ok = DLL(lib, "IupAppend", controlBox, btnAbout)
ok = DLL(lib, "PxBSetCallback", btnAbout, "ACTION"+$00$)
btnClear = DLL(lib, "IupCreate", "button"+$00$)
ok = DLL(lib, "IupSetAttributes", btnClear, "TITLE=Clear, SIZE = 50x"+$00$)
ok = DLL(lib, "IupAppend", controlBox, btnClear)
ok = DLL(lib, "PxBSetCallback", btnClear, "ACTION"+$00$)
btnExit = DLL(lib, "IupCreate", "button"+$00$)
ok = DLL(lib, "IupSetAttributes", btnExit, "TITLE=Exit, SIZE = 50x"+$00$)
ok = DLL(lib, "IupAppend", controlBox, btnExit)
ok = DLL(lib, "PxBSetCallback", btnExit,"ACTION"+$00$)
! Create dictionary panel
dictFrame = DLL(lib, "IupCreate", "frame"+$00$)
ok = DLL(lib, "IupSetAttributes", dictFrame, "TITLE=Dictionaries"+$00$)
ok = DLL(lib, "IupAppend", vbox, dictFrame)
serverList = DLL(lib, "IupCreate", "list"+$00$)
ok = DLL(lib, "IupSetAttributes", serverList, "EXPAND=YES, VISIBLELINES=1"+$00$)
ok = DLL(lib, "IupAppend", dictFrame, serverList)
ok = DLL(lib, "PxBSetCallback", serverList, "ACTION"+$00$)
! Create text part
transFrame = DLL(lib, "IupCreate", "frame"+$00$)
ok = DLL(lib, "IupSetAttributes", transFrame, "TITLE=Translation"+$00$)
ok = DLL(lib, "IupAppend", vbox, transFrame)
text = DLL(lib, "IupCreate", "text"+$00$)
ok = DLL(lib, "IupSetAttributes", text, "MULTILINE=YES, EXPAND=YES"+$00$)
ok = DLL(lib, "IupAppend", transFrame, text)
! Create entry and search button
bottomBox = DLL(lib, "IupCreate", "hbox"+$00$)
ok = DLL(lib, "IupSetAttributes", bottomBox, "GAP=10"+$00$)
ok = DLL(lib, "IupAppend", vbox, bottomBox)
label = DLL(lib, "IupCreate", "label"+$00$)
ok = DLL(lib, "IupSetAttributes", label, "TITLE="+QUO+"Enter Word to Search For:"+QUO+", SIZE=x12"+$00$)
ok = DLL(lib, "IupAppend", bottomBox, label)
entry = DLL(lib, "IupCreate", "text"+$00$)
ok = DLL(lib, "IupSetAttributes", entry, "EXPAND=HORIZONTAL"+$00$)
ok = DLL(lib, "IupAppend", bottomBox, entry)
btnSearch = DLL(lib, "IupCreate", "button"+$00$)
ok = DLL(lib, "IupSetAttributes", btnSearch, "TITLE=Search, SIZE=50x"+$00$)
ok = DLL(lib, "IupAppend", bottomBox, btnSearch)
ok = DLL(lib, "PxBSetCallback", btnSearch, "ACTION"+$00$)
chkAll = DLL(lib, "IupCreate","toggle"+$00$)
ok = DLL(lib, "IupSetAttributes", chkAll, "TITLE=ALL, SIZE=x12"+$00$)
ok = DLL(lib, "IupAppend", bottomBox, chkAll)
chkUTF = DLL(lib, "IupCreate", "toggle"+$00$)
ok = DLL(lib, "IupSetAttributes", chkUTF, "TITLE=UTF-8, SIZE=x12"+$00$)
ok = DLL(lib, "IupAppend", bottomBox, chkUTF)
! Add the main GUI container to the Window
ok = DLL(lib, "IupAppend", win, vbox)
! Setup dialog defaults
ok = DLL(lib, "IupShow", win)
ok = DLL(lib, "IupSetFocus", btnFetch)
FOR i = 0 TO DIM(READ MAX(servers$))
ok = DLL(lib, "IupSetAttribute", serverCombo, "APPENDITEM"+$00$, servers$[i]+$00$)
NEXT
ok = DLL(lib, "IupSetAttribute", serverCombo, "VALUE"+$00$, "1"+$00$)
ok = DLL(lib, "IupUpdate", serverCombo)
server_selection$ = servers$[0]
! Main processing loop
REPEAT
WAIT: ok = DLL(lib, "IupLoopStepWait")
this_event = DLL(lib, "GetEvent")
IF NOT(this_event) THEN GOTO WAIT
SWITCH this_event
CASE serverCombo
GOSUB serverCombo_selected
BREAK
CASE btnFetch
GOSUB btnFetch_clicked
BREAK
CASE btnAbout
GOSUB btnAbout_clicked
BREAK
CASE btnClear
GOSUB btnClear_clicked
BREAK
CASE serverList
GOSUB serverList_selected
BREAK
CASE btnSearch
GOSUB btnSearch_clicked
BREAK
END SWITCH
UNTIL this_event = win OR this_event = btnExit
ok = DLL(lib, "IupClose")
ok = DLL(DROP lib)
END
! Callback routines
btnAbout_clicked:
ok = DLL(lib, "IupMessage", "ABOUT"+$00$, about$)
RETURN
serverCombo_selected:
DIM server_selection$(1024)
ok = DLL(lib, "GetListSelectedText", server_selection$)
server_selection$ = STP(server_selection$, 2)
RETURN
serverList_selected:
DIM whichDictionary$(1024)
ok = DLL(lib, "GetListSelectedText", whichDictionary$)
whichDictionary$ = STP(whichDictionary$, 2)
RETURN
btnFetch_clicked:
dat$ = ""
DIM _total$[*]
count = 1
OPEN (1,BSZ=16384)"[TCP]"+server_selection$+";2628;NoDelay;stream"
WRITE (1)"SHOW DB"+$0D0A$
REPEAT
READ(1)raw_data$
dat$ = dat$ + raw_data$
UNTIL POS("250 ok" = raw_data$)
WRITE(1)"QUIT"+$0D0A$
CLOSE(1)
REPEAT
eol = POS($0D0A$=dat$)
_total$[count] = dat$(1,eol - 1)
dat$ = dat$(eol + 2)
count += 1
UNTIL dat$ = ""
FOR cnt = 3 TO count - 3
ok = DLL(lib, "IupSetAttribute", serverList, "APPENDITEM"+$00$, _total$[cnt]+$00$)
NEXT cnt
ok = DLL(lib, "IupSetAttribute", serverList, "APPENDITEM"+$00$, _total$[3]+$00$)
ok = DLL(lib, "IupSetAttribute", serverList, "VALUE"+$00$, "1"+$00$)
ok = DLL(lib, "IupUpdate", serverCombo)
whichDictionary$ = _total$[3]
RETURN
G_NetError:
PRINT "Server ",server_selection$," not available. (",ERROR,")"
RETURN
btnClear_clicked:
ok = DLL(lib, "IupSetAttribute", serverList,"1"+$00$)
ok = DLL(lib, "IupSetAttribute", text, "VALUE"+$00$, ""+$00$)
ok = DLL(lib, "IupSetAttribute", entry, "VALUE"+$00$, ""+$00$)
RETURN
btnSearch_clicked:
dict$ = ""
dat$ = ""
total$ = ""
info$ = ""
ok = DLL(lib, "IupSetAttribute", text, "VALUE"+$00$, "Fetching...."+$00$)
dict$ = whichDictionary$(1, POS(" " = whichDictionary$) - 1)
OPEN (1,BSZ=16384)"[TCP]"+server_selection$+";2628;NoDelay;stream"
chkval$ = FNIupGetString$(DLL(lib, "IupGetAttribute", chkAll, "VALUE"+$00$))
IF chkval$ = "ON" THEN {
WRITE(1)"DEFINE * " + FNIupGetString$(DLL(lib, "IupGetAttribute", entry,"VALUE"+$00$)) + $0D0A$
} ELSE {
WRITE(1)"DEFINE " + dict$ + " " + FNIupGetString$(DLL(lib, "IupGetAttribute", entry,"VALUE"+$00$)) + $0D0A$
}
REPEAT
READ(1)raw_data$
data_str$ = data_str$ + raw_data$
UNTIL POS("250 ok [d/m/c =" = raw_data$)
WRITE(1)"QUIT"+$0D0A$
CLOSE(1)
! Remove header
data_str$ = data_str$(POS($0D0A$=data_str$, 1, 2) + 2)
REPEAT
eol = POS($0D0A$=data_str$)
dat$ = data_str$(1,eol - 1)
data_str$ = data_str$(eol + 2)
IF dat$(1, 3) <> "151" THEN GOTO IT
entstr$ = FNIupGetString$(DLL(lib, "IupGetAttribute", entry, "VALUE"+$00$))
total$ = total$ + "------------------------------" + $0D0A$
total$ = total$ + dat$(2 + LEN(entstr$) + LEN(dict$)) + $0D0A$
total$ = total$ + "------------------------------"+ $0D0A$
REPEAT
eol = POS($0D0A$=data_str$)
info$ = data_str$(1,eol - 1)
data_str$ = data_str$(eol + 2)
info$ = SUB(info$, CHR(34), CHR(92) + CHR(34))
IF LEN(info$) AND info$(1, 1) <> "." THEN total$ += STP(info$, 2) + $0D0A$
UNTIL info$ > "" AND info$(1, 1) = "."
total$ += $0D0A$
IT:
UNTIL data_str$(1, 3) = "250" OR NUM(data_str$(1, 3)) > 499
IF dat$(1, 3) = "552" THEN total$ = "No match found."
IF dat$(1, 3) = "501" THEN total$ = "Select a dictionary first!"
IF dat$(1, 3) = "550" THEN total$ = "Invalid database!"
ok = DLL(lib, "IupSetAttribute", text, "VALUE"+$00$, total$+$00$)
RETURN
L_NetError:
dat$ = "Could not lookup word! (" + STR(ERR) + ")"
ok = DLL(lib, "IupSetAttribute", text, "VALUE"+$00$, dat$+$00$)
RETURN
! DLL() return string emulator
DEF FNIupGetString$(LOCAL i_ptr)
LOCAL strlen, pxbuff$
DIM pxbuff$(4096)
strlen = DLL(lib, "ptr2pxbstr", i_ptr, pxbuff$)
RETURN pxbuff$(1,strlen)
END DEF