1# entry3.tcl -- 2# 3# This demonstration script creates several entry widgets whose 4# permitted input is constrained in some way. It also shows off a 5# password entry. 6# 7# RCS: @(#) $Id$ 8 9if {![info exists widgetDemo]} { 10 error "This script should be run from the \"widget\" demo." 11} 12 13package require Tk 14 15set w .entry3 16catch {destroy $w} 17toplevel $w 18wm title $w "Constrained Entry Demonstration" 19wm iconname $w "entry3" 20positionWindow $w 21 22label $w.msg -font $font -wraplength 5i -justify left -text "Four different\ 23 entries are displayed below. You can add characters by pointing,\ 24 clicking and typing, though each is constrained in what it will\ 25 accept. The first only accepts 32-bit integers or the empty string\ 26 (checking when focus leaves it) and will flash to indicate any\ 27 problem. The second only accepts strings with fewer than ten\ 28 characters and sounds the bell when an attempt to go over the limit\ 29 is made. The third accepts US phone numbers, mapping letters to\ 30 their digit equivalent and sounding the bell on encountering an\ 31 illegal character or if trying to type over a character that is not\ 32 a digit. The fourth is a password field that accepts up to eight\ 33 characters (silently ignoring further ones), and displaying them as\ 34 asterisk characters." 35 36## See Code / Dismiss buttons 37set btns [addSeeDismiss $w.buttons $w] 38pack $btns -side bottom -fill x 39 40# focusAndFlash -- 41# Error handler for entry widgets that forces the focus onto the 42# widget and makes the widget flash by exchanging the foreground and 43# background colours at intervals of 200ms (i.e. at approximately 44# 2.5Hz). 45# 46# Arguments: 47# W - Name of entry widget to flash 48# fg - Initial foreground colour 49# bg - Initial background colour 50# count - Counter to control the number of times flashed 51 52proc focusAndFlash {W fg bg {count 9}} { 53 focus -force $W 54 if {$count<1} { 55 $W configure -foreground $fg -background $bg 56 } else { 57 if {$count%2} { 58 $W configure -foreground $bg -background $fg 59 } else { 60 $W configure -foreground $fg -background $bg 61 } 62 after 200 [list focusAndFlash $W $fg $bg [expr {$count-1}]] 63 } 64} 65 66labelframe $w.l1 -text "Integer Entry" 67# Alternatively try using {string is digit} for arbitrary length numbers, 68# and not just 32-bit ones. 69entry $w.l1.e -validate focus -vcmd {string is integer %P} 70$w.l1.e configure -invalidcommand \ 71 "focusAndFlash %W [$w.l1.e cget -fg] [$w.l1.e cget -bg]" 72pack $w.l1.e -fill x -expand 1 -padx 1m -pady 1m 73 74labelframe $w.l2 -text "Length-Constrained Entry" 75entry $w.l2.e -validate key -invcmd bell -vcmd {expr {[string length %P]<10}} 76pack $w.l2.e -fill x -expand 1 -padx 1m -pady 1m 77 78### PHONE NUMBER ENTRY ### 79# Note that the source to this is quite a bit longer as the behaviour 80# demonstrated is a lot more ambitious than with the others. 81 82# Initial content for the third entry widget 83set entry3content "1-(000)-000-0000" 84# Mapping from alphabetic characters to numbers. This is probably 85# wrong, but it is the only mapping I have; the UK doesn't really go 86# for associating letters with digits for some reason. 87set phoneNumberMap {} 88foreach {chars digit} {abc 2 def 3 ghi 4 jkl 5 mno 6 pqrs 7 tuv 8 wxyz 9} { 89 foreach char [split $chars ""] { 90 lappend phoneNumberMap $char $digit [string toupper $char] $digit 91 } 92} 93 94# validatePhoneChange -- 95# Checks that the replacement (mapped to a digit) of the given 96# character in an entry widget at the given position will leave a 97# valid phone number in the widget. 98# 99# W - The entry widget to validate 100# vmode - The widget's validation mode 101# idx - The index where replacement is to occur 102# char - The character (or string, though that will always be 103# refused) to be overwritten at that point. 104 105proc validatePhoneChange {W vmode idx char} { 106 global phoneNumberMap entry3content 107 if {$idx == -1} {return 1} 108 after idle [list $W configure -validate $vmode -invcmd bell] 109 if { 110 !($idx<3 || $idx==6 || $idx==7 || $idx==11 || $idx>15) && 111 [string match {[0-9A-Za-z]} $char] 112 } then { 113 $W delete $idx 114 $W insert $idx [string map $phoneNumberMap $char] 115 after idle [list phoneSkipRight $W -1] 116 return 1 117 } 118 return 0 119} 120 121# phoneSkipLeft -- 122# Skip over fixed characters in a phone-number string when moving left. 123# 124# Arguments: 125# W - The entry widget containing the phone-number. 126 127proc phoneSkipLeft {W} { 128 set idx [$W index insert] 129 if {$idx == 8} { 130 # Skip back two extra characters 131 $W icursor [incr idx -2] 132 } elseif {$idx == 7 || $idx == 12} { 133 # Skip back one extra character 134 $W icursor [incr idx -1] 135 } elseif {$idx <= 3} { 136 # Can't move any further 137 bell 138 return -code break 139 } 140} 141 142# phoneSkipRight -- 143# Skip over fixed characters in a phone-number string when moving right. 144# 145# Arguments: 146# W - The entry widget containing the phone-number. 147# add - Offset to add to index before calculation (used by validation.) 148 149proc phoneSkipRight {W {add 0}} { 150 set idx [$W index insert] 151 if {$idx+$add == 5} { 152 # Skip forward two extra characters 153 $W icursor [incr idx 2] 154 } elseif {$idx+$add == 6 || $idx+$add == 10} { 155 # Skip forward one extra character 156 $W icursor [incr idx] 157 } elseif {$idx+$add == 15 && !$add} { 158 # Can't move any further 159 bell 160 return -code break 161 } 162} 163 164labelframe $w.l3 -text "US Phone-Number Entry" 165entry $w.l3.e -validate key -invcmd bell -textvariable entry3content \ 166 -vcmd {validatePhoneChange %W %v %i %S} 167# Click to focus goes to the first editable character... 168bind $w.l3.e <FocusIn> { 169 if {"%d" ne "NotifyAncestor"} { 170 %W icursor 3 171 after idle {%W selection clear} 172 } 173} 174bind $w.l3.e <Left> {phoneSkipLeft %W} 175bind $w.l3.e <Right> {phoneSkipRight %W} 176pack $w.l3.e -fill x -expand 1 -padx 1m -pady 1m 177 178labelframe $w.l4 -text "Password Entry" 179entry $w.l4.e -validate key -show "*" -vcmd {expr {[string length %P]<=8}} 180pack $w.l4.e -fill x -expand 1 -padx 1m -pady 1m 181 182lower [frame $w.mid] 183grid $w.l1 $w.l2 -in $w.mid -padx 3m -pady 1m -sticky ew 184grid $w.l3 $w.l4 -in $w.mid -padx 3m -pady 1m -sticky ew 185grid columnconfigure $w.mid {0 1} -uniform 1 186pack $w.msg -side top 187pack $w.mid -fill both -expand 1 188