Upgrade to Tcl/Tk 8.5b2
[msysgit.git] / mingw / lib / tk8.5 / demos / entry3.tcl
blobc8dd1ab1b02bf9957559d2cb6cbf04c98ca28e3a
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.5 2007/10/17 18:55:05 das 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 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 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