scrlbar.tcl 12 KB

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