📄 listbox.tcl
字号:
} else { set indent [Widget::getoption $path.$item -indent] $path.c coords n:$item [expr {$x1+$indent}] $y0 $path.c coords i:$item [expr {$x0+$indent}] $y0 } incr y0 $dy incr nitem lappend drawn n:$item if { $nitem == $nrows } { set y0 [expr {$dy/2}] set bbox [eval $path.c bbox $drawn] set drawn {} set x0 [expr {[lindex $bbox 2]+$dx}] set x1 [expr {$x0+$padx}] set nitem 0 lappend data(xlist) [lindex $bbox 2] } } if { $nitem && $nitem < $nrows } { set bbox [eval $path.c bbox $drawn] lappend data(xlist) [lindex $bbox 2] } set data(upd,delete) {} $path.c configure -cursor $cursor}# ----------------------------------------------------------------------------# Command ListBox::_redraw_selection# ----------------------------------------------------------------------------proc ListBox::_redraw_selection { path } { variable $path upvar 0 $path data set selbg [Widget::getoption $path -selectbackground] set selfg [Widget::getoption $path -selectforeground] foreach id [$path.c find withtag sel] { set item [string range [lindex [$path.c gettags $id] 1] 2 end] $path.c itemconfigure "n:$item" -fill [_getoption $path $item -foreground] } $path.c delete sel foreach item $data(selitems) { set bbox [$path.c bbox "n:$item"] if { [llength $bbox] } { set id [eval $path.c create rectangle $bbox \ -fill $selbg -outline $selbg -tags [list "sel s:$item"]] $path.c itemconfigure "n:$item" -fill $selfg $path.c lower $id } }}# ----------------------------------------------------------------------------# Command ListBox::_redraw_listbox# ----------------------------------------------------------------------------proc ListBox::_redraw_listbox { path } { variable $path upvar 0 $path data if { [Widget::getoption $path -redraw] } { if { $data(upd,level) == 2 } { _redraw_items $path } _redraw_selection $path _update_scrollregion $path set data(upd,level) 0 set data(upd,afterid) "" }}# ----------------------------------------------------------------------------# Command ListBox::_redraw_idle# ----------------------------------------------------------------------------proc ListBox::_redraw_idle { path level } { variable $path upvar 0 $path data if { $data(nrows) != -1 } { # widget is realized if { [Widget::getoption $path -redraw] && $data(upd,afterid) == "" } { set data(upd,afterid) [after idle ListBox::_redraw_listbox $path] } } if { $level > $data(upd,level) } { set data(upd,level) $level } return ""}# ----------------------------------------------------------------------------# Command ListBox::_resize# ----------------------------------------------------------------------------proc ListBox::_resize { path } { variable $path upvar 0 $path data if { [Widget::getoption $path -multicolumn] } { set bd [expr {[$path.c cget -borderwidth]+[$path.c cget -highlightthickness]}] set h [expr {[winfo height $path] - 2*$bd}] set nrows [expr {$h/[$path.c cget -yscrollincrement]}] if { $nrows == 0 } { set nrows 1 } if { $nrows != $data(nrows) } { set data(nrows) $nrows _redraw_idle $path 2 } else { _update_scrollregion $path } } elseif { $data(nrows) == -1 } { # first Configure event set data(nrows) 0 ListBox::_redraw_listbox $path } else { _update_scrollregion $path }}# ----------------------------------------------------------------------------# Command ListBox::_init_drag_cmd# ----------------------------------------------------------------------------proc ListBox::_init_drag_cmd { path X Y top } { set path [winfo parent $path] set ltags [$path.c gettags current] set item [lindex $ltags 0] if { ![string compare $item "item"] || ![string compare $item "img"] || ![string compare $item "win"] } { set item [string range [lindex $ltags 1] 2 end] if { [set cmd [Widget::getoption $path -draginitcmd]] != "" } { return [uplevel \#0 $cmd [list $path $item $top]] } if { [set type [Widget::getoption $path -dragtype]] == "" } { set type "LISTBOX_ITEM" } if { [set img [Widget::getoption $path.$item -image]] != "" } { pack [label $top.l -image $img -padx 0 -pady 0] } return [list $type {copy move link} $item] } return {}}# ----------------------------------------------------------------------------# Command ListBox::_drop_cmd# ----------------------------------------------------------------------------proc ListBox::_drop_cmd { path source X Y op type dnddata } { set path [winfo parent $path] variable $path upvar 0 $path data if { [string length $data(dnd,afterid)] } { after cancel $data(dnd,afterid) set data(dnd,afterid) "" } $path.c delete drop set data(dnd,scroll) "" if { [llength $data(dnd,item)] || ![llength $data(items)] } { if { [set cmd [Widget::getoption $path -dropcmd]] != "" } { return [uplevel \#0 $cmd [list $path $source $data(dnd,item) $op $type $dnddata]] } } return 0}# ----------------------------------------------------------------------------# Command ListBox::_over_cmd# ----------------------------------------------------------------------------proc ListBox::_over_cmd { path source event X Y op type dnddata } { set path [winfo parent $path] variable $path upvar 0 $path data if { ![string compare $event "leave"] } { # we leave the window listbox $path.c 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 listbox - dnd data initialization set mode [Widget::getoption $path -dropovermode] set data(dnd,mode) 0 foreach c {w p i} { set data(dnd,mode) [expr {($data(dnd,mode) << 1) | ([string first $c $mode] != -1)}] } } set x [expr {$X-[winfo rootx $path]}] set y [expr {$Y-[winfo rooty $path]}] $path.c delete drop set data(dnd,item) "" # 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 } if { ($data(dnd,mode) & 2) && ![llength $data(items)] } { # dropovermode includes position and listbox is empty lappend target "" 0 set vmode [expr {$vmode | 2}] } if { ($data(dnd,mode) & 3) && [llength $data(items)]} { # dropovermode includes item or position # we extract the box (xi,yi,xs,ys) where we can find item around x,y set len [llength $data(items)] set xc [$path.c canvasx $x] set yc [$path.c canvasy $y] set dy [$path.c cget -yscrollincrement] set line [expr {int($yc/$dy)}] set yi [expr {$line*$dy}] set ys [expr {$yi+$dy}] set xi 0 set pos $line if { [Widget::getoption $path -multicolumn] } { set nrows $data(nrows) } else { set nrows $len } if { $line < $nrows } { foreach xs $data(xlist) { if { $xc <= $xs } { break } set xi $xs incr pos $nrows } if { $pos < $len } { set item [lindex $data(items) $pos] set xi [expr {[lindex [$path.c coords n:$item] 0]-[Widget::getoption $path -padx]-1}] if { $data(dnd,mode) & 1 } { # dropovermode includes item lappend target $item set vmode [expr {$vmode | 1}] } else { lappend target "" } if { $data(dnd,mode) & 2 } { # dropovermode includes position if { $yc >= $yi+$dy/2 } { # position is after $item incr pos set yl $ys } else { # position is before $item set yl $yi } lappend target $pos set vmode [expr {$vmode | 2}] } else { lappend target "" } } else { lappend target "" "" } } else { lappend target "" "" } if { ($vmode & 3) == 3 } { # result have both item and position # we compute what is the preferred method if { $yc-$yi <= 3 || $ys-$yc <= 3 } { lappend target "position" } else { lappend target "item" } } } if { $vmode && [set cmd [Widget::getoption $path -dropovercmd]] != "" } { # user-defined dropover command set res [uplevel \#0 $cmd [list $source $target $op $type $dnddata]] set code [lindex $res 0] set vmode 0 if {$code & 1} { # update vmode switch -exact -- [lindex $res 1] { item {set vmode 1} position {set vmode 2} widget {set vmode 4} } } } 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 {[llength $data(items)]} { if { $vmode & 1 } { set data(dnd,item) [list "item" [lindex $target 1]] $path.c create rectangle $xi $yi $xs $ys -tags drop } elseif { $vmode & 2 } { set data(dnd,item) [concat "position" [lindex $target 2]] $path.c create line $xi $yl $xs $yl -tags drop } elseif { $vmode & 4 } { set data(dnd,item) [list "widget"] } else { set code [expr {$code & 2}] } } if { $code & 1 } { DropSite::setcursor based_arrow_down } else { DropSite::setcursor dot } return $code}# ----------------------------------------------------------------------------# Command ListBox::_auto_scroll# ----------------------------------------------------------------------------proc ListBox::_auto_scroll { path x y } { variable $path upvar 0 $path data}# ------------------------------------------------------------------------------# Command ListBox::_multiple_select# ------------------------------------------------------------------------------proc ListBox::_multiple_select { path mode idx } { variable $path upvar 0 $path data if { ![info exists data(anchor)] || ![info exists data(sel_anchor)] } { set data(anchor) $idx set data(sel_anchor) {} } switch -exact -- $mode { n { $path selection set $idx set data(anchor) $idx set data(sel_anchor) {} } c { set l [$path selection get] if { [lsearch -exact $l $idx] >= 0 } { $path selection remove $idx } else { $path selection add $idx } set data(anchor) $idx set data(sel_anchor) {} } s { eval $path selection remove $data(sel_anchor) if { $idx > $data(anchor) } { set istart $data(anchor) set iend $idx } else { set istart $idx set iend $data(anchor) } for { set i $istart } { $i <= $iend } { incr i } { set l [$path selection get] set li [lsearch -exact $l $i] if { $li < 0 } { $path selection add $i lappend data(sel_anchor) $i } } } } set xmax [winfo width $path] set ymax [winfo height $path] set scroll {} if { $y <= 6 } { if { [lindex [$path.c yview] 0] > 0 } { set scroll [list yview -1] DropSite::setcursor sb_up_arrow } } elseif { $y >= $ymax-6 } { if { [lindex [$path.c yview] 1] < 1 } { set scroll [list yview 1] DropSite::setcursor sb_down_arrow } } elseif { $x <= 6 } { if { [lindex [$path.c xview] 0] > 0 } { set scroll [list xview -1] DropSite::setcursor sb_left_arrow } } elseif { $x >= $xmax-6 } { if { [lindex [$path.c 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 { [llength $scroll] && ![string length $data(dnd,afterid)] } { set data(dnd,afterid) [after 200 ListBox::_scroll $path $scroll] } return $data(dnd,afterid)}# ----------------------------------------------------------------------------# Command ListBox::_scroll# ----------------------------------------------------------------------------proc ListBox::_scroll { path cmd dir } { variable $path upvar 0 $path data if { ($dir == -1 && [lindex [$path.c $cmd] 0] > 0) || ($dir == 1 && [lindex [$path.c $cmd] 1] < 1) } { $path $cmd scroll $dir units set data(dnd,afterid) [after 100 ListBox::_scroll $path $cmd $dir] } else { set data(dnd,afterid) "" DropSite::setcursor dot }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -