58 lines
1.6 KiB
Elixir
58 lines
1.6 KiB
Elixir
|
|
--# Passing data using a single map, accessed by field name;
|
|
|
|
include GtkEngine.e
|
|
include std/map.e
|
|
|
|
constant docs = `<u><b>Passing data using a Map</b></u>
|
|
Another way to attach data to controls,
|
|
and retrieve data by field-name.
|
|
|
|
This stores all data in a single map.
|
|
Fields can be entered in random order, but
|
|
each field must be named.
|
|
`
|
|
|
|
object x = map:new() -- using named fields, so can be in random order;
|
|
|
|
map:put(x,"jerry","name=Jerry Smith,bal=39.99,age=15,pix=thumbnails/Jerry.jpg")
|
|
map:put(x,"justin","name=Justin Jones,pix=thumbnails/Justin.jpg,age=14,bal=12.99")
|
|
|
|
constant win = create(GtkWindow,"border=10,size=300x100,position=1,$destroy=Quit")
|
|
|
|
constant panel = create(GtkBox,"orientation=VERTICAL")
|
|
add(win,panel)
|
|
|
|
constant lbl = create(GtkLabel)
|
|
set(lbl,"markup",docs)
|
|
add(panel,lbl)
|
|
|
|
constant
|
|
btn1 = create(GtkButton,"gtk-quit","Quit"),
|
|
btn2 = create(GtkButton,"gtk-ok#Jerry","ShowInfo","jerry"),-- linked by names;
|
|
btn3 = create(GtkButton,"gtk-ok#Justin","ShowInfo","justin"),
|
|
box = create(GtkButtonBox)
|
|
pack(panel,-box)
|
|
add(box,{btn1,btn2,btn3})
|
|
|
|
show_all(win)
|
|
main()
|
|
|
|
------------------------------------------------
|
|
global function ShowInfo(atom ctl, object who)
|
|
------------------------------------------------
|
|
who = unpack(who) -- retrieve passed name (e.g. jerry)
|
|
object data = keyvalues(map:get(x,who),,,,"\t\n\r")
|
|
|
|
for i = 1 to length(data) do
|
|
data[i] = text:format("[]=[]",data[i])
|
|
end for
|
|
display(data)
|
|
Info(,,
|
|
format("[{name}]",data), -- retrieve items by key;
|
|
format(" age: [{age}]\n bal: $[{bal}]",data),,
|
|
format("[{pix}]",data))
|
|
return 1
|
|
end function
|
|
|