#####################################################################
# data format: item-type item-name cmd state subtrees
# data is in param(data)
#
proc build_tree { path {bg \#ffffff} {fg \#000000} } {
    global param
    
    set sbg $param(color-frame-bg)

    if { $path == "." } {
        set f [frame .tree]
    } else {
        set f [frame $path.tree]
    }
    set w [text $f.txt]
    # if ! first time, add scrollbars
    # and remove default bindings
    if { ! [catch { pack forget $w }] } {
	scrollbar $f.sy \
            -orient vertical \
            -width 12 \
	    -background $sbg \
            -activebackground $sbg \
	    -command [list $w yview]
	pack $f.sy -side right -fill y
        foreach b { 1 2 3 } {
          bind Text <B$b-Motion> {}
          bind Text <B$b-Leave> {}
          foreach mod { Shift Control } {
              bind Text <$mod-Button-$b> {}
              foreach mod2 { Double Triple } {
                  bind Text <$mod-$mod2-Button-$b> {}
                  bind Text <$mod2-Button-$b> {}
              }
          }
        }
    }
    $w configure \
	-background $bg \
	-foreground $fg \
	-width 20 \
	-height 10 \
        -wrap none \
	-state disabled \
        -cursor hand2 \
	-yscrollcommand [list $f.sy set]
    set param(tree,num-nodes) 0
    build_subtree $w 0 
    pack $w -fill both -expand 1
    pack $f -fill both -expand 1
}
#####################################################################
#
# Recursively build subtrees.
#
proc build_subtree { w level } {
    global param spaces

    $w configure -state normal

    set data [sort_items $param(data)]
    foreach item $data {
	set type  [lindex $item 0]
	set name  [lindex $item 1]
	set cmd   [lindex $item 2]
	set state [lindex $item 3]
	set sub   [lindex $item 4]
	set tstart [list [$w index end] - 1 lines]
        if { $level > 0 } {
	    $w insert end [string range $spaces 0 [expr $level * 3] ]
        } 
        $w image create end -image ${type}_img
        $w insert end "$name\n"
	set tend [list [$w index end] - 1 lines]
	if { $level < 2 } {
#	    $w tag add "${type}-${name}" $tstart $tend
#	    $w tag bind "${type}-${name}" <Button-1>  
	    $w tag add $param(tree,num-nodes) $tstart $tend
	    $w tag bind $param(tree,num-nodes) <Button-1> { 
		global param
		set tg [%W tag names @%x,%y]
		set y [lindex [split [%W index $tg.first] "."] 0]
		$param(tree,$y,cmd) $param(tree,$y,type) $param(tree,$y,name) $param(tree,$y,state)
                if { [info exists param(tree,curtag)] } {
                      %W tag configure $param(tree,curtag) -background $param(color-text-bg)
                }
                %W tag configure $tg -background $param(color-text-sel)
                set param(tree,curtag) $tg
	    }
	}
	if { [info exists param(tree,$level)] } {
	    incr param(tree,$level)
	} else {
	    set param(tree,$level) 0
	}
        incr param(tree,num-nodes)
	set param(tree,$param(tree,num-nodes),cmd) $cmd
	set param(tree,$param(tree,num-nodes),type) $type
	set param(tree,$param(tree,num-nodes),name) $name
	set param(tree,$param(tree,num-nodes),state) $state
	if { [llength $sub] > 0 } {
            set param(data) $sub
	    build_subtree $w [expr $level + 1]
	}
    }

    if { $level == 0 } {
        $w configure -state disabled
    }
}

#####################################################################
#
# Load icons for tree
#
proc load_images { } {
    image create bitmap eventset_img -file "eventset.bmp"
    image create bitmap groupalias_img -file "group.bmp"
    image create bitmap option_img -file "option.bmp"
}
#####################################################################
# NOTE: 1st character of name must be ascii
#       Don't start them with z
proc sort_items { data } {
    set d {}
    set len [llength $data]
    for { set i 0 } { $i < $len } { incr i } {
       set min 100
       set midx -1
       for { set j 0 } { $j < $len } { incr j } {
          set item [lindex $data $j]
          set n [sort_order [lindex $item 1]]
          if {  $n < $min } {
            set min  $n
            set midx $j
          } 
       }
       lappend d [lindex $data $midx]
       set data [lreplace $data $midx $midx {}]
    }
    return $d
}

proc sort_order { a } {
   if { $a == {} } { return 100 }
   set n [lsearch -exact \
             {groupalias eventset type id group val annotate} \
             $a ]
   if { $n == -1 } { set n 99 }
   return $n
}

#####################################################################
#
# Expand/contract subtree
#
proc expand_toggle { type name state } {
	global param
	return
}

set spaces "                                                       "
