TkTreeCtrl - edit text items in place - example

package require treectrl 2.1

proc buildGUI {} {
    
    set top .tree_test
    catch {destroy $top}
    toplevel $top
    
    set treeFrame [frame $top.treeFrame]
    set tree [buildTree $treeFrame]
    
    grid $treeFrame -row 0 -column 0 -sticky news
    grid rowconfigure $treeFrame 0 -weight 1
    grid columnconfigure $treeFrame 0 -weight 1
    
    grid rowconfigure $top 0 -weight 1
    grid columnconfigure $top 0 -weight 1
}

proc buildTree {t} {
    
    set tree [treectrl $t.tree \
            -xscrollcommand "$t.sx set" \
            -yscrollcommand "$t.sy set" \
            -highlightthickness 0 \
            -selectmode extended]
    
    grid $tree -row 0 -column 0 -sticky news
    
    scrollbar $t.sx -orient h -command "$tree xview"
    scrollbar $t.sy -orient v -command "$tree yview"
    grid $t.sx -row 1 -column 0 -sticky new -padx 0 -pady 0
    grid $t.sy -row 0 -column 1 -sticky nes -padx 0 -pady 0
    
    $tree element create textElem text \
            -fill [list #FFFFFF { selected focus } ]
    $tree element create selectionRect rect \
            -fill [list #00008B { selected focus } #BFBFBF { selected !focus } ] \
            -showfocus no
            
    $tree style create textStyle
    $tree style elements textStyle {selectionRect textElem}
    $tree style layout textStyle textElem \
            -padx { 0 4 } -expand ns -iexpand ns -sticky s
    $tree style layout textStyle selectionRect \
            -union {textElem} -padx { 0 4 } -expand ns -iexpand ns -sticky s
    
    for {set column 0} {$column < 4} {incr column} {
        
        $tree column create
        $tree column configure $column \
                -text "Column $column" \
                -expand yes -button no
    }
    
    $tree notify install <Edit-begin>
    $tree notify install <Edit-end>
    $tree notify install <Edit-accept>
    
    $tree notify bind $tree <Edit-begin> "EditCallback begin %W %I %C %E %t"
    $tree notify bind $tree <Edit-end> "EditCallback end %W %I %C %E %t"
    $tree notify bind $tree <Edit-accept> "EditCallback accept %W %I %C %E %t"
    
    bind $tree <ButtonPress-1> "OnButtonPress1 %W %x %y"
    
    FillTable $tree
    return $tree
}

proc FillTable {tree} {
    
    for {set row 0} {$row < 4} {incr row} {
        set newItem [$tree item create]
        for {set column 0} {$column < 4} {incr column} {
            $tree item style set $newItem $column textStyle
            $tree item element configure $newItem $column textElem \
                    -text "test"
        }
        $tree item lastchild root $newItem
    }
}

proc EditCallback {args} {
    puts "$args"
    
    foreach {callback tree item column elem t} $args {}
    
    if {$callback eq "accept"} {
        $tree item element configure $item $column $elem -text $t
    }
}

proc OnButtonPress1 { tree x y } {
    
    set treeInfo [$tree identify $x $y]
    if {$treeInfo eq ""} {
        $tree selection clear
        return
    }
    
    if {[llength $treeInfo] != 6} {
        return
    }
    
    foreach {item itemNum column columnNum elem elemName} $treeInfo {}
    
    if { $item == "item" && \
         $column == "column" && \
         $elem == "elem" } {
        
        
        if { $elemName eq "textElem"} {
            ::TreeCtrl::ButtonPress1 $tree $x $y
            update
            ::TreeCtrl::EntryOpen $tree $itemNum $columnNum $elemName
            return -code break
        }
    }
}

buildGUI