?? tree.tcl
字號:
upvar 0 $path data if { ![string compare $node "root"] || ![info exists data($node)] } { return -code error "node \"$node\" does not exist" } if { ![info exists data($parent)] } { return -code error "node \"$parent\" does not exist" } set p $parent while { [string compare $p "root"] } { if { ![string compare $p $node] } { return -code error "node \"$parent\" is a descendant of \"$node\"" } set p [parent $path $p] } set oldp [lindex $data($node) 0] set idx [lsearch $data($oldp) $node] set data($oldp) [lreplace $data($oldp) $idx $idx] set data($node) [concat [list $parent] [lrange $data($node) 1 end]] if { ![string compare $index "end"] } { lappend data($parent) $node } else { incr index set data($parent) [linsert $data($parent) $index $node] } if { (![string compare $oldp "root"] || ([visible $path $oldp] && [Widget::getoption $path.$oldp -open])) || (![string compare $parent "root"] || ([visible $path $parent] && [Widget::getoption $path.$parent -open])) } { _redraw_idle $path 3 }}# ------------------------------------------------------------------------------# Command Tree::reorder# ------------------------------------------------------------------------------proc Tree::reorder { path node neworder } { variable $path upvar 0 $path data if { ![info exists data($node)] } { return -code error "node \"$node\" does not exist" } set children [lrange $data($node) 1 end] if { [llength $children] } { set children [BWidget::lreorder $children $neworder] set data($node) [linsert $children 0 [lindex $data($node) 0]] if { [visible $path $node] && [Widget::getoption $path.$node -open] } { _redraw_idle $path 3 } }}# ------------------------------------------------------------------------------# Command Tree::selection# ------------------------------------------------------------------------------proc Tree::selection { path cmd args } { variable $path upvar 0 $path data switch -- $cmd { set { set data(selnodes) {} foreach node $args { if { [info exists data($node)] } { if { [lsearch $data(selnodes) $node] == -1 } { lappend data(selnodes) $node } } } } add { foreach node $args { if { [info exists data($node)] } { if { [lsearch $data(selnodes) $node] == -1 } { lappend data(selnodes) $node } } } } remove { foreach node $args { if { [set idx [lsearch $data(selnodes) $node]] != -1 } { set data(selnodes) [lreplace $data(selnodes) $idx $idx] } } } clear { set data(selnodes) {} } get { return $data(selnodes) } default { return } } _redraw_idle $path 1}# ------------------------------------------------------------------------------# Command Tree::exists# ------------------------------------------------------------------------------proc Tree::exists { path node } { variable $path upvar 0 $path data return [info exists data($node)]}# ------------------------------------------------------------------------------# Command Tree::visible# ------------------------------------------------------------------------------proc Tree::visible { path node } { set idn [$path:cmd find withtag n:$node] return [llength $idn]}# ------------------------------------------------------------------------------# Command Tree::parent# ------------------------------------------------------------------------------proc Tree::parent { path node } { variable $path upvar 0 $path data if { ![info exists data($node)] } { return -code error "node \"$node\" does not exist" } return [lindex $data($node) 0]}# ------------------------------------------------------------------------------# Command Tree::index# ------------------------------------------------------------------------------proc Tree::index { path node } { variable $path upvar 0 $path data if { ![string compare $node "root"] || ![info exists data($node)] } { return -code error "node \"$node\" does not exist" } set parent [lindex $data($node) 0] return [expr {[lsearch $data($parent) $node] - 1}]}# ------------------------------------------------------------------------------# Command Tree::nodes# ------------------------------------------------------------------------------proc Tree::nodes { path node {first ""} {last ""} } { variable $path upvar 0 $path data if { ![info exists data($node)] } { return -code error "node \"$node\" does not exist" } if { ![string length $first] } { return [lrange $data($node) 1 end] } if { ![string length $last] } { return [lindex [lrange $data($node) 1 end] $first] } else { return [lrange [lrange $data($node) 1 end] $first $last] }}# ------------------------------------------------------------------------------# Command Tree::see# ------------------------------------------------------------------------------proc Tree::see { path node } { variable $path upvar 0 $path data if { [Widget::getoption $path -redraw] && $data(upd,afterid) != "" } { after cancel $data(upd,afterid) _redraw_tree $path } set idn [$path:cmd find withtag n:$node] if { $idn != "" } { Tree::_see $path $idn right Tree::_see $path $idn left }}# ------------------------------------------------------------------------------# Command Tree::opentree# ------------------------------------------------------------------------------proc Tree::opentree { path node } { variable $path upvar 0 $path data if { ![string compare $node "root"] || ![info exists data($node)] } { return -code error "node \"$node\" does not exist" } _recexpand $path $node 1 [Widget::getoption $path -opencmd] _redraw_idle $path 3}# ------------------------------------------------------------------------------# Command Tree::closetree# ------------------------------------------------------------------------------proc Tree::closetree { path node } { variable $path upvar 0 $path data if { ![string compare $node "root"] || ![info exists data($node)] } { return -code error "node \"$node\" does not exist" } _recexpand $path $node 0 [Widget::getoption $path -closecmd] _redraw_idle $path 3}# ------------------------------------------------------------------------------# Command Tree::edit# ------------------------------------------------------------------------------proc Tree::edit { path node text {verifycmd ""} {clickres 0} {select 1}} { variable _edit variable $path upvar 0 $path data if { [Widget::getoption $path -redraw] && $data(upd,afterid) != "" } { after cancel $data(upd,afterid) _redraw_tree $path } set idn [$path:cmd find withtag n:$node] if { $idn != "" } { Tree::_see $path $idn right Tree::_see $path $idn left set oldfg [$path:cmd itemcget $idn -fill] set sbg [Widget::getoption $path -selectbackground] set coords [$path:cmd coords $idn] set x [lindex $coords 0] set y [lindex $coords 1] set bd [expr {[$path:cmd cget -borderwidth]+[$path:cmd cget -highlightthickness]}] set w [expr {[winfo width $path] - 2*$bd}] set wmax [expr {[$path:cmd canvasx $w]-$x}] set _edit(text) $text set _edit(wait) 0 $path:cmd itemconfigure $idn -fill [Widget::getoption $path -background] $path:cmd itemconfigure s:$node -fill {} -outline {} set frame [frame $path.edit \ -relief flat -borderwidth 0 -highlightthickness 0 \ -background [Widget::getoption $path -background]] set ent [entry $frame.edit \ -width 0 \ -relief solid \ -borderwidth 1 \ -highlightthickness 0 \ -foreground [Widget::getoption $path.$node -fill] \ -background [Widget::getoption $path -background] \ -selectforeground [Widget::getoption $path -selectforeground] \ -selectbackground $sbg \ -font [Widget::getoption $path.$node -font] \ -textvariable Tree::_edit(text)] pack $ent -ipadx 8 -anchor w set idw [$path:cmd create window $x $y -window $frame -anchor w] trace variable Tree::_edit(text) w "Tree::_update_edit_size $path $ent $idw $wmax" tkwait visibility $ent grab $frame BWidget::focus set $ent _update_edit_size $path $ent $idw $wmax update if { $select } { $ent selection range 0 end $ent icursor end $ent xview end } bind $ent <Escape> {set Tree::_edit(wait) 0} bind $ent <Return> {set Tree::_edit(wait) 1} if { $clickres == 0 || $clickres == 1 } { bind $frame <Button> "set Tree::_edit(wait) $clickres" } set ok 0 while { !$ok } { tkwait variable Tree::_edit(wait) if { !$_edit(wait) || $verifycmd == "" || [uplevel \#0 $verifycmd [list $_edit(text)]] } { set ok 1 } } trace vdelete Tree::_edit(text) w "Tree::_update_edit_size $path $ent $idw $wmax" grab release $frame BWidget::focus release $ent destroy $frame $path:cmd delete $idw $path:cmd itemconfigure $idn -fill $oldfg $path:cmd itemconfigure s:$node -fill $sbg -outline $sbg if { $_edit(wait) } { return $_edit(text) } } return ""}# ------------------------------------------------------------------------------# Command Tree::xview# ------------------------------------------------------------------------------proc Tree::xview { path args } { return [eval $path:cmd xview $args]}# ------------------------------------------------------------------------------# Command Tree::yview# ------------------------------------------------------------------------------proc Tree::yview { path args } { return [eval $path:cmd yview $args]}# ------------------------------------------------------------------------------# Command Tree::_update_edit_size# ------------------------------------------------------------------------------proc Tree::_update_edit_size { path entry idw wmax args } { set entw [winfo reqwidth $entry] if { $entw+8 >= $wmax } { $path:cmd itemconfigure $idw -width $wmax } else { $path:cmd itemconfigure $idw -width 0 }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -