Posted to tcl by Stu at Sat Jan 29 11:35:44 GMT 2022view raw
- bind TCombobox <Key> [list comboKey %W %A]
- proc comboKey {combo key} {
- if {$key eq ""} { return }
- if {[set curIdx [$combo current]] == {}} { return }
- if {[set newIdx [getNextOrPrevMatch [$combo cget -values] $key $curIdx [string is upper $key]]] == -1} { return }
- comboGo $combo $newIdx
- }
- proc comboGo {combo where} {
- if {[set curIdx [$combo current]] == {}} { return }
- set nitems [llength [$combo cget -values]]
- incr nitems -1
- set idx $curIdx
- switch -exact -- $where {
- home { set idx 0 }
- end { set idx $nitems }
- next { incr idx }
- prior { incr idx -1 }
- default { set idx $where }
- }; # hctiws
- if {$idx < 0 || $idx > $nitems || $idx == $curIdx} { return }
- $combo current $idx
- event generate $combo <<SelectAll>>
- event generate $combo <<ComboboxSelected>>
- }
- proc getNextOrPrevMatch {aList aString {curIdx 0} {backwards no}} {
- set idxs [lsearch -all -nocase $aList $aString*]
- if {[llength $idxs] == 0} { return -1 }
- if {[llength $idxs] == 1 && [lindex $idxs 0] == $curIdx} { return -1 }
- set p -1
- foreach idx $idxs {
- if {$idx < $curIdx} { set p $idx }
- if {$idx > $curIdx} { break }
- }
- if {$backwards} {
- if {$p == -1} {
- set newIdx [lindex $idxs end]
- } else {
- set newIdx $p
- }
- } else {
- if {$idx == $curIdx} {
- set newIdx [lindex $idxs 0]
- } else {
- set newIdx $idx
- }
- }
- return $newIdx
- }