scrlbar.tcl 12 KB

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