home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / tcl / 1916 < prev    next >
Encoding:
Text File  |  1992-11-21  |  4.6 KB  |  170 lines

  1. Newsgroups: comp.lang.tcl
  2. Path: sparky!uunet!usc!sol.ctr.columbia.edu!ira.uka.de!math.fu-berlin.de!news.netmbx.de!zelator!gamelan!thomas
  3. From: thf@zelator.in-berlin.de (Thomas Funke)
  4. Subject: Re: Can <Tab> be used to change focus in forms?
  5. Message-ID: <1992Nov19.180939.455@gamelan>
  6. Sender: thomas@gamelan (thomas)
  7. Reply-To: thf@zelator.in-berlin.de (Thomas Funke)
  8. Organization: NNU Corp. - NeXT is Not UN*X
  9. References: <1992Nov18.044237.24882@sinkhole.unf.edu>
  10. Date: Thu, 19 Nov 1992 18:09:39 GMT
  11. Lines: 157
  12.  
  13. In article <1992Nov18.044237.24882@sinkhole.unf.edu> shite@sinkhole.unf.edu  
  14. (Stephen Hite) writes:
  15. > Real beginner question (thanks in advance)...
  16. >     When creating a dialog using the Tk Toolkit (with wish), is
  17. > it easy to get the <Tab> key to act as a focus changer so a user
  18. > could easily go from one field to the next without using the
  19. > mouse?  That is, I'm looking for the functionality you'd get in a dialog
  20. > with Microsoft Windows.  
  21.  
  22. Well, not difficult, but you have to do it yourself. I include some code I used  
  23. a while ago. Excuse the german comments.
  24.  
  25.  
  26. -------------- cut here -----------------------------------------
  27.  
  28. # Erzeugt im Window w einen 'labelled Entry'. ReturnCommand ist
  29. # das Kommando, welches bei Return ausgefuehrt wird, nextEntry
  30. # ins das Widget, welches bei Tab selektiert wird
  31. # Falls der 4, Parameter (check) angegeben ist,
  32. # wird das darin befindliche Check-Kommando (mit Entry-Widget als 
  33. # Parameter) aufgerufen,
  34. # welches 0 liefern muss, wenn das Feld ok ist,
  35. #
  36. # Zu jedem Entry-Widget wird ein (globales) Array erzeugt
  37. # mit folgenden Inhalten:
  38. #
  39. #    entry(check)    -     Checkfunktion fuer das Feld (oder leer)
  40. #    entry(next)    -    Naechstes Feld (fuer TAB)
  41. #    entry(return)    -    Das Kommando, welches bei RET ausgefuehrt wird
  42. #    entry(disabled) -    1 wenn das Feld disabled ist
  43. #
  44. proc entryWidget { w labelText returnCommand nextEntry {check ""}} {
  45.     global $w.entry
  46.     
  47.     frame $w -borderwidth 5
  48.     label $w.label -text $labelText
  49.     entry $w.entry -width 100 -relief sunken
  50.     bind $w.entry <Key-Tab> "entryTab $w.entry"
  51.     bind $w.entry <Key-Return> "entryReturn $w.entry"
  52.     set $w.entry(check) $check
  53.     set $w.entry(next) $nextEntry.entry
  54.     set $w.entry(disabled) 0
  55.     set $w.entry(return) $returnCommand
  56.  
  57.     pack append $w $w.label { left } $w.entry {left fill frame e}
  58.     return $w
  59. }
  60.  
  61. # Wird bei Eingabe eines TAB's im Entry aufgerufen
  62. # Falls das Check-Kommando des Widgets != 0 ergibt, wird
  63. # nicht ins naechste Feld gesprungen !
  64. proc entryTab {w} {
  65.     global $w
  66.  
  67.     trimEntry $w
  68.     if {[set [set w](check)] != ""} {
  69.     if {[eval [set [set w](check)] $w] != 0} return
  70.     }
  71.  
  72.     # Das naechste Feld
  73.     set next [set [set w](next)]
  74.     global $next
  75.  
  76.     while {[set [set next](disabled)]} {
  77.     set next [set [set next](next)]
  78.     global $next
  79.     }
  80.     
  81.     focus $next
  82. }
  83.  
  84. #wird bei RETURN im Entry aufgerufen
  85. proc entryReturn {w} {
  86.     global $w
  87.     
  88.     trimEntry $w
  89.     if {[set [set w](check)] != ""} {
  90.     if {[eval [set [set w](check)] $w] != 0} return
  91.     }
  92.     catch [set [set w](return)]
  93. }
  94.  
  95. # ruft "focus $w" auf, wenn das aktuelle
  96. # Entry die 'Check-Funktion' passiert hat
  97. proc checkFocus {w} {
  98.     set current [focus]
  99.     global $current
  100.  
  101.     if {$w == $current} return
  102.     if {[catch "set [set current](check)"] != 0} {
  103.     focus $w
  104.     return
  105.     }
  106.     
  107.     set checkFunc [set [set current](check)]    
  108.     if {($checkFunc != "") && [$checkFunc $current] != 0} return
  109.     
  110.     focus $w
  111. }
  112.  
  113. # Prueft das aktuelle Feld, wo der Focus
  114. # drauf ist und liefert 0 wenn ok, 
  115. # ansonsten -1
  116. proc checkCurrentFocus {} {
  117.     
  118.     set current [focus]
  119.     global $current
  120.     if { $current  != "none" } {
  121.     if {[catch "set [set current](check)"] == 0} {
  122.         set checkFunc [set [set current](check)]
  123.         if {($checkFunc != "") && [$checkFunc $current] != 0} {
  124.         return -1
  125.         }
  126.     }
  127.     }
  128.     return 0
  129. }
  130.  
  131.  
  132.  
  133. # Erlaubt keine Editierung fuer das angegebene Entry-Widget
  134. proc disableEntry {w} {
  135.     global $w
  136.     
  137.     bind $w <1> return
  138.     $w configure -background bisque2
  139.     $w delete 0 9999
  140.     set [set w](disabled) 1
  141. }
  142.  
  143. # Entry wieder fuer Eingaben enablen
  144. proc enableEntry {w} {
  145.     global $w
  146.     
  147.     bind $w <1> { 
  148.     %W select clear; %W cursor @%x; 
  149.     checkFocus %W; %W select from @%x
  150.     }
  151.     $w configure -background AntiqueWhite1
  152.     set [set w](disabled) 0
  153. }
  154.  
  155. # entfernt Blanks am Anfang und Ende des Entries
  156. proc trimEntry {w} {
  157.     set s [string trim [$w get]]
  158.     $w delete 0 999
  159.     $w insert 0 $s
  160. }
  161. ------------------------------------------------------
  162.  
  163. -- 
  164. ------------------------------------------------------------------
  165. Thomas Funke  ** E-mail: thf@zelator.in-berlin.de
  166. NeXT-Developer * Unix-Consultant
  167. ------------------------------------------------------------------
  168.