Pastey TreeView updated test program
- Posted by irv
Aug 29, 2019
------------------------------------------------------------------------
--# GtkTreeStore updated to demonstrate recursion
------------------------------------------------------------------------
include GtkEngine.e
object store = create(GtkTreeStore,{gSTR,gSTR})
-- above, create two columns, one for the category/subcat name, the other for notes
-- e.g. category = Windows, notes = An Operating System
-- notes are optional.
-- IRL, additional columns would be added for other info, such as price, size, age...
sequence os = {
{"Windows","An Operating System",
{"Bill Gates","Zillionaire"} -- {1,1}
},
{"Linux","A Better Operating System", -- {2}
{"Linus Torvalds","Genius"}, -- {2,1}
{"Cast of thousands","Sweathogs"} -- {2,2}
},
{"Mac","A Religion",
{"Steve Jobs"}, -- {3,1}
{"The Woz", -- {3,2}
{"Billy","The kid", -- {3,2,1}
{"Bowser","Good Dog!"}, -- {3,2,1,1}
{"Fido",
{"Ball"},
{"Chew toy"},
{"Bone"}
} -- sub 2.2.2 -- {3,2,1,2}
},
{
"Susan","The other kid", -- {3,2,2}
{"Grumpy Cat","Sharp on all 4 corners!", -- {3,2,2,1}
{"Kitten 1", -- {3,2,2,1,1}
{"Toy mouse"}, -- {3,2,2,1,1,1}
{"Real mouse","A bit nervous"} -- {3,2,2,1,1,2}
},
{"Kitten 2"},
{"Kitten 3"} -- {3,2,2,1,3}
}
}
}
}
}
set(store,"data",os)
constant
col1 = create(GtkColumn,"title=OS,type=text,markup=1,sort_column_id=1"),
col2 = create(GtkColumn,"title=Notes,type=text,markup=2,sort_column_id=2")
constant tv = create(GtkTreeView,{
{"model",store},
{"append columns",{col1,col2}},
{"enable tree lines",TRUE},
{"rules hint",TRUE},
{"hover expand",TRUE},
{"enable search",TRUE}, -- typeahead;
{"expand all",TRUE},
$})
atom selection = get(tv,"selection") -- GtkTreeSelection
constant
win = create(GtkWindow,"size=250x400,border=10,$destroy=Quit"),
panel = create(GtkBox,"orientation=vertical,spacing=10"),
btn2 = create(GtkToggleButton,"gtk-ok#Expand","Expand"),
btn1 = create(GtkButton,"gtk-quit","Quit"),
box = create(GtkButtonBox),
lbl = create(GtkLabel)
add(win,panel)
pack(panel,tv,TRUE,TRUE,5)
add(panel,lbl)
add(box,{btn1,btn2})
pack(panel,-box)
show_all(win)
connect(tv,"row-activated","ShowSelection") -- open on mouse click;
--set(win,"interactive debugging",1) -- try this
main()
----------------------------------
global function ShowSelection() --
----------------------------------
atom iter = allocate(32), model = allocate(32), parent = allocate(32)
object path = 0, human_path, result
if gtk_func("gtk_tree_selection_get_selected",{P,P,P},{selection,model,iter}) then
result = get(store,"value",iter,1)
display("You clicked []",{result}) -- if you just want to show the clicked item;
path = get(store,"path",iter)
human_path = split(get(path,"to string"),':') -- e.g: 3:2:2:1:2
for i = 1 to length(human_path) do
human_path[i] = to_number(human_path[i]) + 1 -- fix zero-based c stuff;
end for
display(human_path)
-- obtain the full path:
while gtk_func("gtk_tree_model_iter_parent",{P,P,P},{store,parent,iter}) do
result = get(store,"value",parent,1) & " -> " & result
iter = gtk_func("gtk_tree_iter_copy",{P},{parent})
display(result)
end while
Info(,,result)
-- e.g: Mac -> The Woz -> Billy -> Fido -> Bone
end if
return 1
end function
---------------------------
global function Expand() --
---------------------------
if get(btn2,"active") then
set(tv,"expand all")
else
set(tv,"collapse all")
end if
return 1
end function