Update tk to version 8.5.5
[msysgit.git] / mingw / lib / tk8.5 / demos / entry3.tcl
blob2e54b05bd95dd77364b9b49aa7ad661479d19076
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 # RCS: @(#) $Id: entry3.tcl,v 1.7 2007/12/13 15:27:07 dgp Exp $
9 if {![info exists widgetDemo]} {
10 error "This script should be run from the \"widget\" demo."
13 package require Tk
15 set w .entry3
16 catch {destroy $w}
17 toplevel $w
18 wm title $w "Constrained Entry Demonstration"
19 wm iconname $w "entry3"
20 positionWindow $w
22 label $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."
36 ## See Code / Dismiss buttons
37 set btns [addSeeDismiss $w.buttons $w]
38 pack $btns -side bottom -fill x
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).
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
52 proc 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
62 after 200 [list focusAndFlash $W $fg $bg [expr {$count-1}]]
66 labelframe $w.l1 -text "Integer Entry"
67 # Alternatively try using {string is digit} for arbitrary length numbers,
68 # and not just 32-bit ones.
69 entry $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]"
72 pack $w.l1.e -fill x -expand 1 -padx 1m -pady 1m
74 labelframe $w.l2 -text "Length-Constrained Entry"
75 entry $w.l2.e -validate key -invcmd bell -vcmd {expr {[string length %P]<10}}
76 pack $w.l2.e -fill x -expand 1 -padx 1m -pady 1m
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.
82 # Initial content for the third entry widget
83 set 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.
87 set phoneNumberMap {}
88 foreach {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
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.
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.
105 proc 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
118 return 0
121 # phoneSkipLeft --
122 # Skip over fixed characters in a phone-number string when moving left.
124 # Arguments:
125 # W - The entry widget containing the phone-number.
127 proc 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
142 # phoneSkipRight --
143 # Skip over fixed characters in a phone-number string when moving right.
145 # Arguments:
146 # W - The entry widget containing the phone-number.
147 # add - Offset to add to index before calculation (used by validation.)
149 proc 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
164 labelframe $w.l3 -text "US Phone-Number Entry"
165 entry $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...
168 bind $w.l3.e <FocusIn> {
169 if {"%d" ne "NotifyAncestor"} {
170 %W icursor 3
171 after idle {%W selection clear}
174 bind $w.l3.e <Left> {phoneSkipLeft %W}
175 bind $w.l3.e <Right> {phoneSkipRight %W}
176 pack $w.l3.e -fill x -expand 1 -padx 1m -pady 1m
178 labelframe $w.l4 -text "Password Entry"
179 entry $w.l4.e -validate key -show "*" -vcmd {expr {[string length %P]<=8}}
180 pack $w.l4.e -fill x -expand 1 -padx 1m -pady 1m
182 lower [frame $w.mid]
183 grid $w.l1 $w.l2 -in $w.mid -padx 3m -pady 1m -sticky ew
184 grid $w.l3 $w.l4 -in $w.mid -padx 3m -pady 1m -sticky ew
185 grid columnconfigure $w.mid {0 1} -uniform 1
186 pack $w.msg -side top
187 pack $w.mid -fill both -expand 1