cscroll.tcl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. -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 <Enter> "scrollEnter $c"
  47. $c bind all <Leave> "scrollLeave $c"
  48. $c bind all <Button-1> "scrollButton $c"
  49. if {([tk windowingsystem] eq "aqua") && ![package vsatisfies [package provide Tk] 8.7-]} {
  50. bind $c <Button-3> "$c scan mark %x %y"
  51. bind $c <B3-Motion> "$c scan dragto %x %y"
  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. } else {
  65. bind $c <Button-2> "$c scan mark %x %y"
  66. bind $c <B2-Motion> "$c scan dragto %x %y"
  67. # We must make sure that positive and negative movements are rounded
  68. # equally to integers, avoiding the problem that
  69. # (int)1/-30 = -1,
  70. # but
  71. # (int)-1/-30 = 0
  72. # The following code ensure equal +/- behaviour.
  73. bind $c <MouseWheel> {
  74. if {%D >= 0} {
  75. %W yview scroll [expr {%D/-30}] units
  76. } else {
  77. %W yview scroll [expr {(%D-29)/-30}] units
  78. }
  79. }
  80. bind $c <Option-MouseWheel> {
  81. if {%D >= 0} {
  82. %W yview scroll [expr {%D/-3}] units
  83. } else {
  84. %W yview scroll [expr {(%D-2)/-3}] units
  85. }
  86. }
  87. bind $c <Shift-MouseWheel> {
  88. if {%D >= 0} {
  89. %W xview scroll [expr {%D/-30}] units
  90. } else {
  91. %W xview scroll [expr {(%D-29)/-30}] units
  92. }
  93. }
  94. bind $c <Shift-Option-MouseWheel> {
  95. if {%D >= 0} {
  96. %W xview scroll [expr {%D/-3}] units
  97. } else {
  98. %W xview scroll [expr {(%D-2)/-3}] units
  99. }
  100. }
  101. }
  102. if {[tk windowingsystem] eq "x11" && ![package vsatisfies [package provide Tk] 8.7-]} {
  103. # Support for mousewheels on Linux/Unix commonly comes through mapping
  104. # the wheel to the extended buttons. If you have a mousewheel, find
  105. # Linux configuration info at:
  106. # http://linuxreviews.org/howtos/xfree/mouse/
  107. bind $c <Button-4> {
  108. if {!$tk_strictMotif} {
  109. %W yview scroll -5 units
  110. }
  111. }
  112. bind $c <Shift-Button-4> {
  113. if {!$tk_strictMotif} {
  114. %W xview scroll -5 units
  115. }
  116. }
  117. bind $c <Button-5> {
  118. if {!$tk_strictMotif} {
  119. %W yview scroll 5 units
  120. }
  121. }
  122. bind $c <Shift-Button-5> {
  123. if {!$tk_strictMotif} {
  124. %W xview scroll 5 units
  125. }
  126. }
  127. }
  128. proc scrollEnter canvas {
  129. global oldFill
  130. set id [$canvas find withtag current]
  131. if {[lsearch [$canvas gettags current] text] >= 0} {
  132. set id [expr {$id-1}]
  133. }
  134. set oldFill [lindex [$canvas itemconfig $id -fill] 4]
  135. if {[winfo depth $canvas] > 1} {
  136. if {[tk windowingsystem] eq "aqua"} {
  137. $canvas itemconfigure $id -fill systemSelectedTextBackgroundColor
  138. } else {
  139. $canvas itemconfigure $id -fill LightSeaGreen
  140. }
  141. }
  142. }
  143. proc scrollLeave canvas {
  144. global oldFill
  145. set id [$canvas find withtag current]
  146. if {[lsearch [$canvas gettags current] text] >= 0} {
  147. set id [expr {$id-1}]
  148. }
  149. $canvas itemconfigure $id -fill $oldFill
  150. }
  151. proc scrollButton canvas {
  152. set id [$canvas find withtag current]
  153. if {[lsearch [$canvas gettags current] text] < 0} {
  154. set id [expr {$id+1}]
  155. }
  156. puts stdout "You buttoned at [lindex [$canvas itemconf $id -text] 4]"
  157. }