2016-11-25 00:33:18 -07:00

90 lines
2.5 KiB
Elixir

--# Passing data using multiple maps
include GtkEngine.e
include std/map.e
constant docs = `<u><b>Passing data using Maps</b></u>
Another way to attach data to controls.
This creates a separate map for each person, and
passes just that map to the called routine.
Each field must be named, and can be accessed later by
name rather than position.
Maps can be saved to disk, and loaded as necessary.
`
constant jerrymap = load_map(locate_file("resources/Jerry.map"))
constant justinmap = load_map(locate_file("resources/Justin.map"))
constant win = create(GtkWindow,"border=10,size=300x100,position=1")
connect(win,"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,"emblem-ok-symbolic#_Jerry","ShowInfo",jerrymap), -- link by map id;
btn3 = create(GtkButton,"emblem-ok-symbolic#_Justin","ShowInfo",justinmap),
btn4 = create(GtkButton,"gtk-save","Save"),
box = create(GtkButtonBox)
set(btn4,"sensitive",FALSE)
add(box,{btn1,btn2,btn3,btn4})
pack(panel,-box)
show_all(win)
main()
--------------------------------------------------------------
global function ShowInfo(atom ctl, object amap) -- map id passed
---------------------------------------------------------------
amap = pairs(amap)
for i = 1 to length(amap) do
amap[i] = text:format("[]=[]",amap[i])
end for
Info(,"Student",
text:format("Name: [{name}]",amap), -- retrieve items by key;
text:format(" age: [{age}]\n bal: $[{bal}]",amap),,
locate_file(text:format("[{pix}]",amap)))
return 1
end function
-------------------------
global function Save()
-------------------------
-- here's how to save a couple of maps
-- either way works, use whichever is easier;
map a = map:new_from_kvpairs({
{"name","Jerry Smith"},
{"age",`16`}, -- note, numbers must be quoted somehow;
{"bal",`49.95`},
{"city","Buffalo NY"},
{"phone","000-555-1212"},
{"pix","thumbnails/Jerry.jpg"}})
map b = map:new_from_string("""
name="Justin Jones",
age='14',
bal='12.99',
city="Reno, NV",
phone=123-456-6789,
pix=thumbnails/Justin.jpg""")
-- SM_RAW map is encoded, so not readable except to a programmer,
-- and the file is Binary mime type, so no apps to read it are usually available;
save_map(a,canonical_path("~/demos/resources/Jerry.map"),SM_TEXT)
save_map(b,canonical_path("~/demos/resources/Justin.map"),SM_TEXT)
return 1
end function