scrlbar.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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" || [tk windowingsystem] eq "aqua"} {
  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. } else {
  139. bind Scrollbar <MouseWheel> {
  140. tk::ScrollByUnits %W v [expr {- (%D /120 ) * 4}]
  141. }
  142. bind Scrollbar <Shift-MouseWheel> {
  143. tk::ScrollByUnits %W h [expr {- (%D /120 ) * 4}]
  144. }
  145. }
  146. # tk::ScrollButtonDown --
  147. # This procedure is invoked when a button is pressed in a scrollbar.
  148. # It changes the way the scrollbar is displayed and takes actions
  149. # depending on where the mouse is.
  150. #
  151. # Arguments:
  152. # w - The scrollbar widget.
  153. # x, y - Mouse coordinates.
  154. proc tk::ScrollButtonDown {w x y} {
  155. variable ::tk::Priv
  156. set Priv(relief) [$w cget -activerelief]
  157. $w configure -activerelief sunken
  158. set element [$w identify $x $y]
  159. if {$element eq "slider"} {
  160. ScrollStartDrag $w $x $y
  161. } else {
  162. ScrollSelect $w $element initial
  163. }
  164. }
  165. # ::tk::ScrollButtonUp --
  166. # This procedure is invoked when a button is released in a scrollbar.
  167. # It cancels scans and auto-repeats that were in progress, and restores
  168. # the way the active element is displayed.
  169. #
  170. # Arguments:
  171. # w - The scrollbar widget.
  172. # x, y - Mouse coordinates.
  173. proc ::tk::ScrollButtonUp {w x y} {
  174. variable ::tk::Priv
  175. tk::CancelRepeat
  176. if {[info exists Priv(relief)]} {
  177. # Avoid error due to spurious release events
  178. $w configure -activerelief $Priv(relief)
  179. ScrollEndDrag $w $x $y
  180. $w activate [$w identify $x $y]
  181. }
  182. }
  183. # ::tk::ScrollSelect --
  184. # This procedure is invoked when a button is pressed over the scrollbar.
  185. # It invokes one of several scrolling actions depending on where in
  186. # the scrollbar the button was pressed.
  187. #
  188. # Arguments:
  189. # w - The scrollbar widget.
  190. # element - The element of the scrollbar that was selected, such
  191. # as "arrow1" or "trough2". Shouldn't be "slider".
  192. # repeat - Whether and how to auto-repeat the action: "noRepeat"
  193. # means don't auto-repeat, "initial" means this is the
  194. # first action in an auto-repeat sequence, and "again"
  195. # means this is the second repetition or later.
  196. proc ::tk::ScrollSelect {w element repeat} {
  197. variable ::tk::Priv
  198. if {![winfo exists $w]} return
  199. switch -- $element {
  200. "arrow1" {ScrollByUnits $w hv -1}
  201. "trough1" {ScrollByPages $w hv -1}
  202. "trough2" {ScrollByPages $w hv 1}
  203. "arrow2" {ScrollByUnits $w hv 1}
  204. default {return}
  205. }
  206. if {$repeat eq "again"} {
  207. set Priv(afterId) [after [$w cget -repeatinterval] \
  208. [list tk::ScrollSelect $w $element again]]
  209. } elseif {$repeat eq "initial"} {
  210. set delay [$w cget -repeatdelay]
  211. if {$delay > 0} {
  212. set Priv(afterId) [after $delay \
  213. [list tk::ScrollSelect $w $element again]]
  214. }
  215. }
  216. }
  217. # ::tk::ScrollStartDrag --
  218. # This procedure is called to initiate a drag of the slider. It just
  219. # remembers the starting position of the mouse and slider.
  220. #
  221. # Arguments:
  222. # w - The scrollbar widget.
  223. # x, y - The mouse position at the start of the drag operation.
  224. proc ::tk::ScrollStartDrag {w x y} {
  225. variable ::tk::Priv
  226. if {[$w cget -command] eq ""} {
  227. return
  228. }
  229. set Priv(pressX) $x
  230. set Priv(pressY) $y
  231. set Priv(initValues) [$w get]
  232. set iv0 [lindex $Priv(initValues) 0]
  233. if {[llength $Priv(initValues)] == 2} {
  234. set Priv(initPos) $iv0
  235. } elseif {$iv0 == 0} {
  236. set Priv(initPos) 0.0
  237. } else {
  238. set Priv(initPos) [expr {(double([lindex $Priv(initValues) 2])) \
  239. / [lindex $Priv(initValues) 0]}]
  240. }
  241. }
  242. # ::tk::ScrollDrag --
  243. # This procedure is called for each mouse motion even when the slider
  244. # is being dragged. It notifies the associated widget if we're not
  245. # jump scrolling, and it just updates the scrollbar if we are jump
  246. # scrolling.
  247. #
  248. # Arguments:
  249. # w - The scrollbar widget.
  250. # x, y - The current mouse position.
  251. proc ::tk::ScrollDrag {w x y} {
  252. variable ::tk::Priv
  253. if {$Priv(initPos) eq ""} {
  254. return
  255. }
  256. set delta [$w delta [expr {$x - $Priv(pressX)}] [expr {$y - $Priv(pressY)}]]
  257. if {[$w cget -jump]} {
  258. if {[llength $Priv(initValues)] == 2} {
  259. $w set [expr {[lindex $Priv(initValues) 0] + $delta}] \
  260. [expr {[lindex $Priv(initValues) 1] + $delta}]
  261. } else {
  262. set delta [expr {round($delta * [lindex $Priv(initValues) 0])}]
  263. eval [list $w] set [lreplace $Priv(initValues) 2 3 \
  264. [expr {[lindex $Priv(initValues) 2] + $delta}] \
  265. [expr {[lindex $Priv(initValues) 3] + $delta}]]
  266. }
  267. } else {
  268. ScrollToPos $w [expr {$Priv(initPos) + $delta}]
  269. }
  270. }
  271. # ::tk::ScrollEndDrag --
  272. # This procedure is called to end an interactive drag of the slider.
  273. # It scrolls the window if we're in jump mode, otherwise it does nothing.
  274. #
  275. # Arguments:
  276. # w - The scrollbar widget.
  277. # x, y - The mouse position at the end of the drag operation.
  278. proc ::tk::ScrollEndDrag {w x y} {
  279. variable ::tk::Priv
  280. if {$Priv(initPos) eq ""} {
  281. return
  282. }
  283. if {[$w cget -jump]} {
  284. set delta [$w delta [expr {$x - $Priv(pressX)}] \
  285. [expr {$y - $Priv(pressY)}]]
  286. ScrollToPos $w [expr {$Priv(initPos) + $delta}]
  287. }
  288. set Priv(initPos) ""
  289. }
  290. # ::tk::ScrollByUnits --
  291. # This procedure tells the scrollbar's associated widget to scroll up
  292. # or down by a given number of units. It notifies the associated widget
  293. # in different ways for old and new command syntaxes.
  294. #
  295. # Arguments:
  296. # w - The scrollbar widget.
  297. # orient - Which kinds of scrollbars this applies to: "h" for
  298. # horizontal, "v" for vertical, "hv" for both.
  299. # amount - How many units to scroll: typically 1 or -1.
  300. proc ::tk::ScrollByUnits {w orient amount} {
  301. set cmd [$w cget -command]
  302. if {$cmd eq "" || ([string first \
  303. [string index [$w cget -orient] 0] $orient] < 0)} {
  304. return
  305. }
  306. set info [$w get]
  307. if {[llength $info] == 2} {
  308. uplevel #0 $cmd scroll $amount units
  309. } else {
  310. uplevel #0 $cmd [expr {[lindex $info 2] + $amount}]
  311. }
  312. }
  313. # ::tk::ScrollByPages --
  314. # This procedure tells the scrollbar's associated widget to scroll up
  315. # or down by a given number of screenfuls. It notifies the associated
  316. # widget in different ways for old and new command syntaxes.
  317. #
  318. # Arguments:
  319. # w - The scrollbar widget.
  320. # orient - Which kinds of scrollbars this applies to: "h" for
  321. # horizontal, "v" for vertical, "hv" for both.
  322. # amount - How many screens to scroll: typically 1 or -1.
  323. proc ::tk::ScrollByPages {w orient amount} {
  324. set cmd [$w cget -command]
  325. if {$cmd eq "" || ([string first \
  326. [string index [$w cget -orient] 0] $orient] < 0)} {
  327. return
  328. }
  329. set info [$w get]
  330. if {[llength $info] == 2} {
  331. uplevel #0 $cmd scroll $amount pages
  332. } else {
  333. uplevel #0 $cmd [expr {[lindex $info 2] + $amount*([lindex $info 1] - 1)}]
  334. }
  335. }
  336. # ::tk::ScrollToPos --
  337. # This procedure tells the scrollbar's associated widget to scroll to
  338. # a particular location, given by a fraction between 0 and 1. It notifies
  339. # the associated widget in different ways for old and new command syntaxes.
  340. #
  341. # Arguments:
  342. # w - The scrollbar widget.
  343. # pos - A fraction between 0 and 1 indicating a desired position
  344. # in the document.
  345. proc ::tk::ScrollToPos {w pos} {
  346. set cmd [$w cget -command]
  347. if {$cmd eq ""} {
  348. return
  349. }
  350. set info [$w get]
  351. if {[llength $info] == 2} {
  352. uplevel #0 $cmd moveto $pos
  353. } else {
  354. uplevel #0 $cmd [expr {round([lindex $info 0]*$pos)}]
  355. }
  356. }
  357. # ::tk::ScrollTopBottom
  358. # Scroll to the top or bottom of the document, depending on the mouse
  359. # position.
  360. #
  361. # Arguments:
  362. # w - The scrollbar widget.
  363. # x, y - Mouse coordinates within the widget.
  364. proc ::tk::ScrollTopBottom {w x y} {
  365. variable ::tk::Priv
  366. set element [$w identify $x $y]
  367. if {[string match *1 $element]} {
  368. ScrollToPos $w 0
  369. } elseif {[string match *2 $element]} {
  370. ScrollToPos $w 1
  371. }
  372. # Set Priv(relief), since it's needed by tk::ScrollButtonUp.
  373. set Priv(relief) [$w cget -activerelief]
  374. }
  375. # ::tk::ScrollButton2Down
  376. # This procedure is invoked when button 2 is pressed over a scrollbar.
  377. # If the button is over the trough or slider, it sets the scrollbar to
  378. # the mouse position and starts a slider drag. Otherwise it just
  379. # behaves the same as button 1.
  380. #
  381. # Arguments:
  382. # w - The scrollbar widget.
  383. # x, y - Mouse coordinates within the widget.
  384. proc ::tk::ScrollButton2Down {w x y} {
  385. variable ::tk::Priv
  386. set element [$w identify $x $y]
  387. if {[string match {arrow[12]} $element]} {
  388. ScrollButtonDown $w $x $y
  389. return
  390. }
  391. ScrollToPos $w [$w fraction $x $y]
  392. set Priv(relief) [$w cget -activerelief]
  393. # Need the "update idletasks" below so that the widget calls us
  394. # back to reset the actual scrollbar position before we start the
  395. # slider drag.
  396. update idletasks
  397. $w configure -activerelief sunken
  398. $w activate slider
  399. ScrollStartDrag $w $x $y
  400. }