📄 combobox.tcl
字号:
if {$exact && $exactMatch == -1} { # this means we didn't find an exact match $widgets(listbox) selection clear 0 end $widgets(listbox) see $index } elseif {!$exact} { # this means we found something, but it isn't an exact # match. If we find something that *is* an exact match we # don't need to do the following, since it would merely # be replacing the data in the entry widget with itself set oldstate [$widgets(entry) cget -state] $widgets(entry) configure -state normal $widgets(entry) delete 0 end $widgets(entry) insert end $thisItem $widgets(entry) selection clear $widgets(entry) selection range $marker end $widgets(listbox) activate $index $widgets(listbox) selection clear 0 end $widgets(listbox) selection anchor $index $widgets(listbox) selection set $index $widgets(listbox) see $index $widgets(entry) configure -state $oldstate }}# ::combobox::Select --## selects an item from the list and sets the value of the combobox# to that value## Arguments:## w widget pathname# index listbox index of item to be selected## Returns:## empty stringproc ::combobox::Select {w index} { upvar ::combobox::${w}::widgets widgets upvar ::combobox::${w}::options options # the catch is because I'm sloppy -- presumably, the only time # an error will be caught is if there is no selection. if {![catch {set data [$widgets(listbox) get [lindex $index 0]]}]} { ::combobox::SetValue $widgets(this) $data $widgets(listbox) selection clear 0 end $widgets(listbox) selection anchor $index $widgets(listbox) selection set $index } $widgets(entry) selection range 0 end $widgets(entry) icursor end $widgets(this) close return ""}# ::combobox::HandleScrollbar --# # causes the scrollbar of the dropdown list to appear or disappear# based on the contents of the dropdown listbox## Arguments:## w widget pathname# action the action to perform on the scrollbar## Returns:## an empty stringproc ::combobox::HandleScrollbar {w {action "unknown"}} { upvar ::combobox::${w}::widgets widgets upvar ::combobox::${w}::options options if {$options(-height) == 0} { set hlimit $options(-maxheight) } else { set hlimit $options(-height) } switch $action { "grow" { if {$hlimit > 0 && [$widgets(listbox) size] > $hlimit} { pack forget $widgets(listbox) pack $widgets(vsb) -side right -fill y -expand n pack $widgets(listbox) -side left -fill both -expand y } } "shrink" { if {$hlimit > 0 && [$widgets(listbox) size] <= $hlimit} { pack forget $widgets(vsb) } } "crop" { # this means the window was cropped and we definitely # need a scrollbar no matter what the user wants pack forget $widgets(listbox) pack $widgets(vsb) -side right -fill y -expand n pack $widgets(listbox) -side left -fill both -expand y } default { if {$hlimit > 0 && [$widgets(listbox) size] > $hlimit} { pack forget $widgets(listbox) pack $widgets(vsb) -side right -fill y -expand n pack $widgets(listbox) -side left -fill both -expand y } else { pack forget $widgets(vsb) } } } return ""}# ::combobox::ComputeGeometry --## computes the geometry of the dropdown list based on the size of the# combobox...## Arguments:## w widget pathname## Returns:## the desired geometry of the listboxproc ::combobox::ComputeGeometry {w} { upvar ::combobox::${w}::widgets widgets upvar ::combobox::${w}::options options if {$options(-height) == 0 && $options(-maxheight) != "0"} { # if this is the case, count the items and see if # it exceeds our maxheight. If so, set the listbox # size to maxheight... set nitems [$widgets(listbox) size] if {$nitems > $options(-maxheight)} { # tweak the height of the listbox $widgets(listbox) configure -height $options(-maxheight) } else { # un-tweak the height of the listbox $widgets(listbox) configure -height 0 } update idletasks } # compute height and width of the dropdown list set bd [$widgets(dropdown) cget -borderwidth] set height [expr {[winfo reqheight $widgets(dropdown)] + $bd + $bd}] if {[string length $options(-dropdownwidth)] == 0 || $options(-dropdownwidth) == 0} { set width [winfo width $widgets(this)] } else { set m [font measure [$widgets(listbox) cget -font] "m"] set width [expr {$options(-dropdownwidth) * $m}] } # figure out where to place it on the screen, trying to take into # account we may be running under some virtual window manager set screenWidth [winfo screenwidth $widgets(this)] set screenHeight [winfo screenheight $widgets(this)] set rootx [winfo rootx $widgets(this)] set rooty [winfo rooty $widgets(this)] set vrootx [winfo vrootx $widgets(this)] set vrooty [winfo vrooty $widgets(this)] # the x coordinate is simply the rootx of our widget, adjusted for # the virtual window. We won't worry about whether the window will # be offscreen to the left or right -- we want the illusion that it # is part of the entry widget, so if part of the entry widget is off- # screen, so will the list. If you want to change the behavior, # simply change the if statement... (and be sure to update this # comment!) set x [expr {$rootx + $vrootx}] if {0} { set rightEdge [expr {$x + $width}] if {$rightEdge > $screenWidth} { set x [expr {$screenWidth - $width}] } if {$x < 0} {set x 0} } # the y coordinate is the rooty plus vrooty offset plus # the height of the static part of the widget plus 1 for a # tiny bit of visual separation... set y [expr {$rooty + $vrooty + [winfo reqheight $widgets(this)] + 1}] set bottomEdge [expr {$y + $height}] if {$bottomEdge >= $screenHeight} { # ok. Fine. Pop it up above the entry widget isntead of # below. set y [expr {($rooty - $height - 1) + $vrooty}] if {$y < 0} { # this means it extends beyond our screen. How annoying. # Now we'll try to be real clever and either pop it up or # down, depending on which way gives us the biggest list. # then, we'll trim the list to fit and force the use of # a scrollbar # (sadly, for windows users this measurement doesn't # take into consideration the height of the taskbar, # but don't blame me -- there isn't any way to detect # it or figure out its dimensions. The same probably # applies to any window manager with some magic windows # glued to the top or bottom of the screen) if {$rooty > [expr {$screenHeight / 2}]} { # we are in the lower half of the screen -- # pop it up. Y is zero; that parts easy. The height # is simply the y coordinate of our widget, minus # a pixel for some visual separation. The y coordinate # will be the topof the screen. set y 1 set height [expr {$rooty - 1 - $y}] } else { # we are in the upper half of the screen -- # pop it down set y [expr {$rooty + $vrooty + \ [winfo reqheight $widgets(this)] + 1}] set height [expr {$screenHeight - $y}] } # force a scrollbar HandleScrollbar $widgets(this) crop } } if {$y < 0} { # hmmm. Bummer. set y 0 set height $screenheight } set geometry [format "=%dx%d+%d+%d" $width $height $x $y] return $geometry}# ::combobox::DoInternalWidgetCommand --## perform an internal widget command, then mung any error results# to look like it came from our megawidget. A lot of work just to# give the illusion that our megawidget is an atomic widget## Arguments:## w widget pathname# subwidget pathname of the subwidget # command subwidget command to be executed# args arguments to the command## Returns:## The result of the subwidget command, or an errorproc ::combobox::DoInternalWidgetCommand {w subwidget command args} { upvar ::combobox::${w}::widgets widgets upvar ::combobox::${w}::options options set subcommand $command set command [concat $widgets($subwidget) $command $args] if {[catch $command result]} { # replace the subwidget name with the megawidget name regsub $widgets($subwidget) $result $widgets(this) result # replace specific instances of the subwidget command # with our megawidget command switch $subwidget,$subcommand { listbox,index {regsub "index" $result "list index" result} listbox,insert {regsub "insert" $result "list insert" result} listbox,delete {regsub "delete" $result "list delete" result} listbox,get {regsub "get" $result "list get" result} listbox,size {regsub "size" $result "list size" result} } error $result } else { return $result }}# ::combobox::WidgetProc --## This gets uses as the widgetproc for an combobox widget. # Notice where the widget is created and you'll see that the# actual widget proc merely evals this proc with all of the# arguments intact.## Note that some widget commands are defined "inline" (ie:# within this proc), and some do most of their work in # separate procs. This is merely because sometimes it was# easier to do it one way or the other.## Arguments:## w widget pathname# command widget subcommand# args additional arguments; varies with the subcommand## Results:## Performs the requested widget commandproc ::combobox::WidgetProc {w command args} { upvar ::combobox::${w}::widgets widgets upvar ::combobox::${w}::options options upvar ::combobox::${w}::oldFocus oldFocus upvar ::combobox::${w}::oldFocus oldGrab set command [::combobox::Canonize $w command $command] # this is just shorthand notation... set doWidgetCommand \ [list ::combobox::DoInternalWidgetCommand $widgets(this)] if {$command == "list"} { # ok, the next argument is a list command; we'll # rip it from args and append it to command to # create a unique internal command # # NB: because of the sloppy way we are doing this, # we'll also let the user enter our secret command # directly (eg: listinsert, listdelete), but we # won't document that fact set command "list-[lindex $args 0]" set args [lrange $args 1 end] } set result "" # many of these commands are just synonyms for specific # commands in one of the subwidgets. We'll get them out # of the way first, then do the custom commands. switch $command { bbox - delete - get - icursor - index - insert - scan - selection - xview { set result [eval $doWidgetCommand entry $command $args] } list-get {set result [eval $doWidgetCommand listbox get $args]} list-index {set result [eval $doWidgetCommand listbox index $args]} list-size {set result [eval $doWidgetCommand listbox size $args]} entryset { # update the entry field without invoking the command ::combobox::SetValue $widgets(this) [lindex $args 0] 0 } select { if {[llength $args] == 1} { set index [lindex $args 0] set result [Select $widgets(this) $index] } else { error "usage: $w select index" } } subwidget { set knownWidgets [list button entry listbox dropdown vsb] if {[llength $args] == 0} { return $knownWidgets } set name [lindex $args 0] if {[lsearch $knownWidgets $name] != -1} { set result $widgets($name) } else { error "unknown subwidget $name" } } curselection { set result [eval $doWidgetCommand listbox curselection] } list-insert { eval $doWidgetCommand listbox insert $args set result [HandleScrollbar $w "grow"] } list-delete { eval $doWidgetCommand listbox delete $args set result [HandleScrollbar $w "shrink"] } toggle { # ignore this command if the widget is disabled... if {$options(-state) == "disabled"} return # pops down the list if it is not, hides it # if it is... if {[winfo ismapped $widgets(dropdown)]} { set result [$widgets(this) close] } else { set result [$widgets(this) open] } } open { # if this is an editable combobox, the focus should # be set to the entry widget if {$options(-editable)} { focus $widgets(entry) $widgets(entry) select range 0 end $widgets(entry) icursor end } # if we are disabled, we won't allow this to happen if {$options(-state) == "disabled"} { return 0 } # if there is a -opencommand, execute it now if {[string length $options(-opencommand)] > 0} { # hmmm... should I do a catch, or just let the normal # error handling handle any errors? For now, the latter... uplevel \#0 $options(-opencommand) } # compute the geometry of the window to pop up, and set # it, and force the window manager to take notice
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -