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