📄 combobox.itk
字号:
} else { _postList }}# ------------------------------------------------------# PROTECTED METHOD: _unpostList## Unmap the listbox (pop it down).## ------------------------------------------------------itcl::body iwidgets::Combobox::_unpostList {} { # Determine if event occured in the scrolledlistbox and, if it did, # don't unpost it. (A selection in the list unposts it correctly and # in the scrollbar we don't want to unpost it.) set x [winfo x $itk_component(list)] set y [winfo y $itk_component(list)] set w [winfo width $itk_component(list)] set h [winfo height $itk_component(list)] wm withdraw $itk_component(popup) ::grab release $itk_component(popup) # Added by csmith, 12/19/00. Thanks to Erik Leunissen for finding # this problem. We need to restore any previous grabs when the # dropdown listbox is unmapped. if {$_grab(window) != ""} { if {$_grab(status) == "global"} { ::grab -global $_grab(window) } else { ::grab $_grab(window) } set _grab(window) "" set _grab(status) "" } # Added by csmith, 10/26/00. This binding resets the binding # created in _postList - see that method for further details. bind $itk_component(entry) <Unmap> {} set _isPosted false $itk_component(list) selection clear 0 end if {$_currItem != {}} { $itk_component(list) selection set $_currItem $_currItem $itk_component(list) activate $_currItem } switch -- $itk_option(-editable) { 1 - true - yes - on { $itk_component(entry) configure -state normal } 0 - false - no - off { $itk_component(entry) configure -state disabled } } _drawArrow update}# ------------------------------------------------------# PROTECTED METHOD: _commonBindings## Bindings that are used by both simple and dropdown# style Comboboxes.## ------------------------------------------------------itcl::body iwidgets::Combobox::_commonBindings {} { bind $itk_component(entry) <KeyPress-BackSpace> [itcl::code $this _bs] bind $itk_component(entry) <KeyRelease> [itcl::code $this _lookup %K] bind $itk_component(entry) <Down> [itcl::code $this _next] bind $itk_component(entry) <Up> [itcl::code $this _previous] bind $itk_component(entry) <Control-n> [itcl::code $this _next] bind $itk_component(entry) <Control-p> [itcl::code $this _previous] bind [_slbListbox] <Control-n> [itcl::code $this _next] bind [_slbListbox] <Control-p> [itcl::code $this _previous]}# ------------------------------------------------------# PROTECTED METHOD: _dropdownBindings## Bindings used only by the dropdown type Combobox.## ------------------------------------------------------itcl::body iwidgets::Combobox::_dropdownBindings {} { bind $itk_component(popup) <Escape> [itcl::code $this _unpostList] bind $itk_component(popup) <space> \ "[itcl::code $this _stateSelect]; [itcl::code $this _unpostList]" bind $itk_component(popup) <Return> \ "[itcl::code $this _stateSelect]; [itcl::code $this _unpostList]" bind $itk_component(popup) <ButtonRelease-1> \ [itcl::code $this _dropdownBtnRelease %W %x %y] bind $itk_component(list) <Map> \ [itcl::code $this _listShowing 1] bind $itk_component(list) <Unmap> \ [itcl::code $this _listShowing 0] # once in the listbox, we drop on the next release (unless in scrollbar) bind [_slbListbox] <Enter> \ [itcl::code $this _ignoreNextBtnRelease false] bind $itk_component(arrowBtn) <3> [itcl::code $this _next] bind $itk_component(arrowBtn) <Shift-3> [itcl::code $this _previous] bind $itk_component(arrowBtn) <Down> [itcl::code $this _next] bind $itk_component(arrowBtn) <Up> [itcl::code $this _previous] bind $itk_component(arrowBtn) <Control-n> [itcl::code $this _next] bind $itk_component(arrowBtn) <Control-p> [itcl::code $this _previous] bind $itk_component(arrowBtn) <Shift-Down> [itcl::code $this _toggleList] bind $itk_component(arrowBtn) <Shift-Up> [itcl::code $this _toggleList] bind $itk_component(arrowBtn) <Return> [itcl::code $this _toggleList] bind $itk_component(arrowBtn) <space> [itcl::code $this _toggleList] bind $itk_component(entry) <Configure> [itcl::code $this _resizeArrow] bind $itk_component(entry) <Shift-Down> [itcl::code $this _toggleList] bind $itk_component(entry) <Shift-Up> [itcl::code $this _toggleList]}# ------------------------------------------------------# PROTECTED METHOD: _simpleBindings## Bindings used only by the simple type Comboboxes.## ------------------------------------------------------itcl::body iwidgets::Combobox::_simpleBindings {} { bind [_slbListbox] <ButtonRelease-1> [itcl::code $this _stateSelect] bind [_slbListbox] <space> [itcl::code $this _stateSelect] bind [_slbListbox] <Return> [itcl::code $this _stateSelect] bind $itk_component(entry) <Escape> "" bind $itk_component(entry) <Shift-Down> "" bind $itk_component(entry) <Shift-Up> "" bind $itk_component(entry) <Configure> ""}# ------------------------------------------------------# PROTECTED METHOD: _listShowing ?val?## Used instead of "tkwait visibility" to make sure that# the dropdown list is visible. Whenever the list gets# mapped or unmapped, this method is called to keep# track of it. When it is called with the value "-wait",# it waits for the list to be mapped.# ------------------------------------------------------itcl::body iwidgets::Combobox::_listShowing {{val ""}} { if {$val == ""} { return $_listShowing($this) } elseif {$val == "-wait"} { while {!$_listShowing($this)} { tkwait variable [itcl::scope _listShowing($this)] } return } set _listShowing($this) $val}# ------------------------------------------------------# PRIVATE METHOD: _slbListbox## Access the tk listbox window out of the scrolledlistbox.## ------------------------------------------------------itcl::body iwidgets::Combobox::_slbListbox {} { return [$itk_component(list) component listbox]}# ------------------------------------------------------# PRIVATE METHOD: _stateSelect## only allows a B1 release in the listbox to have an effect if -state is# normal.## ------------------------------------------------------itcl::body iwidgets::Combobox::_stateSelect {} { switch -- $itk_option(-state) { normal { [itcl::code $this _selectCmd] } }}# ------------------------------------------------------# PRIVATE METHOD: _bs## A part of the auto-completion code, this function sets a flag when the# Backspace key is hit and there is a selection in the entry field.# Note that it's probably buggy to assume that a selection being present# means that that selection came from auto-completion.## ------------------------------------------------------itcl::body iwidgets::Combobox::_bs {} { # # exit if completion is turned off # switch -- $itk_option(-completion) { 0 - no - false - off { return } } # # critical section flag. it ain't perfect, but for most usage it'll # keep us from being in this code "twice" at the same time # (auto-repeated keystrokes are a pain!) # if {$_inbs} { return } else { set _inbs 1 } # # set the _doit flag if there is a selection set in the entry field # set _doit 0 if [$itk_component(entry) selection present] { set _doit 1 } # # clear the semaphore and return # set _inbs 0}# ------------------------------------------------------# PRIVATE METHOD: _lookup## handles auto-completion of text typed (or insert'd) into the entry field.## ------------------------------------------------------itcl::body iwidgets::Combobox::_lookup {key} { # # Don't process auto-completion stuff if navigation key was released # Fixes SF bug 501300 # if {$_next_prevFLAG} { set _next_prevFLAG 0 return } # # exit if completion is turned off # switch -- $itk_option(-completion) { 0 - no - false - off { return } } # # critical section flag. it ain't perfect, but for most usage it'll # keep us from being in this code "twice" at the same time # (auto-repeated keystrokes are a pain!) # if {$_inlookup} { return } else { set _inlookup 1 } # # if state of megawidget is disabled, or the entry is not editable, # clear the semaphore and exit # if {$itk_option(-state) == "disabled" \ || [lsearch {on 1 true yes} $itk_option(-editable)] == -1} { set _inlookup 0 return } # # okay, *now* we can get to work # the _bs function is called on keyPRESS of BackSpace, and will set # the _doit flag if there's a selection set in the entryfield. If # there is, we're assuming that it's generated by completion itself # (this is probably a Bad Assumption), so we'll want to whack the # selected text, as well as the character immediately preceding the # insertion cursor. # if {$key == "BackSpace"} { if {$_doit} { set first [expr {[$itk_component(entry) index insert] -1}] $itk_component(entry) delete $first end $itk_component(entry) icursor $first } } # # get the text left in the entry field, and its length. if # zero-length, clear the selection in the listbox, clear the # semaphore, and boogie. # set text [get] set len [string length $text] if {$len == 0} { $itk_component(list) selection clear 0 end set _inlookup 0 return } # No need to do lookups for Shift keys or Arrows. The up/down # arrow keys should walk up/down the listbox entries. switch $key { Shift_L - Shift_R - Up - Down - Left - Right { set _inlookup 0 return } default { } } # Added by csmith 12/11/01 to resolve SF ticket #474817. It's an unusual # circumstance, but we need to make sure the character passed into this # method matches the last character in the entry's text string. It's # possible to type fast enough that the _lookup method gets invoked # *after* multiple characters have been typed and *before* the first # character has been processed. For example, you can type "bl" very # quickly, and by the time the interpreter processes "b", the "l" has # already been placed in the entry field. This causes problems as noted # in the SF ticket. # # Thus, if the character currently being processed does not match the # last character in the entry field, reset the _inlookup flag and return. # Also, note that we're only concerned with single characters here, not # keys such as backspace, delete, etc. if {$key != [string range $text end end] && [string match ? $key]} { set _inlookup 0 return } # # okay, so we have to do a lookup. find the first match in the # listbox to the text we've got in the entry field (glob). # if one exists, clear the current listbox selection, and set it to # the one we just found, making that one visible in the listbox. # then, pick off the text from the listbox entry that hadn't yet been # entered into the entry field. we need to tack that text onto the # end of the entry field, select it, and then set the insertion cursor # back to just before the point where we just added that text. # if one didn't exist, then just clear the listbox selection # set item [lsearch [$itk_component(list) get 0 end] "$text*" ] if {$item != -1} { $itk_component(list) selection clear 0 end $itk_component(list) selection set $item $item see $item set remainder [string range [$itk_component(list) get $item] $len end] $itk_component(entry) insert end $remainder $itk_component(entry) selection range $len end $itk_component(entry) icursor $len } else { $itk_component(list) selection clear 0 end } # # clear the semaphore and return # set _inlookup 0 return}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -