home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / tcl / 2195 < prev    next >
Encoding:
Text File  |  1992-12-22  |  4.0 KB  |  129 lines

  1. Newsgroups: comp.lang.tcl
  2. Path: sparky!uunet!caen!spencer
  3. From: spencer@med.umich.edu (Spencer W. Thomas)
  4. Subject: Re: Problem losing expose events
  5. Message-ID: <SPENCER.92Dec21164420@guraldi.med.umich.edu>
  6. Date: Mon, 21 Dec 92 16:44:20 EST
  7. Organization: University of Michigan
  8. Distribution: comp
  9. Nntp-Posting-Host: guraldi.itn.med.umich.edu
  10. Lines: 117
  11.  
  12. In response to schendr@drssparc.eptd.Texaco.COM (Dan R. Schenck).  I
  13. tried e-mail but it bounced.
  14.  
  15. You need to make winDone global, and declare it in your timedMsg
  16. routine command (and, with tk3.0 in the <Expose> event command).  The
  17. bound commands get executed at the global level, so they cannot see
  18. variables internal to other procedures.
  19.  
  20. That said, I don't get the behavior you're observing.  When I push the
  21. "test" button, the window comes up, I get the "Returned from timed
  22. message" line, and then the window goes away.  In fact, it frequently
  23. returns from the timedMessage routine before the window is actually
  24. exposed.  When I click Quit, the program quits.  Since the problem is
  25. based on a race condition, this is not too surprising.
  26.  
  27. I'm running X11R5, Motif, tk2.3/tclX6.4c.  The really interesting
  28. thing is when I put an "echo winDone" right after the tkwait line.
  29. Without the "global winDone" declaration, this always prints 0.  With
  30. the global declaration, it prints 1.  Something strange is happening
  31. in tkwait.
  32.  
  33. Under tk3.0, I do get the behavior you're seeing.  Apparently the
  34. tk3.0 update command waits until the window is exposed before
  35. returning, so the tkwait is superfluous.  (I'm not sure about this,
  36. but I always get "About to wait winDone = 1" from the code below under
  37. tk3.0, but not always under tk2.3.) I fixed the "race condition" by
  38. adding an if {$winDone == 0} before the tkwait.
  39.  
  40. The code below is my modification to your code with a switch variable
  41. on the first line to change the behavior between "broken" and working.
  42.  
  43.  
  44. =S
  45.  
  46. set broken 0
  47.  
  48. # Define tclX procs if not present. (tk3.0)
  49. if {[string length [info command echo]] == 0 } {
  50. proc echo {args} {
  51.     puts stderr $args;
  52. }
  53. proc max {a b} {
  54.     if {$a > $b} { return $a } { return $b }
  55. }
  56. }
  57.  
  58. proc genName {{var var}} {
  59.   global nameCount
  60.   if [info exists nameCount($var)] {
  61.     incr nameCount($var)
  62.   } else {
  63.     set nameCount($var) 0
  64.   }
  65.   return "$var$nameCount($var)"
  66. }
  67.  
  68. proc timedMsg { msg { timeout 30000 }} {
  69.     global broken
  70.     if {! $broken} {global winDone}
  71.   set winDone {0}
  72. # Tk3.0 doesn't allow capitalized window names.
  73.   set w [genName .infoMessage]
  74.   toplevel $w
  75.   bind $w <Expose> { global broken; echo exposed ;if {! $broken} {global winDone};set winDone {1} }
  76.   wm withdraw $w
  77.   wm title $w "Message"
  78.  
  79.   frame $w.m
  80.  
  81.   set tmp [split $msg \n]
  82.   set i 0
  83.   foreach line $tmp {
  84.     set i [max $i [string length $line]]
  85.   }
  86.  
  87.   message $w.m.l -text "$msg" \
  88.     -width [expr $i*9] \
  89.     -relief {raised} \
  90.     -justify {center}
  91.   button $w.m.b -command "destroy $w" -text "OK"
  92.  
  93.   pack append $w.m $w.m.l top $w.m.b { top pady 5 }
  94.   pack append $w $w.m {top expand fill}
  95.   if { $timeout != "INFINITE" } { after $timeout "removeTimedMsg $w" }
  96.  
  97.   wm deiconify $w
  98.     echo Before update winDone = $winDone
  99.   update
  100.     echo About to wait winDone = $winDone
  101.   if { $winDone == 0 } { tkwait variable winDone }
  102.   
  103.     echo After wait winDone=$winDone
  104.  
  105.   return $w
  106.  
  107. }
  108.  
  109. proc removeTimedMsg { win } {
  110.  
  111.   if { [lsearch [winfo children .] $win] > -1 } { destroy $win }
  112.  
  113. }
  114.  
  115. frame .f -bg {linen} -relief {raised}
  116. button .f.testbug -text {Launch Msg Win} -bg {cornflowerblue} -fg {white} \
  117.   -command {
  118.     timedMsg "Test of expose bug" 3000
  119.     puts stdout "Returned from timed message"
  120.   }
  121. button .f.quit -text {Quit} -bg {red} -fg {white} -command { destroy . }
  122. pack append .f .f.testbug { top frame center padx 10 pady 10 } \
  123.            .f.quit { top frame center padx 10 pady 10 }
  124. pack append . .f { top frame center expand fill }
  125. --
  126. =Spencer W. Thomas         |  Info Tech and Networking, B1911 CFOB, 0704
  127.    "Genome Informatician"    |  Univ of Michigan, Ann Arbor, MI 48109
  128. Spencer.W.Thomas@med.umich.edu    |  313-747-2778, FAX 313-764-4133
  129.