📄 listbox.tcl
字号:
}# ----------------------------------------------------------------------------# Command ListBox::selection# ----------------------------------------------------------------------------proc ListBox::selection { path cmd args } { variable $path upvar 0 $path data switch -- $cmd { set { set data(selitems) {} foreach item $args { if { [lsearch $data(selitems) $item] == -1 } { if { [lsearch $data(items) $item] != -1 } { lappend data(selitems) $item } } } } add { foreach item $args { if { [lsearch $data(selitems) $item] == -1 } { if { [lsearch $data(items) $item] != -1 } { lappend data(selitems) $item } } } } remove { foreach item $args { if { [set idx [lsearch $data(selitems) $item]] != -1 } { set data(selitems) [lreplace $data(selitems) $idx $idx] } } } clear { set data(selitems) {} } get { return $data(selitems) } includes { return [expr {[lsearch $data(selitems) $args] != -1}] } default { return } } _redraw_idle $path 1}# ----------------------------------------------------------------------------# Command ListBox::exists# ----------------------------------------------------------------------------proc ListBox::exists { path item } { variable $path upvar 0 $path data return [expr {[lsearch $data(items) $item] != -1}]}# ----------------------------------------------------------------------------# Command ListBox::index# ----------------------------------------------------------------------------proc ListBox::index { path item } { variable $path upvar 0 $path data return [lsearch $data(items) $item]}# ----------------------------------------------------------------------------# ListBox::find# Returns the item given a position.# findInfo @x,y ?confine?# lineNumber# ----------------------------------------------------------------------------proc ListBox::find {path findInfo {confine ""}} { variable $path upvar 0 $path widgetData if {[regexp -- {^@([0-9]+),([0-9]+)$} $findInfo match x y]} { set x [$path.c canvasx $x] set y [$path.c canvasy $y] } elseif {[regexp -- {^[0-9]+$} $findInfo lineNumber]} { set dy [Widget::getoption $path -deltay] set y [expr {$dy*($lineNumber+0.5)}] set confine "" } else { return -code error "invalid find spec \"$findInfo\"" } set found 0 set xi 0 foreach xs $widgetData(xlist) { if {$x <= $xs} { foreach id [$path.c find overlapping $xi $y $xs $y] { set ltags [$path.c gettags $id] set item [lindex $ltags 0] if { ![string compare $item "item"] || ![string compare $item "img"] || ![string compare $item "win"] } { # item is the label or image/window of the node set item [string range [lindex $ltags 1] 2 end] set found 1 break } } break } set xi $xs } if {$found} { if {![string compare $confine "confine"]} { # test if x stand inside node bbox set xi [expr {[lindex [$path.c coords n:$item] 0]-[Widget::getoption $path -padx]}] set xs [lindex [$path.c bbox n:$item] 2] if {$x >= $xi && $x <= $xs} { return $item } } else { return $item } } return ""}# ----------------------------------------------------------------------------# Command ListBox::item - deprecated# ----------------------------------------------------------------------------proc ListBox::item { path first {last ""} } { variable $path upvar 0 $path data if { ![string length $last] } { return [lindex $data(items) $first] } else { return [lrange $data(items) $first $last] }}# ----------------------------------------------------------------------------# Command ListBox::items# ----------------------------------------------------------------------------proc ListBox::items { path {first ""} {last ""}} { variable $path upvar 0 $path data if { ![string length $first] } { return $data(items) } if { ![string length $last] } { return [lindex $data(items) $first] } else { return [lrange $data(items) $first $last] }}# ----------------------------------------------------------------------------# Command ListBox::see# ----------------------------------------------------------------------------proc ListBox::see { path item } { variable $path upvar 0 $path data if { [Widget::getoption $path -redraw] && $data(upd,afterid) != "" } { after cancel $data(upd,afterid) _redraw_listbox $path } set idn [$path.c find withtag n:$item] if { $idn != "" } { ListBox::_see $path $idn right ListBox::_see $path $idn left }}# ----------------------------------------------------------------------------# Command ListBox::edit# ----------------------------------------------------------------------------proc ListBox::edit { path item 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_listbox $path } set idn [$path.c find withtag n:$item] if { $idn != "" } { ListBox::_see $path $idn right ListBox::_see $path $idn left set oldfg [$path.c itemcget $idn -fill] set sbg [Widget::getoption $path -selectbackground] set coords [$path.c coords $idn] set x [lindex $coords 0] set y [lindex $coords 1] set bd [expr {[$path.c cget -borderwidth]+[$path.c cget -highlightthickness]}] set w [expr {[winfo width $path] - 2*$bd}] set wmax [expr {[$path.c canvasx $w]-$x}] $path.c itemconfigure $idn -fill [Widget::getoption $path -background] $path.c itemconfigure s:$item -fill {} -outline {} set _edit(text) $text set _edit(wait) 0 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 [_getoption $path $item -foreground] \ -background [Widget::getoption $path -background] \ -selectforeground [Widget::getoption $path -selectforeground] \ -selectbackground $sbg \ -font [_getoption $path $item -font] \ -textvariable ListBox::_edit(text)] pack $ent -ipadx 8 -anchor w set idw [$path.c create window $x $y -window $frame -anchor w] trace variable ListBox::_edit(text) w "ListBox::_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 } bindtags $ent [list $ent Entry] bind $ent <Escape> {set ListBox::_edit(wait) 0} bind $ent <Return> {set ListBox::_edit(wait) 1} if { $clickres == 0 || $clickres == 1 } { bind $frame <Button> "set ListBox::_edit(wait) $clickres" } set ok 0 while { !$ok } { tkwait variable ListBox::_edit(wait) if { !$_edit(wait) || $verifycmd == "" || [uplevel \#0 $verifycmd [list $_edit(text)]] } { set ok 1 } } trace vdelete ListBox::_edit(text) w "ListBox::_update_edit_size $path $ent $idw $wmax" grab release $frame BWidget::focus release $ent destroy $frame $path.c delete $idw $path.c itemconfigure $idn -fill $oldfg $path.c itemconfigure s:$item -fill $sbg -outline $sbg if { $_edit(wait) } { return $_edit(text) } } return ""}# ----------------------------------------------------------------------------# Command ListBox::xview# ----------------------------------------------------------------------------proc ListBox::xview { path args } { return [eval $path.c xview $args]}# ----------------------------------------------------------------------------# Command ListBox::yview# ----------------------------------------------------------------------------proc ListBox::yview { path args } { return [eval $path.c yview $args]}# ----------------------------------------------------------------------------# Command ListBox::_update_edit_size# ----------------------------------------------------------------------------proc ListBox::_update_edit_size { path entry idw wmax args } { set entw [winfo reqwidth $entry] if { $entw >= $wmax } { $path.c itemconfigure $idw -width $wmax } else { $path.c itemconfigure $idw -width 0 }}# ----------------------------------------------------------------------------# Command ListBox::_getoption# Returns the value of option for node. If empty, returned value is those# of the ListBox.# ----------------------------------------------------------------------------proc ListBox::_getoption { path item option } { set value [Widget::getoption $path.$item $option] if {![string length $value]} { set value [Widget::getoption $path $option] } return $value}# ----------------------------------------------------------------------------# Command ListBox::_destroy# ----------------------------------------------------------------------------proc ListBox::_destroy { path } { variable $path upvar 0 $path data if { $data(upd,afterid) != "" } { after cancel $data(upd,afterid) } if { $data(dnd,afterid) != "" } { after cancel $data(dnd,afterid) } foreach item $data(items) { Widget::destroy $path.$item } Widget::destroy $path unset data rename $path {}}# ----------------------------------------------------------------------------# Command ListBox::_see# ----------------------------------------------------------------------------proc ListBox::_see { path idn side } { set bbox [$path.c bbox $idn] set scrl [$path.c cget -scrollregion] set ymax [lindex $scrl 3] set dy [$path.c cget -yscrollincrement] set yv [$path.c yview] set yv0 [expr {round([lindex $yv 0]*$ymax/$dy)}] set yv1 [expr {round([lindex $yv 1]*$ymax/$dy)}] set y [expr {int([lindex [$path.c coords $idn] 1]/$dy)}] if { $y < $yv0 } { $path.c yview scroll [expr {$y-$yv0}] units } elseif { $y >= $yv1 } { $path.c yview scroll [expr {$y-$yv1+1}] units } set xmax [lindex $scrl 2] set dx [$path.c cget -xscrollincrement] set xv [$path.c xview] if { ![string compare $side "right"] } { set xv1 [expr {round([lindex $xv 1]*$xmax/$dx)}] set x1 [expr {int([lindex $bbox 2]/$dx)}] if { $x1 >= $xv1 } { $path.c xview scroll [expr {$x1-$xv1+1}] units } } else { set xv0 [expr {round([lindex $xv 0]*$xmax/$dx)}] set x0 [expr {int([lindex $bbox 0]/$dx)}] if { $x0 < $xv0 } { $path.c xview scroll [expr {$x0-$xv0}] units } }}# ----------------------------------------------------------------------------# Command ListBox::_update_scrollregion# ----------------------------------------------------------------------------proc ListBox::_update_scrollregion { path } { set bd [expr {2*([$path.c cget -borderwidth]+[$path.c cget -highlightthickness])}] set w [expr {[winfo width $path] - $bd}] set h [expr {[winfo height $path] - $bd}] set xinc [$path.c cget -xscrollincrement] set yinc [$path.c cget -yscrollincrement] set bbox [$path.c bbox all] if { [llength $bbox] } { set xs [lindex $bbox 2] set ys [lindex $bbox 3] if { $w < $xs } { set w [expr {int($xs)}] if { [set r [expr {$w % $xinc}]] } { set w [expr {$w+$xinc-$r}] } } if { $h < $ys } { set h [expr {int($ys)}] if { [set r [expr {$h % $yinc}]] } { set h [expr {$h+$yinc-$r}] } } } $path.c configure -scrollregion [list 0 0 $w $h]}# ----------------------------------------------------------------------------# Command ListBox::_draw_item# ----------------------------------------------------------------------------proc ListBox::_draw_item { path item x0 x1 y } { set indent [Widget::getoption $path.$item -indent] $path.c create text [expr {$x1+$indent}] $y \ -text [Widget::getoption $path.$item -text] \ -fill [_getoption $path $item -foreground] \ -font [_getoption $path $item -font] \ -anchor w \ -tags "item n:$item" if { [set win [Widget::getoption $path.$item -window]] != "" } { $path.c create window [expr {$x0+$indent}] $y \ -window $win -anchor w -tags "win i:$item" } elseif { [set img [Widget::getoption $path.$item -image]] != "" } { $path.c create image [expr {$x0+$indent}] $y \ -image $img -anchor w -tags "img i:$item" }}# ----------------------------------------------------------------------------# Command ListBox::_redraw_items# ----------------------------------------------------------------------------proc ListBox::_redraw_items { path } { variable $path upvar 0 $path data set cursor [$path.c cget -cursor] $path.c configure -cursor watch set dx [Widget::getoption $path -deltax] set dy [Widget::getoption $path -deltay] set padx [Widget::getoption $path -padx] set y0 [expr {$dy/2}] set x0 4 set x1 [expr {$x0+$padx}] set nitem 0 set drawn {} set data(xlist) {} if { [Widget::getoption $path -multicolumn] } { set nrows $data(nrows) } else { set nrows [llength $data(items)] } foreach item $data(upd,delete) { $path.c delete i:$item n:$item s:$item } foreach item $data(items) { if { [info exists data(upd,create,$item)] } { _draw_item $path $item $x0 $x1 $y0 unset data(upd,create,$item)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -