scrlbar.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. # scrlbar.tcl --
  2. #
  3. # This file defines the default bindings for Tk scrollbar widgets.
  4. # It also provides procedures that help in implementing the bindings.
  5. #
  6. # Copyright (c) 1994 The Regents of the University of California.
  7. # Copyright (c) 1994-1996 Sun Microsystems, Inc.
  8. #
  9. # See the file "license.terms" for information on usage and redistribution
  10. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11. #
  12. #-------------------------------------------------------------------------
  13. # The code below creates the default class bindings for scrollbars.
  14. #-------------------------------------------------------------------------
  15. # Standard Motif bindings:
  16. if {[tk windowingsystem] eq "x11"} {
  17. bind Scrollbar <Enter> {
  18. if {$tk_strictMotif} {
  19. set tk::Priv(activeBg) [%W cget -activebackground]
  20. %W configure -activebackground [%W cget -background]
  21. }
  22. %W activate [%W identify %x %y]
  23. }
  24. bind Scrollbar <Motion> {
  25. %W activate [%W identify %x %y]
  26. }
  27. # The "info exists" command in the following binding handles the
  28. # situation where a Leave event occurs for a scrollbar without the Enter
  29. # event. This seems to happen on some systems (such as Solaris 2.4) for
  30. # unknown reasons.
  31. bind Scrollbar <Leave> {
  32. if {$tk_strictMotif && [info exists tk::Priv(activeBg)]} {
  33. %W configure -activebackground $tk::Priv(activeBg)
  34. }
  35. %W activate {}
  36. }
  37. bind Scrollbar <1> {
  38. tk::ScrollButtonDown %W %x %y
  39. }
  40. bind Scrollbar <B1-Motion> {
  41. tk::ScrollDrag %W %x %y
  42. }
  43. bind Scrollbar <B1-B2-Motion> {
  44. tk::ScrollDrag %W %x %y
  45. }
  46. bind Scrollbar <ButtonRelease-1> {
  47. tk::ScrollButtonUp %W %x %y
  48. }
  49. bind Scrollbar <B1-Leave> {
  50. # Prevents <Leave> binding from being invoked.
  51. }
  52. bind Scrollbar <B1-Enter> {
  53. # Prevents <Enter> binding from being invoked.
  54. }
  55. bind Scrollbar <2> {
  56. tk::ScrollButton2Down %W %x %y
  57. }
  58. bind Scrollbar <B1-2> {
  59. # Do nothing, since button 1 is already down.
  60. }
  61. bind Scrollbar <B2-1> {
  62. # Do nothing, since button 2 is already down.
  63. }
  64. bind Scrollbar <B2-Motion> {
  65. tk::ScrollDrag %W %x %y
  66. }
  67. bind Scrollbar <ButtonRelease-2> {
  68. tk::ScrollButtonUp %W %x %y
  69. }
  70. bind Scrollbar <B1-ButtonRelease-2> {
  71. # Do nothing: B1 release will handle it.
  72. }
  73. bind Scrollbar <B2-ButtonRelease-1> {
  74. # Do nothing: B2 release will handle it.
  75. }
  76. bind Scrollbar <B2-Leave> {
  77. # Prevents <Leave> binding from being invoked.
  78. }
  79. bind Scrollbar <B2-Enter> {
  80. # Prevents <Enter> binding from being invoked.
  81. }
  82. bind Scrollbar <Control-1> {
  83. tk::ScrollTopBottom %W %x %y
  84. }
  85. bind Scrollbar <Control-2> {
  86. tk::ScrollTopBottom %W %x %y
  87. }
  88. bind Scrollbar <Up> {
  89. tk::ScrollByUnits %W v -1
  90. }
  91. bind Scrollbar <Down> {
  92. tk::ScrollByUnits %W v 1
  93. }
  94. bind Scrollbar <Control-Up> {
  95. tk::ScrollByPages %W v -1
  96. }
  97. bind Scrollbar <Control-Down> {
  98. tk::ScrollByPages %W v 1
  99. }
  100. bind Scrollbar <Left> {
  101. tk::ScrollByUnits %W h -1
  102. }
  103. bind Scrollbar <Right> {
  104. tk::ScrollByUnits %W h 1
  105. }
  106. bind Scrollbar <Control-Left> {
  107. tk::ScrollByPages %W h -1
  108. }
  109. bind Scrollbar <Control-Right> {
  110. tk::ScrollByPages %W h 1
  111. }
  112. bind Scrollbar <Prior> {
  113. tk::ScrollByPages %W hv -1
  114. }
  115. bind Scrollbar <Next> {
  116. tk::ScrollByPages %W hv 1
  117. }
  118. bind Scrollbar <Home> {
  119. tk::ScrollToPos %W 0
  120. }
  121. bind Scrollbar <End> {
  122. tk::ScrollToPos %W 1
  123. }
  124. }
  125. if {[tk windowingsystem] eq "aqua"} {
  126. bind Scrollbar <MouseWheel> {
  127. tk::ScrollByUnits %W v [expr {- (%D)}]
  128. }
  129. bind Scrollbar <Option-MouseWheel> {
  130. tk::ScrollByUnits %W v [expr {-10 * (%D)}]
  131. }
  132. bind Scrollbar <Shift-MouseWheel> {
  133. tk::ScrollByUnits %W h [expr {- (%D)}]
  134. }
  135. bind Scrollbar <Shift-Option-MouseWheel> {
  136. tk::ScrollByUnits %W h [expr {-10 * (%D)}]
  137. }
  138. }
  139. # tk::ScrollButtonDown --
  140. # This procedure is invoked when a button is pressed in a scrollbar.
  141. # It changes the way the scrollbar is displayed and takes actions
  142. # depending on where the mouse is.
  143. #
  144. # Arguments:
  145. # w - The scrollbar widget.
  146. # x, y - Mouse coordinates.
  147. proc tk::ScrollButtonDown {w x y} {
  148. variable ::tk::Priv
  149. set Priv(relief) [$w cget -activerelief]
  150. $w configure -activerelief sunken
  151. set element [$w identify $x $y]
  152. if {$element eq "slider"} {
  153. ScrollStartDrag $w $x $y
  154. } else {
  155. ScrollSelect $w $element initial
  156. }
  157. }
  158. # ::tk::ScrollButtonUp --
  159. # This procedure is invoked when a button is released in a scrollbar.
  160. # It cancels scans and auto-repeats that were in progress, and restores
  161. # the way the active element is displayed.
  162. #
  163. # Arguments:
  164. # w - The scrollbar widget.
  165. # x, y - Mouse coordinates.
  166. proc ::tk::ScrollButtonUp {w x y} {
  167. variable ::tk::Priv
  168. tk::CancelRepeat
  169. if {[info exists Priv(relief)]} {
  170. # Avoid error due to spurious release events
  171. $w configure -activerelief $Priv(relief)
  172. ScrollEndDrag $w $x $y
  173. $w activate [$w identify $x $y]
  174. }
  175. }
  176. # ::tk::ScrollSelect --
  177. # This procedure is invoked when a button is pressed over the scrollbar.
  178. # It invokes one of several scrolling actions depending on where in
  179. # the scrollbar the button was pressed.
  180. #
  181. # Arguments:
  182. # w - The scrollbar widget.
  183. # element - The element of the scrollbar that was selected, such
  184. # as "arrow1" or "trough2". Shouldn't be "slider".
  185. # repeat - Whether and how to auto-repeat the action: "noRepeat"
  186. # means don't auto-repeat, "initial" means this is the
  187. # first action in an auto-repeat sequence, and "again"
  188. # means this is the second repetition or later.
  189. proc ::tk::ScrollSelect {w element repeat} {
  190. variable ::tk::Priv
  191. if {![winfo exists $w]} return
  192. switch -- $element {
  193. "arrow1" {ScrollByUnits $w hv -1}
  194. "trough1" {ScrollByPages $w hv -1}
  195. "trough2" {ScrollByPages $w hv 1}
  196. "arrow2" {ScrollByUnits $w hv 1}
  197. default {return}
  198. }
  199. if {$repeat eq "again"} {
  200. set Priv(afterId) [after [$w cget -repeatinterval] \
  201. [list tk::ScrollSelect $w $element again]]
  202. } elseif {$repeat eq "initial"} {
  203. set delay [$w cget -repeatdelay]
  204. if {$delay > 0} {
  205. set Priv(afterId) [after $delay \
  206. [list tk::ScrollSelect $w $element again]]
  207. }
  208. }
  209. }
  210. # ::tk::ScrollStartDrag --
  211. # This procedure is called to initiate a drag of the slider. It just
  212. # remembers the starting position of the mouse and slider.
  213. #
  214. # Arguments:
  215. # w - The scrollbar widget.
  216. # x, y - The mouse position at the start of the drag operation.
  217. proc ::tk::ScrollStartDrag {w x y} {
  218. variable ::tk::Priv
  219. if {[$w cget -command] eq ""} {
  220. return
  221. }
  222. set Priv(pressX) $x
  223. set Priv(pressY) $y
  224. set Priv(initValues) [$w get]
  225. set iv0 [lindex $Priv(initValues) 0]
  226. if {[llength $Priv(initValues)] == 2} {
  227. set Priv(initPos) $iv0
  228. } elseif {$iv0 == 0} {
  229. set Priv(initPos) 0.0
  230. } else {
  231. set Priv(initPos) [expr {(double([lindex $Priv(initValues) 2])) \
  232. / [lindex $Priv(initValues) 0]}]
  233. }
  234. }
  235. # ::tk::ScrollDrag --
  236. # This procedure is called for each mouse motion even when the slider
  237. # is being dragged. It notifies the associated widget if we're not
  238. # jump scrolling, and it just updates the scrollbar if we are jump
  239. # scrolling.
  240. #
  241. # Arguments:
  242. # w - The scrollbar widget.
  243. # x, y - The current mouse position.
  244. proc ::tk::ScrollDrag {w x y} {
  245. variable ::tk::Priv
  246. if {$Priv(initPos) eq ""} {
  247. return
  248. }
  249. set delta [$w delta [expr {$x - $Priv(pressX)}] [expr {$y - $Priv(pressY)}]]
  250. if {[$w cget -jump]} {
  251. if {[llength $Priv(initValues)] == 2} {
  252. $w set [expr {[lindex $Priv(initValues) 0] + $delta}] \
  253. [expr {[lindex $Priv(initValues) 1] + $delta}]
  254. } else {
  255. set delta [expr {round($delta * [lindex $Priv(initValues) 0])}]
  256. eval [list $w] set [lreplace $Priv(initValues) 2 3 \
  257. [expr {[lindex $Priv(initValues) 2] + $delta}] \
  258. [expr {[lindex $Priv(initValues) 3] + $delta}]]
  259. }
  260. } else {
  261. ScrollToPos $w [expr {$Priv(initPos) + $delta}]
  262. }
  263. }
  264. # ::tk::ScrollEndDrag --
  265. # This procedure is called to end an interactive drag of the slider.
  266. # It scrolls the window if we're in jump mode, otherwise it does nothing.
  267. #
  268. # Arguments:
  269. # w - The scrollbar widget.
  270. # x, y - The mouse position at the end of the drag operation.
  271. proc ::tk::ScrollEndDrag {w x y} {
  272. variable ::tk::Priv
  273. if {$Priv(initPos) eq ""} {
  274. return
  275. }
  276. if {[$w cget -jump]} {
  277. set delta [$w delta [expr {$x - $Priv(pressX)}] \
  278. [expr {$y - $Priv(pressY)}]]
  279. ScrollToPos $w [expr {$Priv(initPos) + $delta}]
  280. }
  281. set Priv(initPos) ""
  282. }
  283. # ::tk::ScrollByUnits --
  284. # This procedure tells the scrollbar's associated widget to scroll up
  285. # or down by a given number of units. It notifies the associated widget
  286. # in different ways for old and new command syntaxes.
  287. #
  288. # Arguments:
  289. # w - The scrollbar widget.
  290. # orient - Which kinds of scrollbars this applies to: "h" for
  291. # horizontal, "v" for vertical, "hv" for both.
  292. # amount - How many units to scroll: typically 1 or -1.
  293. proc ::tk::ScrollByUnits {w orient amount} {
  294. set cmd [$w cget -command]
  295. if {$cmd eq "" || ([string first \
  296. [string index [$w cget -orient] 0] $orient] < 0)} {
  297. return
  298. }
  299. set info [$w get]
  300. if {[llength $info] == 2} {
  301. uplevel #0 $cmd scroll $amount units
  302. } else {
  303. uplevel #0 $cmd [expr {[lindex $info 2] + $amount}]
  304. }
  305. }
  306. # ::tk::ScrollByPages --
  307. # This procedure tells the scrollbar's associated widget to scroll up
  308. # or down by a given number of screenfuls. It notifies the associated
  309. # widget in different ways for old and new command syntaxes.
  310. #
  311. # Arguments:
  312. # w - The scrollbar widget.
  313. # orient - Which kinds of scrollbars this applies to: "h" for
  314. # horizontal, "v" for vertical, "hv" for both.
  315. # amount - How many screens to scroll: typically 1 or -1.
  316. proc ::tk::ScrollByPages {w orient amount} {
  317. set cmd [$w cget -command]
  318. if {$cmd eq "" || ([string first \
  319. [string index [$w cget -orient] 0] $orient] < 0)} {
  320. return
  321. }
  322. set info [$w get]
  323. if {[llength $info] == 2} {
  324. uplevel #0 $cmd scroll $amount pages
  325. } else {
  326. uplevel #0 $cmd [expr {[lindex $info 2] + $amount*([lindex $info 1] - 1)}]
  327. }
  328. }
  329. # ::tk::ScrollToPos --
  330. # This procedure tells the scrollbar's associated widget to scroll to
  331. # a particular location, given by a fraction between 0 and 1. It notifies
  332. # the associated widget in different ways for old and new command syntaxes.
  333. #
  334. # Arguments:
  335. # w - The scrollbar widget.
  336. # pos - A fraction between 0 and 1 indicating a desired position
  337. # in the document.
  338. proc ::tk::ScrollToPos {w pos} {
  339. set cmd [$w cget -command]
  340. if {$cmd eq ""} {
  341. return
  342. }
  343. set info [$w get]
  344. if {[llength $info] == 2} {
  345. uplevel #0 $cmd moveto $pos
  346. } else {
  347. uplevel #0 $cmd [expr {round([lindex $info 0]*$pos)}]
  348. }
  349. }
  350. # ::tk::ScrollTopBottom
  351. # Scroll to the top or bottom of the document, depending on the mouse
  352. # position.
  353. #
  354. # Arguments:
  355. # w - The scrollbar widget.
  356. # x, y - Mouse coordinates within the widget.
  357. proc ::tk::ScrollTopBottom {w x y} {
  358. variable ::tk::Priv
  359. set element [$w identify $x $y]
  360. if {[string match *1 $element]} {
  361. ScrollToPos $w 0
  362. } elseif {[string match *2 $element]} {
  363. ScrollToPos $w 1
  364. }
  365. # Set Priv(relief), since it's needed by tk::ScrollButtonUp.
  366. set Priv(relief) [$w cget -activerelief]
  367. }
  368. # ::tk::ScrollButton2Down
  369. # This procedure is invoked when button 2 is pressed over a scrollbar.
  370. # If the button is over the trough or slider, it sets the scrollbar to
  371. # the mouse position and starts a slider drag. Otherwise it just
  372. # behaves the same as button 1.
  373. #
  374. # Arguments:
  375. # w - The scrollbar widget.
  376. # x, y - Mouse coordinates within the widget.
  377. proc ::tk::ScrollButton2Down {w x y} {
  378. variable ::tk::Priv
  379. set element [$w identify $x $y]
  380. if {[string match {arrow[12]} $element]} {
  381. ScrollButtonDown $w $x $y
  382. return
  383. }
  384. ScrollToPos $w [$w fraction $x $y]
  385. set Priv(relief) [$w cget -activerelief]
  386. # Need the "update idletasks" below so that the widget calls us
  387. # back to reset the actual scrollbar position before we start the
  388. # slider drag.
  389. update idletasks
  390. $w configure -activerelief sunken
  391. $w activate slider
  392. ScrollStartDrag $w $x $y
  393. }