
------------------------------------------------------------------------
--# GtkTreeView with a drop-down Combo column
-- lets user select from a limited list of choices.
------------------------------------------------------------------------

include GtkEngine.e

constant docs = `<u><b>Combo in ListView</b></u>
Click a cell in the <i>Combo</i> column
<i>twice</i> to drop-down a list of manufacturers
`
constant win = create(GtkWindow,"size=400x200,border_width=10,position=1,$destroy=Quit")

constant panel = create(GtkBox,"orientation=VERTICAL")
    add(win,panel)

constant lbl = create(GtkLabel)
    pack(panel,lbl)
    set(lbl,"markup",docs)
	
constant tv = create(GtkTreeView)
constant category_selection = get(tv,"selection")
    add(panel,tv)

constant
 rend1 = create(GtkCellRendererText),
 col1 = create(GtkTreeViewColumn,{
    {"title","Text"},
    {"pack start",rend1},
    {"fixed width",100},
    {"expand",TRUE},
    {"add attribute",rend1,"text",1}})

constant col2 = create(GtkTreeViewColumn)
constant rend2 = create(GtkCellRendererCombo)
    connect(rend2,"changed",_("SelectionChanged"))
    set(col2,{
	{"title","Combo"},
	{"pack start",rend2},
	{"fixed width",150},
	{"add attribute",rend2,"text",2}})

    set(tv,"append columns",{col1,col2})
    
constant categories = {
    {"Television","Samsung"},
    {"Mobile Phone","LG"},
    {"DVD Player","Sony"}
    }
    
constant manufacturers = {
    {"Sony"}, 
    {"LG"}, 
    {"Panasonic"}, 
    {"Toshiba"}, 
    {"Nokia"}, 
    {"Samsung"}
    }
    
constant model1 = create(GtkListStore,{gSTR,gSTR})
    set(tv,"model",model1)
    set(model1,"data",categories)

constant model2 = create(GtkListStore,{gSTR})
    set(model2,"data",manufacturers)
    set(rend2,{
	{"property","model",model2},
	{"text-column",0},
	{"editable",TRUE}})

constant box = create(GtkButtonBox,"margin_top=10"),
    btn1 = create(GtkButton,"gtk-quit","Quit"),
    btn2 = create(GtkButton,"gtk-ok",_("PrintContents"))
    add(box,{btn1,btn2})
    pack(panel,-box)
    
show_all(win)

if minor_version > 14 then
   -- set(win,"interactive debugging",TRUE)
end if

main()

------------------------------------------------------------------------
function SelectionChanged(atom combo, atom path, atom iter)
------------------------------------------------------------------------
object newval = get(model2,"value",iter,1) 
integer row = get(category_selection,"selected row")
set(model1,"col data",row,2,newval)
return 0
end function

------------------------------------------------------------------------
function PrintContents() -- run from an xterm 
------------------------------------------------------------------------
object results = get(model1,"data"), txt = ""
 display(results)
 for i = 1 to length(results) do
    txt &= results[i][1] & " = " & results[i][2] & "\n"
 end for
 Info(win,"Your Choices",txt)
return 1
end function
