
--------------------------------------------------------------------------------
--# Widgets have a "data" field which is a sort of associative list.
-- You delare various named variables and attach them to the control,
-- then access them later by name. All names and data MUST be strings.

-- This particular demo makes using these much more complicated that it 
-- needs to be, because I am 'encrypting' the answers,
-- and displaying them in a drop-down GtkExpander.

-- For an easier introduction to the use of data fields, please look at 
-- Passing Data in /documentation/HowItWorks.html and test18.ex
--------------------------------------------------------------------------------

include GtkEngine.e 
include resources/mongoose.e -- for oeu_logo as pixbuf
include resources/clown.e -- for clown pixbuf (pixbufs are re-usable, unlike images)

constant docs = `
Widgets have a <b><u>Data</u></b> space
which you can use to declare and pass 
key/value pairs
`
constant
 
q1 = "What is purple and conquered the world?",
ans1 = -- 'encoded' answer, so as not to spoil the joke for the programmer...
{254,27,69,114,71,74,117,110,129,106,119,109,110,123,41,125,113,110,41,80,123,106,121,110,
42,69,56,114,71},

q2 = "What lies at the bottom of the ocean and twitches?",
ans2 = -- 'top secret!'
{254,23,69,114,71,74,41,119,110,123,127,120,126,124,41,128,123,110,108,116,55,69,56,114,71},

q3 = "Why is Christmas just like another day at the office?",
ans3 = 
{254,71,98,120,126,41,109,120,41,106,117,117,41,120,111,41,125,113,110,41,128,120,123,116,
41,106,119,109,41,125,113,110,41,111,106,125,41,112,126,130,19,114,119,41,125,113,110,41,
124,126,114,125,41,112,110,125,124,41,106,117,117,41,125,113,110,41,108,123,110,109,114,
125,55}

constant foo = call_back(routine_id("Foo")) -- allow for compiling w/o error

constant win = create(GtkWindow,
    "title=`Data Passing`,border_width=10,position=1,icon=oeu_logo,$destroy=Quit")
   
constant panel = create(GtkBox,VERTICAL,10)
    add(win,panel)

constant lbl = create(GtkLabel)
    set(lbl,"markup",docs)
    add(panel,lbl)
    
Box box = create(GtkButtonBox)
	add(panel,box)
	
constant btn1 = add(box,create(GtkButton,"dialog-question#Question 1",foo))
    set(btn1,{
	{"tooltip text","World History"},
	{"data","question",q1},
	{"data","answer",ans1}})

constant btn2 = add(box,create(GtkButton,"dialog-question#Question 2",foo))
    set(btn2,{
	{"tooltip text","Science"},
	{"data","question",q2},
	{"data","answer",ans2}})

constant btn3 = add(box,create(GtkButton,"dialog-question#Question 3",foo))
    set(btn3,{
	{"tooltip text","Business"},
	{"data","question",q3},
	{"data","answer",ans3}})
	
show_all(win)
main()

-----------------------------------------------------
function Foo(atom ctl)
-----------------------------------------------------
object title = get(ctl,"tooltip text") 
object question = get(ctl,"data","question")
object ans = get(ctl,"data","answer")

ans = deserialize(ans)
ans = ans[1]

atom okbtn = create(GtkButton,"gtk-ok")
    show(okbtn)
    
atom dlg = create(GtkDialog,{ -- we make our own custom dialog;
    {"title",title},
    {"icon",oeu_logo},
    {"border width",10},
    {"position",GTK_WIN_POS_MOUSE},
    {"add action widget",okbtn,MB_OK}})

atom ca = get(dlg,"content area")

atom lbl1 = add(ca,create(GtkLabel,{
    {"font","Comic Sans MS, Century Schoolbook L, URW Chancery L, Bold 12"},
    {"markup",question},
    {"show",1}}))
  
atom exp = add(ca,create(GtkExpander,"Click here for the answer:"))
    set(exp,{
	{"font","12"},
	{"resize toplevel",TRUE},
	{"show",1}})
	
atom box = create(GtkBox,VERTICAL)
  add(exp,box)
    
atom lbl2 = add(box,create(GtkLabel,{
    {"font","Segoe Print Normal 24"},
    {"color","blue"},
    {"markup",ans}}))

atom img = create(GtkImage,clown)
    add(box,img)
    show_all(box)
   
set(dlg,"run")
set(img,"destroy") -- small memory leak if not destroyed.
set(dlg,"destroy")

return 1
end function

