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