home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09962.iso / vrml / cp2b2x.exe / DATA.Z / cscroll.tcl < prev    next >
Text File  |  1996-04-23  |  3KB  |  85 lines

  1. # cscroll.tcl --
  2. #
  3. # This demonstration script creates a simple canvas that can be
  4. # scrolled in two dimensions.
  5. #
  6. # @(#) cscroll.tcl 1.1 95/05/26 15:56:27
  7.  
  8. set w .cscroll
  9. catch {destroy $w}
  10. toplevel $w
  11. wm title $w "Scrollable Canvas Demonstration"
  12. wm iconname $w "cscroll"
  13. positionWindow $w
  14. set c $w.c
  15.  
  16. label $w.msg -font $font -wraplength 4i -justify left -text "This window displays a canvas widget that can be scrolled either using the scrollbars or by dragging with button 2 in the canvas.  If you click button 1 on one of the rectangles, its indices will be printed on stdout."
  17. pack $w.msg -side top
  18.  
  19. frame $w.buttons
  20. pack  $w.buttons -side bottom -expand y -fill x -pady 2m
  21. button $w.buttons.dismiss -text Dismiss -command "destroy $w"
  22. button $w.buttons.code -text "See Code" -command "showCode $w"
  23. pack $w.buttons.dismiss $w.buttons.code -side left -expand 1
  24.  
  25. scrollbar $w.hscroll -orient horiz -command "$c xview"
  26. pack $w.hscroll -side bottom -fill x
  27. scrollbar $w.vscroll -command "$c yview"
  28. pack $w.vscroll -side right -fill y
  29.  
  30. canvas $c -relief sunken -borderwidth 2 -scrollregion {-11c -11c 50c 20c} \
  31.     -xscrollcommand "$w.hscroll set" \
  32.     -yscrollcommand "$w.vscroll set"
  33. pack $c -expand yes -fill both
  34.  
  35. set bg [lindex [$c config -bg] 4]
  36. for {set i 0} {$i < 20} {incr i} {
  37.     set x [expr {-10 + 3*$i}]
  38.     for {set j 0; set y -10} {$j < 10} {incr j; incr y 3} {
  39.     $c create rect ${x}c ${y}c [expr $x+2]c [expr $y+2]c \
  40.         -outline black -fill $bg -tags rect
  41.     $c create text [expr $x+1]c [expr $y+1]c -text "$i,$j" \
  42.         -anchor center -tags text
  43.     }
  44. }
  45.  
  46. $c bind all <Any-Enter> "scrollEnter $c"
  47. $c bind all <Any-Leave> "scrollLeave $c"
  48. $c bind all <1> "scrollButton $c"
  49. bind $c <2> "$c scan mark %x %y"
  50. bind $c <B2-Motion> "$c scan dragto %x %y"
  51.  
  52. proc scrollEnter canvas {
  53.     global oldFill
  54.     set id [$canvas find withtag current]
  55.     if {[lsearch [$canvas gettags current] text] >= 0} {
  56.     set id [expr $id-1]
  57.     }
  58.     set oldFill [lindex [$canvas itemconfig $id -fill] 4]
  59.     if {[winfo depth $canvas] > 1} {
  60.     $canvas itemconfigure $id -fill SeaGreen1
  61.     } else {
  62.     $canvas itemconfigure $id -fill black
  63.     $canvas itemconfigure [expr $id+1] -fill white
  64.     }
  65. }
  66.  
  67. proc scrollLeave canvas {
  68.     global oldFill
  69.     set id [$canvas find withtag current]
  70.     if {[lsearch [$canvas gettags current] text] >= 0} {
  71.     set id [expr $id-1]
  72.     }
  73.     $canvas itemconfigure $id -fill $oldFill
  74.     $canvas itemconfigure [expr $id+1] -fill black
  75. }
  76.  
  77. proc scrollButton canvas {
  78.     global oldFill
  79.     set id [$canvas find withtag current]
  80.     if {[lsearch [$canvas gettags current] text] < 0} {
  81.     set id [expr $id+1]
  82.     }
  83.     puts stdout "You buttoned at [lindex [$canvas itemconf $id -text] 4]"
  84. }
  85.