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

  1. Newsgroups: comp.lang.tcl
  2. Path: sparky!uunet!timbuk.cray.com!hemlock.cray.com!mgc
  3. From: mgc@cray.com (M. G. Christenson)
  4. Subject: Re: automatic scrolling (fixed)
  5. Message-ID: <1992Dec28.153357.2646@hemlock.cray.com>
  6. Lines: 195
  7. Nntp-Posting-Host: pecan
  8. Reply-To: mgc@cray.com
  9. Organization: Cray Research, Inc.
  10. Date: 28 Dec 92 15:33:57 CST
  11.  
  12. My newsreader mangled my previous post of my autoscrolling code.
  13. This is what it should have looked like:
  14.  
  15. proc autoscroll_var w {
  16.     global autoscroll_index
  17.  
  18.     if [info exists autoscroll_index($w)] {
  19.     return autoscroll.$autoscroll_index($w)
  20.     } else {
  21.     if [info exists autoscroll_index(next)] {
  22.         set next [expr {$autoscroll_index(next) + 1}]
  23.     } else {
  24.         set next 0
  25.     }
  26.     set autoscroll_index(next) $next
  27.     return autoscroll.[set autoscroll_index($w) $next]
  28.     }
  29. }
  30.  
  31. proc autoscroll_init \
  32.     {w delta_t {yscrollbar {}} {delta_y 1} {xscrollbar {}} {delta_x 1}} {
  33.     upvar #0 [autoscroll_var $w] v
  34.  
  35.     if {$delta_t == 0} {
  36.     catch {unset v}
  37.     return {}
  38.     }
  39.  
  40.     set v(autoscroll_delta_t) $delta_t
  41.     set v(autoscroll_delta_x) $delta_x
  42.     set v(autoscroll_delta_y) $delta_y
  43.  
  44.     set v(autoscroll_xscrollbar) $xscrollbar
  45.     if {$xscrollbar == {}} {
  46.     set v(autoscroll_dx) 0
  47.     }
  48.     set v(autoscroll_yscrollbar) $yscrollbar
  49.     if {$yscrollbar == {}} {
  50.     set v(autoscroll_dy) 0
  51.     }
  52.  
  53.     if {[trace vinfo v(autoscroll_dx)] == {}} {
  54.     set v(autoscrolling) 0
  55.     set v(autoscroll_dx) 0
  56.     set v(autoscroll_dy) 0
  57.  
  58.     trace variable v(autoscroll_dx) w "autoscroll $w"
  59.     trace variable v(autoscroll_dy) w "autoscroll $w"
  60.     }
  61.  
  62.     return {}
  63. }
  64.  
  65. proc autoscroll_event {w x y event} {
  66.     set cmd [bind $w $event]
  67.     if {$cmd == {}} {
  68.     set cmd [bind [winfo class $w] $event]
  69.     }
  70.     if {$cmd != {}} {
  71.     regsub -all %W $cmd $w cmd
  72.     regsub -all %x $cmd $x cmd
  73.     regsub -all %y $cmd $y cmd
  74.     uplevel #0 $cmd
  75.     }
  76. }
  77.  
  78. proc autoscroll {w args} {
  79.     upvar #0 [autoscroll_var $w] v
  80.  
  81.     if {$args == {}} {
  82.     if $v(autoscroll_dx) {
  83.         set current [lindex [$v(autoscroll_xscrollbar) get] 2]
  84.         set cmd [lindex [$v(autoscroll_xscrollbar) config -command] 4]
  85.         eval $cmd [expr "$current + $v(autoscroll_dx)"]
  86.         if {$v(autoscroll_event) != {}} {
  87.         autoscroll_event $w $v(autoscroll_x) $v(autoscroll_y) \
  88.             $v(autoscroll_event)
  89.         }
  90.     }
  91.     if $v(autoscroll_dy) {
  92.         set current [lindex [$v(autoscroll_yscrollbar) get] 2]
  93.         set cmd [lindex [$v(autoscroll_yscrollbar) config -command] 4]
  94.         eval $cmd [expr "$current + $v(autoscroll_dy)"]
  95.         if {$v(autoscroll_event) != {}} {
  96.         autoscroll_event $w $v(autoscroll_x) $v(autoscroll_y) \
  97.             $v(autoscroll_event)
  98.         }
  99.     }
  100.     set v(autoscrolling) 0
  101.     }
  102.  
  103.     if {$v(autoscroll_dx) || $v(autoscroll_dy)} {
  104.     if !$v(autoscrolling) {
  105.         set v(autoscrolling) 1
  106.         after $v(autoscroll_delta_t) autoscroll $w
  107.     }
  108.     } else {
  109.     set v(autoscrolling) 0
  110.     }
  111. }
  112.  
  113. proc clear_autoscroll {w} {
  114.     upvar #0 [autoscroll_var $w] v
  115.  
  116.     if [info exists v] {
  117.     set v(autoscroll_dx) 0
  118.     set v(autoscroll_dy) 0
  119.     }
  120.     return {}
  121. }
  122.  
  123. proc check_autoscroll {w x y {event {}}} {
  124.     upvar #0 [autoscroll_var $w] v
  125.  
  126.     if ![info exists v] {
  127.     return {}
  128.     }
  129.  
  130.     set dx 0
  131.     if {$v(autoscroll_xscrollbar) != {}} {
  132.     if {$x < 0} {
  133.         set dx -$v(autoscroll_delta_x)
  134.     } else {
  135.         if {$x >= [winfo width $w]} {
  136.         set dx $v(autoscroll_delta_x)
  137.         }
  138.     }
  139.     }
  140.  
  141.     set dy 0
  142.     if {$v(autoscroll_yscrollbar) != {}} {
  143.     if {$y < 0} {
  144.         set dy -$v(autoscroll_delta_y)
  145.     } else {
  146.         if {$y >= [winfo height $w]} {
  147.         set dy $v(autoscroll_delta_y)
  148.         }
  149.     }
  150.     }
  151.  
  152.     set v(autoscroll_x) $x
  153.     set v(autoscroll_y) $y
  154.     set v(autoscroll_event) $event
  155.  
  156.     if {$v(autoscroll_dx) != $dx} {
  157.     set v(autoscroll_dx) $dx
  158.     }
  159.  
  160.     if {$v(autoscroll_dy) != $dy} {
  161.     set v(autoscroll_dy) $dy
  162.     }
  163.  
  164.     return {}
  165. }
  166.  
  167. proc bind_for_autoscroll {{classes {Text Listbox Canvas Entry}}} {
  168.     foreach class $classes {
  169.     bind $class <B1-Motion> {+check_autoscroll %W %x %y <B1-Motion>}
  170.     bind $class <Shift-B1-Motion> \
  171.         {+check_autoscroll %W %x %y <Shift-B1-Motion>}
  172.     bind $class <Any-ButtonRelease-1> {+clear_autoscroll %W}
  173.     }
  174. }
  175.  
  176. proc autoscroll_example {} {
  177.     catch {destroy .example}
  178.  
  179.     frame .example
  180.     scrollbar .example.h -orient horizontal -command ".example.c xview"
  181.     scrollbar .example.v -command ".example.c yview"
  182.     canvas .example.c -confine 0 -xscrollcommand ".example.h set" \
  183.     -yscrollcommand ".example.v set"
  184.  
  185.     bind .example.c <1> {set x [%W canvasx %x]; set y [%W canvasy %y]}
  186.     bind .example.c <B1-Motion> {
  187.     %W delete oval
  188.     set xx  [%W canvasx %x]
  189.     set yy  [%W canvasy %y]
  190.     %W create oval \
  191.         $x $y $xx $yy -tag oval -fill red
  192.     %W config -scrollregion [%W bbox withtag oval]
  193.     }
  194.  
  195.     autoscroll_init .example.c 100 .example.v 1 .example.h 1
  196.  
  197.     bind_for_autoscroll .example.c
  198.  
  199.     pack append .example \
  200.     .example.h {bottom fillx} \
  201.     .example.c {left expand fill} \
  202.     .example.v {left filly}
  203.  
  204.     pack append . .example {top expand fill}
  205. }
  206.  
  207.