Pastey Updated tree store

------------------------------------------------------------------------
--# GtkTreeStore  
------------------------------------------------------------------------ 
 
include ../GtkEngine.e 
 
object store = create(GtkTreeStore,{gSTR,gSTR}) 
 
sequence os = { 
    {"Windows","An Operating ? System", 
        {"Bill Gates"}}, 
    {"Linux","An Operating ! System", 
        {"Linus Torvalds"}}, 
    {"Mac","A Religion", 
        {"Steve Jobs", 
         "The Woz", 
            {"Billy (the kid)", 
                {"Bowser","Fido"}, 
             "Susan (the other kid)", 
                {"Grumpy Cat"} 
            } 
        } 
    } 
} 
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}, 
--  {"expand all"}, 
    $}) 
     
constant selection = get(tv,"selection")  
 
     
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) 
 
    add(win,panel) 
    pack(panel,tv,TRUE,TRUE,5) 
    add(box,{btn1,btn2}) 
    pack(panel,-box) 
     
show_all(win) 
    connect(selection,"changed","ShowSelection") 
main() 
 
---------------------------------- 
global function ShowSelection() -- 
---------------------------------- 
atom iter = allocate(32), model = allocate(32), parent = allocate(32) 
atom fn1 = define_c_func(LIBS[1],"gtk_tree_selection_get_selected",{P,P,P},P) 
atom fn2 = define_c_func(LIBS[1],"gtk_tree_model_iter_parent",{P,P,P},B) 
atom fn3 = define_c_func(LIBS[1],"gtk_tree_iter_copy",{P},P) 
 
object result  
if c_func(fn1,{selection,model,iter}) then 
	result = get(store,"value",iter,1) 
	display(result) -- just shows clicked item; 
 
	while c_func(fn2,{store,parent,iter}) do 
		result = get(store,"value",parent,1)  & " -> " & result 
		iter = c_func(fn3,{parent}) 
	end while 
	display(result) -- show full path to clicked item; 
	-- e.g: Mac -> The Woz -> Billy (the kid) -> Fido 
 
end if 
return 1 
end function 
 
------------------------ 
global function Expand() 
------------------------ 
    if get(btn2,"active") then 
        set(tv,"expand all") 
        display(get(selection,"selected")) 
    else 
        set(tv,"collapse all") 
    end if 
return 1 
end function

1. Comment by irv 1 month ago

Need to remove the line display(get(selection,"selected") in function Expand(), or you get a crash!