📄 tree.tcl
字号:
set data(upd,level) 0 set data(upd,afterid) "" }}# ------------------------------------------------------------------------------# Command Tree::_redraw_selection# ------------------------------------------------------------------------------proc Tree::_redraw_selection { path } { variable $path upvar 0 $path data set selbg [Widget::getoption $path -selectbackground] set selfg [Widget::getoption $path -selectforeground] foreach id [$path:cmd find withtag sel] { set node [string range [lindex [$path:cmd gettags $id] 1] 2 end] $path:cmd itemconfigure "n:$node" -fill [Widget::getoption $path.$node -fill] } $path:cmd delete sel foreach node $data(selnodes) { set bbox [$path:cmd bbox "n:$node"] if { [llength $bbox] } { set id [eval $path:cmd create rectangle $bbox -fill $selbg -outline $selbg -tags [list "sel s:$node"]] $path:cmd itemconfigure "n:$node" -fill $selfg $path:cmd lower $id } }}# ------------------------------------------------------------------------------# Command Tree::_redraw_idle# ------------------------------------------------------------------------------proc Tree::_redraw_idle { path level } { variable $path upvar 0 $path data if { [Widget::getoption $path -redraw] && $data(upd,afterid) == "" } { set data(upd,afterid) [after idle Tree::_redraw_tree $path] } if { $level > $data(upd,level) } { set data(upd,level) $level } return ""}# --------------------------------------------------------------------------------------------# Commandes pour le Drag and Drop# ------------------------------------------------------------------------------# Command Tree::_init_drag_cmd# ------------------------------------------------------------------------------proc Tree::_init_drag_cmd { path X Y top } { set ltags [$path:cmd gettags current] set item [lindex $ltags 0] if { ![string compare $item "node"] || ![string compare $item "img"] || ![string compare $item "win"] } { set node [string range [lindex $ltags 1] 2 end] if { [set cmd [Widget::getoption $path -draginitcmd]] != "" } { return [uplevel \#0 $cmd [list $path $node $top]] } if { [set type [Widget::getoption $path -dragtype]] == "" } { set type "TREE_NODE" } if { [set img [Widget::getoption $path.$node -image]] != "" } { pack [label $top.l -image $img -padx 0 -pady 0] } return [list $type {copy move link} $node] } return {}}# ------------------------------------------------------------------------------# Command Tree::_drop_cmd# ------------------------------------------------------------------------------proc Tree::_drop_cmd { path source X Y op type dnddata } { variable $path upvar 0 $path data $path:cmd delete drop if { [string length $data(dnd,afterid)] } { after cancel $data(dnd,afterid) set data(dnd,afterid) "" } set data(dnd,scroll) "" if { [llength $data(dnd,node)] } { if { [set cmd [Widget::getoption $path -dropcmd]] != "" } { return [uplevel \#0 $cmd [list $path $source $data(dnd,node) $op $type $dnddata]] } } return 0}# ------------------------------------------------------------------------------# Command Tree::_over_cmd# ------------------------------------------------------------------------------proc Tree::_over_cmd { path source event X Y op type dnddata } { variable $path upvar 0 $path data if { ![string compare $event "leave"] } { # we leave the window tree $path:cmd delete drop if { [string length $data(dnd,afterid)] } { after cancel $data(dnd,afterid) set data(dnd,afterid) "" } set data(dnd,scroll) "" return 0 } if { ![string compare $event "enter"] } { # we enter the window tree - dnd data initialization set mode [Widget::getoption $path -dropovermode] set data(dnd,mode) 0 foreach c {w p n} { set data(dnd,mode) [expr {($data(dnd,mode) << 1) | ([string first $c $mode] != -1)}] } set bbox [$path:cmd bbox all] if { [llength $bbox] } { set data(dnd,xs) [lindex $bbox 2] } else { set data(dnd,xs) 0 } set data(dnd,node) {} } set x [expr {$X-[winfo rootx $path]}] set y [expr {$Y-[winfo rooty $path]}] $path:cmd delete drop set data(dnd,node) {} # test for auto-scroll unless mode is widget only if { $data(dnd,mode) != 4 && [_auto_scroll $path $x $y] != "" } { return 2 } if { $data(dnd,mode) & 4 } { # dropovermode includes widget set target [list widget] set vmode 4 } else { set target [list ""] set vmode 0 } set xc [$path:cmd canvasx $x] set xs $data(dnd,xs) if { $xc <= $xs } { set yc [$path:cmd canvasy $y] set dy [$path:cmd cget -yscrollincrement] set line [expr {int($yc/$dy)}] set xi 0 set yi [expr {$line*$dy}] set ys [expr {$yi+$dy}] foreach id [$path:cmd find overlapping $xi $yi $xs $ys] { set ltags [$path:cmd gettags $id] set item [lindex $ltags 0] if { ![string compare $item "node"] || ![string compare $item "img"] || ![string compare $item "win"] } { # item is the label or image/window of the node set node [string range [lindex $ltags 1] 2 end] set xi [expr {[lindex [$path:cmd coords n:$node] 0]-[Widget::getoption $path -padx]}] if { $data(dnd,mode) & 1 } { # dropovermode includes node lappend target $node set vmode [expr {$vmode | 1}] } else { lappend target "" } if { $data(dnd,mode) & 2 } { # dropovermode includes position if { $yc >= $yi+$dy/2 } { # position is after $node if { [Widget::getoption $path.$node -open] && [llength $data($node)] > 1 } { # $node is open and have subnodes # drop position is 0 in children of $node set parent $node set index 0 set xli [expr {$xi-5}] } else { # $node is not open and doesn't have subnodes # drop position is after $node in children of parent of $node set parent [lindex $data($node) 0] set index [lsearch $data($parent) $node] set xli [expr {$xi-[Widget::getoption $path -deltax]-5}] } set yl $ys } else { # position is before $node # drop position is before $node in children of parent of $node set parent [lindex $data($node) 0] set index [expr {[lsearch $data($parent) $node] - 1}] set xli [expr {$xi-[Widget::getoption $path -deltax]-5}] set yl $yi } lappend target [list $parent $index] set vmode [expr {$vmode | 2}] } else { lappend target {} } if { ($vmode & 3) == 3 } { # result have both node and position # we compute what is the preferred method if { $yc-$yi <= 3 || $ys-$yc <= 3 } { lappend target "position" } else { lappend target "node" } } break } } } if { $vmode && [set cmd [Widget::getoption $path -dropovercmd]] != "" } { # user-defined dropover command set res [uplevel \#0 $cmd [list $path $source $target $op $type $dnddata]] set code [lindex $res 0] set newmode 0 if { $code & 1 } { # update vmode set mode [lindex $res 1] if { ($vmode & 1) && ![string compare $mode "node"] } { set newmode 1 } elseif { ($vmode & 2) && ![string compare $mode "position"] } { set newmode 2 } elseif { ($vmode & 4) && ![string compare $mode "widget"] } { set newmode 4 } } set vmode $newmode } else { if { ($vmode & 3) == 3 } { # result have both item and position # we choose the preferred method if { ![string compare [lindex $target 3] "position"] } { set vmode [expr {$vmode & ~1}] } else { set vmode [expr {$vmode & ~2}] } } if { $data(dnd,mode) == 4 || $data(dnd,mode) == 0 } { # dropovermode is widget or empty - recall is not necessary set code 1 } else { set code 3 } } # draw dnd visual following vmode if { $vmode & 1 } { set data(dnd,node) [list "node" [lindex $target 1]] $path:cmd create rectangle $xi $yi $xs $ys -tags drop } elseif { $vmode & 2 } { set data(dnd,node) [concat "position" [lindex $target 2]] $path:cmd create line $xli [expr {$yl-$dy/2}] $xli $yl $xs $yl -tags drop } elseif { $vmode & 4 } { set data(dnd,node) [list "widget"] } else { set code [expr {$code & 2}] } if { $code & 1 } { DropSite::setcursor based_arrow_down } else { DropSite::setcursor dot } return $code}# ------------------------------------------------------------------------------# Command Tree::_auto_scroll# ------------------------------------------------------------------------------proc Tree::_auto_scroll { path x y } { variable $path upvar 0 $path data set xmax [winfo width $path] set ymax [winfo height $path] set scroll {} if { $y <= 6 } { if { [lindex [$path:cmd yview] 0] > 0 } { set scroll [list yview -1] DropSite::setcursor sb_up_arrow } } elseif { $y >= $ymax-6 } { if { [lindex [$path:cmd yview] 1] < 1 } { set scroll [list yview 1] DropSite::setcursor sb_down_arrow } } elseif { $x <= 6 } { if { [lindex [$path:cmd xview] 0] > 0 } { set scroll [list xview -1] DropSite::setcursor sb_left_arrow } } elseif { $x >= $xmax-6 } { if { [lindex [$path:cmd xview] 1] < 1 } { set scroll [list xview 1] DropSite::setcursor sb_right_arrow } } if { [string length $data(dnd,afterid)] && [string compare $data(dnd,scroll) $scroll] } { after cancel $data(dnd,afterid) set data(dnd,afterid) "" } set data(dnd,scroll) $scroll if { [string length $scroll] && ![string length $data(dnd,afterid)] } { set data(dnd,afterid) [after 200 Tree::_scroll $path $scroll] } return $data(dnd,afterid)}# ------------------------------------------------------------------------------# Command Tree::_scroll# ------------------------------------------------------------------------------proc Tree::_scroll { path cmd dir } { variable $path upvar 0 $path data if { ($dir == -1 && [lindex [$path:cmd $cmd] 0] > 0) || ($dir == 1 && [lindex [$path:cmd $cmd] 1] < 1) } { $path:cmd $cmd scroll $dir units set data(dnd,afterid) [after 100 Tree::_scroll $path $cmd $dir] } else { set data(dnd,afterid) "" DropSite::setcursor dot }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -