entry3.tcl 6.0 KB

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