cscroll.tcl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # cscroll.tcl --
  2. #
  3. # This demonstration script creates a simple canvas that can be
  4. # scrolled in two dimensions.
  5. if {![info exists widgetDemo]} {
  6. error "This script should be run from the \"widget\" demo."
  7. }
  8. package require Tk
  9. set w .cscroll
  10. catch {destroy $w}
  11. toplevel $w
  12. wm title $w "Scrollable Canvas Demonstration"
  13. wm iconname $w "cscroll"
  14. positionWindow $w
  15. set c $w.c
  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. ## See Code / Dismiss buttons
  19. set btns [addSeeDismiss $w.buttons $w]
  20. pack $btns -side bottom -fill x
  21. frame $w.grid
  22. scrollbar $w.hscroll -orient horiz -command "$c xview"
  23. scrollbar $w.vscroll -command "$c yview"
  24. canvas $c -relief sunken -borderwidth 2 -scrollregion {-11c -11c 50c 20c} \
  25. -xscrollcommand "$w.hscroll set" \
  26. -yscrollcommand "$w.vscroll set"
  27. pack $w.grid -expand yes -fill both -padx 1 -pady 1
  28. grid rowconfig $w.grid 0 -weight 1 -minsize 0
  29. grid columnconfig $w.grid 0 -weight 1 -minsize 0
  30. grid $c -padx 1 -in $w.grid -pady 1 \
  31. -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
  32. grid $w.vscroll -in $w.grid -padx 1 -pady 1 \
  33. -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
  34. grid $w.hscroll -in $w.grid -padx 1 -pady 1 \
  35. -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news
  36. set bg [lindex [$c config -bg] 4]
  37. for {set i 0} {$i < 20} {incr i} {
  38. set x [expr {-10 + 3*$i}]
  39. for {set j 0; set y -10} {$j < 10} {incr j; incr y 3} {
  40. $c create rect ${x}c ${y}c [expr {$x+2}]c [expr {$y+2}]c \
  41. -outline black -fill $bg -tags rect
  42. $c create text [expr {$x+1}]c [expr {$y+1}]c -text "$i,$j" \
  43. -anchor center -tags text
  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. if {[tk windowingsystem] eq "aqua"} {
  52. bind $c <MouseWheel> {
  53. %W yview scroll [expr {- (%D)}] units
  54. }
  55. bind $c <Option-MouseWheel> {
  56. %W yview scroll [expr {-10 * (%D)}] units
  57. }
  58. bind $c <Shift-MouseWheel> {
  59. %W xview scroll [expr {- (%D)}] units
  60. }
  61. bind $c <Shift-Option-MouseWheel> {
  62. %W xview scroll [expr {-10 * (%D)}] units
  63. }
  64. }
  65. proc scrollEnter canvas {
  66. global oldFill
  67. set id [$canvas find withtag current]
  68. if {[lsearch [$canvas gettags current] text] >= 0} {
  69. set id [expr {$id-1}]
  70. }
  71. set oldFill [lindex [$canvas itemconfig $id -fill] 4]
  72. if {[winfo depth $canvas] > 1} {
  73. $canvas itemconfigure $id -fill SeaGreen1
  74. } else {
  75. $canvas itemconfigure $id -fill black
  76. $canvas itemconfigure [expr {$id+1}] -fill white
  77. }
  78. }
  79. proc scrollLeave canvas {
  80. global oldFill
  81. set id [$canvas find withtag current]
  82. if {[lsearch [$canvas gettags current] text] >= 0} {
  83. set id [expr {$id-1}]
  84. }
  85. $canvas itemconfigure $id -fill $oldFill
  86. $canvas itemconfigure [expr {$id+1}] -fill black
  87. }
  88. proc scrollButton canvas {
  89. global oldFill
  90. set id [$canvas find withtag current]
  91. if {[lsearch [$canvas gettags current] text] < 0} {
  92. set id [expr {$id+1}]
  93. }
  94. puts stdout "You buttoned at [lindex [$canvas itemconf $id -text] 4]"
  95. }