(phixonline)-->
with javascript_semantics
constant answers = {"As I see it, yes", "Ask again later", "You may rely on it",
"Without a doubt", "Don't bet on it", "Outlook not so good",
"Signs point to yes", "It is decidedly so", "It is certain",
"Better not tell you now", "My reply is no", "Outlook good",
"Concentrate and ask again", "Reply hazy, try again", "Yes",
"Most likely", "Cannot predict now", "My sources say maybe",
"My sources say no", "Yes, definitely", "Yes, probably not",
"Very doubtful", "Your question has already been answered"}
include pGUI.e
Ihandle dlg, vbox, question, answer
sequence answered = {}
bool clearkey = false
function key_cb(Ihandle /*dlg*/, atom c)
if c=K_ESC then return IUP_CLOSE end if -- (standard practice for me)
if c=K_F5 then return IUP_DEFAULT end if -- (let browser reload work)
if c=K_CR then
string q = IupGetAttribute(question,"VALUE"), reply
if length(q) then
if find(q,answered) then
reply = "Your question has already been answered"
else
answered = append(answered,q)
reply = answers[rand(length(answers))]
end if
IupSetStrAttribute(answer,"TITLE",reply)
clearkey = true
end if
elsif clearkey then
IupSetAttribute(question,"VALUE","")
clearkey = false
end if
return IUP_CONTINUE
end function
IupOpen()
question = IupText("EXPAND=HORIZONTAL")
answer = IupLabel("","EXPAND=HORIZONTAL")
vbox = IupVbox({question,answer},`MARGIN=40x40`)
dlg = IupDialog(vbox,`TITLE="Magic 8-ball",SIZE=250x100`)
IupSetCallback(dlg,"KEY_CB",Icallback("key_cb"))
IupShow(dlg)
if platform()!=JS then
IupMainLoop()
IupHide(dlg)
end if