Posted to tcl by jeremy_c at Fri Feb 05 13:39:59 GMT 2010view raw
- namespace eval ttk::combobox {
- proc CompleteEntry {W key} {
- if {[string length $key] > 1 && [string tolower $key] != $key} {return}
- if {[$W cget -state] == "readonly"} {
- set value $key
- } else {
- set text [string map [list {[} {\[} {]} {\]}] [$W get]]
- if {[string equal $text ""]} {return}
- set value $text
- }
- set values [$W cget -values]
- set x [lsearch -nocase $values $value*]
- if {$x < 0} {return}
- set index [$W index insert]
- $W set [lindex $values $x]
- $W icursor $index
- $W selection range insert end
- }
- proc CompleteList {W key} {
- for {set idx 0} {$idx < [$W size]} {incr idx} {
- if {[string match -nocase $key* [$W get $idx]]} {
- $W selection clear 0 end
- $W selection set $idx
- $W activate $idx
- break
- }
- }
- }
- }
- bind ComboboxListbox <KeyPress> { ttk::combobox::CompleteList %W %K }
- bind TCombobox <KeyRelease> { ttk::combobox::CompleteEntry %W %K }
- set values {One Two Three Four Five Six Seven Eight Nine Ten}
- ttk::label .l1 -text Readonly
- ttk::combobox .c1 -state readonly -values $values
- ttk::label .l2 -text Editable
- ttk::combobox .c2 -values $values
- pack .l1 .c1 .l2 .c2
Comments
Posted by patthoyts at Fri Feb 05 14:06:45 GMT 2010 [text] [code]
package require Tk 8.5 namespace eval ttk::combobox {} proc ttk::combobox::CompleteEntry {W key} { if {[string length $key] > 1 && [string tolower $key] != $key} {return} if {[$W cget -state] == "readonly"} { set value $key } else { set value [string map [list \[ \\\[ \] \\\] * \\* ? \\?] [$W get]] if {$value eq ""} {return} } set values [$W cget -values] set x [lsearch -nocase $values $value*] if {$x < 0} {return} set index [$W index insert] $W set [lindex $values $x] $W icursor $index $W selection range insert end } proc ttk::combobox::CompleteList {W key} { for {set idx 0} {$idx < [$W size]} {incr idx} { if {[string match -nocase $key* [$W get $idx]]} { $W selection clear 0 end $W selection set $idx $W activate $idx break } } } bind ComboboxListbox <KeyPress> { ttk::combobox::CompleteList %W %K } bind TCombobox <KeyRelease> { ttk::combobox::CompleteEntry %W %K } puts "argv0: $argv0 script:[info script]" console show if {[info exists argv0] && $argv0 eq [info script]} { set values {aaa bbb c[error] ddd 121 333 [test] ?? *stuff } ttk::label .l1 -text Readonly ttk::combobox .c1 -state readonly -values $values ttk::label .l2 -text Editable ttk::combobox .c2 -values $values ttk::button .exit -text Exit -command {set ::forever 1} wm protocol . WM_DELETE_WINDOW {set ::forever 1} pack .l1 .c1 .l2 .c2 .exit bind . <Control-F2> {console show} vwait ::forever exit }
Posted by jeremy_c at Fri Feb 05 14:35:09 GMT 2010 [text] [code]
package require Tk namespace eval ttk::combobox {} proc ttk::combobox::EscapeKey {key} { switch -- $key { bracketleft { return {\[} } bracketright { return {\]} } asterisk { return {\*} } question { return {\?} } default { return $key } } } proc ttk::combobox::CompleteEntry {W key} { if {[string length $key] > 1 && [string tolower $key] != $key} {return} if {[$W instate readonly]} { set value [EscapeKey $key] } else { set value [string map { {[} {\[} {]} {\]} {?} {\?} {*} {\*} } [$W get]] if {[string equal $value ""]} {return} } set values [$W cget -values] set x [lsearch -nocase $values $value*] if {$x < 0} {return} set index [$W index insert] $W set [lindex $values $x] $W icursor $index $W selection range insert end if {[$W instate readonly]} { event generate $W <<ComboboxSelected>> -when mark } } proc ttk::combobox::CompleteList {W key} { set key [EscapeKey $key] for {set idx 0} {$idx < [$W size]} {incr idx} { if {[string match -nocase $key* [$W get $idx]]} { $W selection clear 0 end $W selection set $idx $W activate $idx break } } } bind ComboboxListbox <KeyPress> { ttk::combobox::CompleteList %W %K } bind TCombobox <KeyRelease> { ttk::combobox::CompleteEntry %W %K } set values {One Two Three Four Five *Hello ?Hello [Hello] Six Seven Eight Nine Ten} ttk::label .l1 -text Readonly ttk::combobox .c1 -state readonly -values $values ttk::label .l2 -text Editable ttk::combobox .c2 -values $values bind .c1 <<ComboboxSelected>> { puts [.c1 get] } bind .c2 <<ComboboxSelected>> { puts [.c2 get] } pack .l1 .c1 .l2 .c2