scrlbar.tcl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 © 1994 The Regents of the University of California.
  7. # Copyright © 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 <Button-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 <Button-2> {
  56. tk::ScrollButton2Down %W %x %y
  57. }
  58. bind Scrollbar <B1-Button-2> {
  59. # Do nothing, since button 1 is already down.
  60. }
  61. bind Scrollbar <B2-Button-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-Button-1> {
  83. tk::ScrollTopBottom %W %x %y
  84. }
  85. bind Scrollbar <Control-Button-2> {
  86. tk::ScrollTopBottom %W %x %y
  87. }
  88. bind Scrollbar <<PrevLine>> {
  89. tk::ScrollByUnits %W v -1
  90. }
  91. bind Scrollbar <<NextLine>> {
  92. tk::ScrollByUnits %W v 1
  93. }
  94. bind Scrollbar <<PrevPara>> {
  95. tk::ScrollByPages %W v -1
  96. }
  97. bind Scrollbar <<NextPara>> {
  98. tk::ScrollByPages %W v 1
  99. }
  100. bind Scrollbar <<PrevChar>> {
  101. tk::ScrollByUnits %W h -1
  102. }
  103. bind Scrollbar <<NextChar>> {
  104. tk::ScrollByUnits %W h 1
  105. }
  106. bind Scrollbar <<PrevWord>> {
  107. tk::ScrollByPages %W h -1
  108. }
  109. bind Scrollbar <<NextWord>> {
  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 <<LineStart>> {
  119. tk::ScrollToPos %W 0
  120. }
  121. bind Scrollbar <<LineEnd>> {
  122. tk::ScrollToPos %W 1
  123. }
  124. }
  125. bind Scrollbar <Enter> {+
  126. set tk::Priv(xEvents) 0; set tk::Priv(yEvents) 0
  127. }
  128. bind Scrollbar <MouseWheel> {
  129. tk::ScrollByUnits %W vh %D -40.0
  130. }
  131. bind Scrollbar <Option-MouseWheel> {
  132. tk::ScrollByUnits %W vh %D -12.0
  133. }
  134. bind Scrollbar <Shift-MouseWheel> {
  135. tk::ScrollByUnits %W hv %D -40.0
  136. }
  137. bind Scrollbar <Shift-Option-MouseWheel> {
  138. tk::ScrollByUnits %W hv %D -12.0
  139. }
  140. bind Scrollbar <TouchpadScroll> {
  141. lassign [tk::PreciseScrollDeltas %D] tk::Priv(deltaX) tk::Priv(deltaY)
  142. if {$tk::Priv(deltaX) != 0 && [%W cget -orient] eq "horizontal"} {
  143. tk::ScrollbarScrollByPixels %W h $tk::Priv(deltaX)
  144. }
  145. if {$tk::Priv(deltaY) != 0 && [%W cget -orient] eq "vertical"} {
  146. tk::ScrollbarScrollByPixels %W v $tk::Priv(deltaY)
  147. }
  148. }
  149. # tk::ScrollButtonDown --
  150. # This procedure is invoked when a button is pressed in a scrollbar.
  151. # It changes the way the scrollbar is displayed and takes actions
  152. # depending on where the mouse is.
  153. #
  154. # Arguments:
  155. # w - The scrollbar widget.
  156. # x, y - Mouse coordinates.
  157. proc tk::ScrollButtonDown {w x y} {
  158. variable ::tk::Priv
  159. set Priv(relief) [$w cget -activerelief]
  160. $w configure -activerelief sunken
  161. set element [$w identify $x $y]
  162. if {$element eq "slider"} {
  163. ScrollStartDrag $w $x $y
  164. } else {
  165. ScrollSelect $w $element initial
  166. }
  167. }
  168. # ::tk::ScrollButtonUp --
  169. # This procedure is invoked when a button is released in a scrollbar.
  170. # It cancels scans and auto-repeats that were in progress, and restores
  171. # the way the active element is displayed.
  172. #
  173. # Arguments:
  174. # w - The scrollbar widget.
  175. # x, y - Mouse coordinates.
  176. proc ::tk::ScrollButtonUp {w x y} {
  177. variable ::tk::Priv
  178. tk::CancelRepeat
  179. if {[info exists Priv(relief)]} {
  180. # Avoid error due to spurious release events
  181. $w configure -activerelief $Priv(relief)
  182. ScrollEndDrag $w $x $y
  183. $w activate [$w identify $x $y]
  184. }
  185. }
  186. # ::tk::ScrollSelect --
  187. # This procedure is invoked when a button is pressed over the scrollbar.
  188. # It invokes one of several scrolling actions depending on where in
  189. # the scrollbar the button was pressed.
  190. #
  191. # Arguments:
  192. # w - The scrollbar widget.
  193. # element - The element of the scrollbar that was selected, such
  194. # as "arrow1" or "trough2". Shouldn't be "slider".
  195. # repeat - Whether and how to auto-repeat the action: "noRepeat"
  196. # means don't auto-repeat, "initial" means this is the
  197. # first action in an auto-repeat sequence, and "again"
  198. # means this is the second repetition or later.
  199. proc ::tk::ScrollSelect {w element repeat} {
  200. variable ::tk::Priv
  201. if {![winfo exists $w]} return
  202. switch -- $element {
  203. "arrow1" {ScrollByUnits $w hv -1}
  204. "trough1" {ScrollByPages $w hv -1}
  205. "trough2" {ScrollByPages $w hv 1}
  206. "arrow2" {ScrollByUnits $w hv 1}
  207. default {return}
  208. }
  209. if {$repeat eq "again"} {
  210. set Priv(afterId) [after [$w cget -repeatinterval] \
  211. [list tk::ScrollSelect $w $element again]]
  212. } elseif {$repeat eq "initial"} {
  213. set delay [$w cget -repeatdelay]
  214. if {$delay > 0} {
  215. set Priv(afterId) [after $delay \
  216. [list tk::ScrollSelect $w $element again]]
  217. }
  218. }
  219. }
  220. # ::tk::ScrollStartDrag --
  221. # This procedure is called to initiate a drag of the slider. It just
  222. # remembers the starting position of the mouse and slider.
  223. #
  224. # Arguments:
  225. # w - The scrollbar widget.
  226. # x, y - The mouse position at the start of the drag operation.
  227. proc ::tk::ScrollStartDrag {w x y} {
  228. variable ::tk::Priv
  229. if {[$w cget -command] eq ""} {
  230. return
  231. }
  232. set Priv(pressX) $x
  233. set Priv(pressY) $y
  234. set Priv(initValues) [$w get]
  235. set iv0 [lindex $Priv(initValues) 0]
  236. if {[llength $Priv(initValues)] == 2} {
  237. set Priv(initPos) $iv0
  238. } elseif {$iv0 == 0} {
  239. set Priv(initPos) 0.0
  240. } else {
  241. set Priv(initPos) [expr {(double([lindex $Priv(initValues) 2])) \
  242. / [lindex $Priv(initValues) 0]}]
  243. }
  244. }
  245. # ::tk::ScrollDrag --
  246. # This procedure is called for each mouse motion even when the slider
  247. # is being dragged. It notifies the associated widget if we're not
  248. # jump scrolling, and it just updates the scrollbar if we are jump
  249. # scrolling.
  250. #
  251. # Arguments:
  252. # w - The scrollbar widget.
  253. # x, y - The current mouse position.
  254. proc ::tk::ScrollDrag {w x y} {
  255. variable ::tk::Priv
  256. if {$Priv(initPos) eq ""} {
  257. return
  258. }
  259. set delta [$w delta [expr {$x - $Priv(pressX)}] [expr {$y - $Priv(pressY)}]]
  260. if {[$w cget -jump]} {
  261. if {[llength $Priv(initValues)] == 2} {
  262. $w set [expr {[lindex $Priv(initValues) 0] + $delta}] \
  263. [expr {[lindex $Priv(initValues) 1] + $delta}]
  264. } else {
  265. set delta [expr {round($delta * [lindex $Priv(initValues) 0])}]
  266. eval [list $w] set [lreplace $Priv(initValues) 2 3 \
  267. [expr {[lindex $Priv(initValues) 2] + $delta}] \
  268. [expr {[lindex $Priv(initValues) 3] + $delta}]]
  269. }
  270. } else {
  271. ScrollToPos $w [expr {$Priv(initPos) + $delta}]
  272. }
  273. }
  274. # ::tk::ScrollEndDrag --
  275. # This procedure is called to end an interactive drag of the slider.
  276. # It scrolls the window if we're in jump mode, otherwise it does nothing.
  277. #
  278. # Arguments:
  279. # w - The scrollbar widget.
  280. # x, y - The mouse position at the end of the drag operation.
  281. proc ::tk::ScrollEndDrag {w x y} {
  282. variable ::tk::Priv
  283. if {$Priv(initPos) eq ""} {
  284. return
  285. }
  286. if {[$w cget -jump]} {
  287. set delta [$w delta [expr {$x - $Priv(pressX)}] \
  288. [expr {$y - $Priv(pressY)}]]
  289. ScrollToPos $w [expr {$Priv(initPos) + $delta}]
  290. }
  291. set Priv(initPos) ""
  292. }
  293. # ::tk::ScrollbarScrollByPixels --
  294. # This procedure tells the scrollbar's associated widget to scroll up
  295. # or down by a given number of pixels. It only works with scrollbars
  296. # because it uses the delta command.
  297. #
  298. # Arguments:
  299. # w - The scrollbar widget.
  300. # orient - Which kind of scrollbar this applies to: "h" for
  301. # horizontal, "v" for vertical.
  302. # amount - How many pixels to scroll.
  303. proc ::tk::ScrollbarScrollByPixels {w orient amount} {
  304. set cmd [$w cget -command]
  305. if {$cmd eq ""} {
  306. return
  307. }
  308. set xyview [lindex [split $cmd] end]
  309. if {$orient eq "v"} {
  310. if {$xyview eq "xview"} {
  311. return
  312. }
  313. }
  314. if {$orient eq "h"} {
  315. if {$xyview eq "yview"} {
  316. return
  317. }
  318. }
  319. # The code below works with both the current and old syntax for
  320. # the scrollbar get command.
  321. set info [$w get]
  322. if {[llength $info] == 2} {
  323. set first [lindex $info 0]
  324. } else {
  325. set first [lindex $info 2]
  326. }
  327. set pixels [expr {-$amount}]
  328. uplevel #0 $cmd moveto [expr $first + [$w delta $pixels $pixels]]
  329. }
  330. # ::tk::ScrollByUnits --
  331. # This procedure tells the scrollbar's associated widget to scroll up
  332. # or down by a given number of units. It notifies the associated widget
  333. # in different ways for old and new command syntaxes.
  334. #
  335. # Arguments:
  336. # w - The scrollbar widget.
  337. # orient - Which kinds of scrollbars this applies to: "h" for
  338. # horizontal, "v" for vertical, "hv" or "vh" for both.
  339. # amount - How many units to scroll: typically 1 or -1.
  340. proc ::tk::ScrollByUnits {w orient amount {factor 1.0}} {
  341. set cmd [$w cget -command]
  342. if {$cmd eq "" || ([string first \
  343. [string index [$w cget -orient] 0] $orient] < 0)} {
  344. return
  345. }
  346. if {[string length $orient] == 2 && $factor != 1.0} {
  347. # Count both the <MouseWheel> and <Shift-MouseWheel>
  348. # events, and ignore the non-dominant ones
  349. variable ::tk::Priv
  350. set axis [expr {[string index $orient 0] eq "h" ? "x" : "y"}]
  351. incr Priv(${axis}Events)
  352. if {($Priv(xEvents) + $Priv(yEvents) > 10) &&
  353. ($axis eq "x" && $Priv(xEvents) < $Priv(yEvents) ||
  354. $axis eq "y" && $Priv(yEvents) < $Priv(xEvents))} {
  355. return
  356. }
  357. }
  358. set info [$w get]
  359. if {[llength $info] == 2} {
  360. uplevel #0 $cmd scroll [expr {$amount/$factor}] units
  361. } else {
  362. uplevel #0 $cmd [expr {[lindex $info 2] + [expr {$amount/$factor}]}]
  363. }
  364. }
  365. # ::tk::ScrollByPages --
  366. # This procedure tells the scrollbar's associated widget to scroll up
  367. # or down by a given number of screenfuls. It notifies the associated
  368. # widget in different ways for old and new command syntaxes.
  369. #
  370. # Arguments:
  371. # w - The scrollbar widget.
  372. # orient - Which kinds of scrollbars this applies to: "h" for
  373. # horizontal, "v" for vertical, "hv" for both.
  374. # amount - How many screens to scroll: typically 1 or -1.
  375. proc ::tk::ScrollByPages {w orient amount} {
  376. set cmd [$w cget -command]
  377. if {$cmd eq "" || ([string first \
  378. [string index [$w cget -orient] 0] $orient] < 0)} {
  379. return
  380. }
  381. set info [$w get]
  382. if {[llength $info] == 2} {
  383. uplevel #0 $cmd scroll $amount pages
  384. } else {
  385. uplevel #0 $cmd [expr {[lindex $info 2] + $amount*([lindex $info 1] - 1)}]
  386. }
  387. }
  388. # ::tk::ScrollToPos --
  389. # This procedure tells the scrollbar's associated widget to scroll to
  390. # a particular location, given by a fraction between 0 and 1. It notifies
  391. # the associated widget in different ways for old and new command syntaxes.
  392. #
  393. # Arguments:
  394. # w - The scrollbar widget.
  395. # pos - A fraction between 0 and 1 indicating a desired position
  396. # in the document.
  397. proc ::tk::ScrollToPos {w pos} {
  398. set cmd [$w cget -command]
  399. if {$cmd eq ""} {
  400. return
  401. }
  402. set info [$w get]
  403. if {[llength $info] == 2} {
  404. uplevel #0 $cmd moveto $pos
  405. } else {
  406. uplevel #0 $cmd [expr {round([lindex $info 0]*$pos)}]
  407. }
  408. }
  409. # ::tk::ScrollTopBottom
  410. # Scroll to the top or bottom of the document, depending on the mouse
  411. # position.
  412. #
  413. # Arguments:
  414. # w - The scrollbar widget.
  415. # x, y - Mouse coordinates within the widget.
  416. proc ::tk::ScrollTopBottom {w x y} {
  417. variable ::tk::Priv
  418. set element [$w identify $x $y]
  419. if {[string match *1 $element]} {
  420. ScrollToPos $w 0
  421. } elseif {[string match *2 $element]} {
  422. ScrollToPos $w 1
  423. }
  424. # Set Priv(relief), since it's needed by tk::ScrollButtonUp.
  425. set Priv(relief) [$w cget -activerelief]
  426. }
  427. # ::tk::ScrollButton2Down
  428. # This procedure is invoked when button 2 is pressed over a scrollbar.
  429. # If the button is over the trough or slider, it sets the scrollbar to
  430. # the mouse position and starts a slider drag. Otherwise it just
  431. # behaves the same as button 1.
  432. #
  433. # Arguments:
  434. # w - The scrollbar widget.
  435. # x, y - Mouse coordinates within the widget.
  436. proc ::tk::ScrollButton2Down {w x y} {
  437. variable ::tk::Priv
  438. if {![winfo exists $w]} {
  439. return
  440. }
  441. set element [$w identify $x $y]
  442. if {[string match {arrow[12]} $element]} {
  443. ScrollButtonDown $w $x $y
  444. return
  445. }
  446. ScrollToPos $w [$w fraction $x $y]
  447. set Priv(relief) [$w cget -activerelief]
  448. # Need the "update idletasks" below so that the widget calls us
  449. # back to reset the actual scrollbar position before we start the
  450. # slider drag.
  451. update idletasks
  452. if {[winfo exists $w]} {
  453. $w configure -activerelief sunken
  454. $w activate slider
  455. ScrollStartDrag $w $x $y
  456. }
  457. }