listbox.tcl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. # listbox.tcl --
  2. #
  3. # This file defines the default bindings for Tk listbox widgets
  4. # and provides procedures that help in implementing those bindings.
  5. #
  6. # Copyright © 1994 The Regents of the University of California.
  7. # Copyright © 1994-1995 Sun Microsystems, Inc.
  8. # Copyright © 1998 Scriptics Corporation.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #--------------------------------------------------------------------------
  13. # tk::Priv elements used in this file:
  14. #
  15. # afterId - Token returned by "after" for autoscanning.
  16. # listboxPrev - The last element to be selected or deselected
  17. # during a selection operation.
  18. # listboxSelection - All of the items that were selected before the
  19. # current selection operation (such as a mouse
  20. # drag) started; used to cancel an operation.
  21. #--------------------------------------------------------------------------
  22. #-------------------------------------------------------------------------
  23. # The code below creates the default class bindings for listboxes.
  24. #-------------------------------------------------------------------------
  25. # Note: the check for existence of %W below is because this binding
  26. # is sometimes invoked after a window has been deleted (e.g. because
  27. # there is a double-click binding on the widget that deletes it). Users
  28. # can put "break"s in their bindings to avoid the error, but this check
  29. # makes that unnecessary.
  30. bind Listbox <Button-1> {
  31. if {[winfo exists %W]} {
  32. tk::ListboxBeginSelect %W [%W index @%x,%y] 1
  33. }
  34. }
  35. # Ignore double clicks so that users can define their own behaviors.
  36. # Among other things, this prevents errors if the user deletes the
  37. # listbox on a double click.
  38. bind Listbox <Double-Button-1> {
  39. # Empty script
  40. }
  41. bind Listbox <B1-Motion> {
  42. set tk::Priv(x) %x
  43. set tk::Priv(y) %y
  44. tk::ListboxMotion %W [%W index @%x,%y]
  45. }
  46. bind Listbox <ButtonRelease-1> {
  47. tk::CancelRepeat
  48. %W activate @%x,%y
  49. }
  50. bind Listbox <Shift-Button-1> {
  51. tk::ListboxBeginExtend %W [%W index @%x,%y]
  52. }
  53. bind Listbox <Control-Button-1> {
  54. tk::ListboxBeginToggle %W [%W index @%x,%y]
  55. }
  56. bind Listbox <B1-Leave> {
  57. set tk::Priv(x) %x
  58. set tk::Priv(y) %y
  59. tk::ListboxAutoScan %W
  60. }
  61. bind Listbox <B1-Enter> {
  62. tk::CancelRepeat
  63. }
  64. bind Listbox <<PrevLine>> {
  65. tk::ListboxUpDown %W -1
  66. }
  67. bind Listbox <<SelectPrevLine>> {
  68. tk::ListboxExtendUpDown %W -1
  69. }
  70. bind Listbox <<NextLine>> {
  71. tk::ListboxUpDown %W 1
  72. }
  73. bind Listbox <<SelectNextLine>> {
  74. tk::ListboxExtendUpDown %W 1
  75. }
  76. bind Listbox <<PrevChar>> {
  77. %W xview scroll -1 units
  78. }
  79. bind Listbox <<PrevWord>> {
  80. %W xview scroll -1 pages
  81. }
  82. bind Listbox <<NextChar>> {
  83. %W xview scroll 1 units
  84. }
  85. bind Listbox <<NextWord>> {
  86. %W xview scroll 1 pages
  87. }
  88. bind Listbox <Prior> {
  89. %W yview scroll -1 pages
  90. %W activate @0,0
  91. }
  92. bind Listbox <Next> {
  93. %W yview scroll 1 pages
  94. %W activate @0,0
  95. }
  96. bind Listbox <Control-Prior> {
  97. %W xview scroll -1 pages
  98. }
  99. bind Listbox <Control-Next> {
  100. %W xview scroll 1 pages
  101. }
  102. bind Listbox <<LineStart>> {
  103. %W xview moveto 0
  104. }
  105. bind Listbox <<LineEnd>> {
  106. %W xview moveto 1
  107. }
  108. bind Listbox <Control-Home> {
  109. %W activate 0
  110. %W see 0
  111. %W selection clear 0 end
  112. %W selection set 0
  113. tk::FireListboxSelectEvent %W
  114. }
  115. bind Listbox <Control-Shift-Home> {
  116. tk::ListboxDataExtend %W 0
  117. }
  118. bind Listbox <Control-End> {
  119. %W activate end
  120. %W see end
  121. %W selection clear 0 end
  122. %W selection set end
  123. tk::FireListboxSelectEvent %W
  124. }
  125. bind Listbox <Control-Shift-End> {
  126. tk::ListboxDataExtend %W [%W index end]
  127. }
  128. bind Listbox <<Copy>> {
  129. if {[selection own -displayof %W] eq "%W"} {
  130. clipboard clear -displayof %W
  131. clipboard append -displayof %W [selection get -displayof %W]
  132. }
  133. }
  134. bind Listbox <space> {
  135. tk::ListboxBeginSelect %W [%W index active]
  136. }
  137. bind Listbox <<Invoke>> {
  138. tk::ListboxBeginSelect %W [%W index active]
  139. }
  140. bind Listbox <Select> {
  141. tk::ListboxBeginSelect %W [%W index active]
  142. }
  143. bind Listbox <Control-Shift-space> {
  144. tk::ListboxBeginExtend %W [%W index active]
  145. }
  146. bind Listbox <Shift-Select> {
  147. tk::ListboxBeginExtend %W [%W index active]
  148. }
  149. bind Listbox <Escape> {
  150. tk::ListboxCancel %W
  151. }
  152. bind Listbox <<SelectAll>> {
  153. tk::ListboxSelectAll %W
  154. }
  155. bind Listbox <<SelectNone>> {
  156. if {[%W cget -selectmode] ne "browse"} {
  157. %W selection clear 0 end
  158. tk::FireListboxSelectEvent %W
  159. }
  160. }
  161. # Additional Tk bindings that aren't part of the Motif look and feel:
  162. bind Listbox <Button-2> {
  163. %W scan mark %x %y
  164. }
  165. bind Listbox <B2-Motion> {
  166. %W scan dragto %x %y
  167. }
  168. bind Listbox <MouseWheel> {
  169. tk::MouseWheel %W y %D -40.0 units
  170. }
  171. bind Listbox <Option-MouseWheel> {
  172. tk::MouseWheel %W y %D -12.0 units
  173. }
  174. bind Listbox <Shift-MouseWheel> {
  175. tk::MouseWheel %W x %D -40.0 units
  176. }
  177. bind Listbox <Shift-Option-MouseWheel> {
  178. tk::MouseWheel %W x %D -12.0 units
  179. }
  180. bind Listbox <TouchpadScroll> {
  181. if {%# %% 5 == 0} {
  182. lassign [tk::PreciseScrollDeltas %D] tk::Priv(deltaX) tk::Priv(deltaY)
  183. if {$tk::Priv(deltaX) != 0} {
  184. %W xview scroll [expr {-$tk::Priv(deltaX)}] units
  185. }
  186. if {$tk::Priv(deltaY) != 0} {
  187. %W yview scroll [expr {-$tk::Priv(deltaY)}] units
  188. }
  189. }
  190. }
  191. # ::tk::ListboxBeginSelect --
  192. #
  193. # This procedure is typically invoked on button-1 presses. It begins
  194. # the process of making a selection in the listbox. Its exact behavior
  195. # depends on the selection mode currently in effect for the listbox;
  196. # see the Motif documentation for details.
  197. #
  198. # Arguments:
  199. # w - The listbox widget.
  200. # el - The element for the selection operation (typically the
  201. # one under the pointer). Must be in numerical form.
  202. proc ::tk::ListboxBeginSelect {w el {focus 1}} {
  203. variable ::tk::Priv
  204. if {[$w cget -selectmode] eq "multiple"} {
  205. if {[$w selection includes $el]} {
  206. $w selection clear $el
  207. } else {
  208. $w selection set $el
  209. }
  210. } else {
  211. $w selection clear 0 end
  212. $w selection set $el
  213. $w selection anchor $el
  214. set Priv(listboxSelection) {}
  215. set Priv(listboxPrev) $el
  216. }
  217. tk::FireListboxSelectEvent $w
  218. # check existence as ListboxSelect may destroy us
  219. if {$focus && [winfo exists $w] && [$w cget -state] eq "normal"} {
  220. focus $w
  221. }
  222. }
  223. # ::tk::ListboxMotion --
  224. #
  225. # This procedure is called to process mouse motion events while
  226. # button 1 is down. It may move or extend the selection, depending
  227. # on the listbox's selection mode.
  228. #
  229. # Arguments:
  230. # w - The listbox widget.
  231. # el - The element under the pointer (must be a number).
  232. proc ::tk::ListboxMotion {w el} {
  233. variable ::tk::Priv
  234. if {$el == $Priv(listboxPrev)} {
  235. return
  236. }
  237. set anchor [$w index anchor]
  238. switch [$w cget -selectmode] {
  239. browse {
  240. $w selection clear 0 end
  241. $w selection set $el
  242. set Priv(listboxPrev) $el
  243. tk::FireListboxSelectEvent $w
  244. }
  245. extended {
  246. set i $Priv(listboxPrev)
  247. if {$i < 0} {
  248. set i $el
  249. $w selection set $el
  250. }
  251. if {[$w selection includes anchor]} {
  252. $w selection clear $i $el
  253. $w selection set anchor $el
  254. } else {
  255. $w selection clear $i $el
  256. $w selection clear anchor $el
  257. }
  258. if {![info exists Priv(listboxSelection)]} {
  259. set Priv(listboxSelection) [$w curselection]
  260. }
  261. while {($i < $el) && ($i < $anchor)} {
  262. if {$i in $Priv(listboxSelection)} {
  263. $w selection set $i
  264. }
  265. incr i
  266. }
  267. while {($i > $el) && ($i > $anchor)} {
  268. if {$i in $Priv(listboxSelection)} {
  269. $w selection set $i
  270. }
  271. incr i -1
  272. }
  273. set Priv(listboxPrev) $el
  274. tk::FireListboxSelectEvent $w
  275. }
  276. }
  277. }
  278. # ::tk::ListboxBeginExtend --
  279. #
  280. # This procedure is typically invoked on shift-button-1 presses. It
  281. # begins the process of extending a selection in the listbox. Its
  282. # exact behavior depends on the selection mode currently in effect
  283. # for the listbox; see the Motif documentation for details.
  284. #
  285. # Arguments:
  286. # w - The listbox widget.
  287. # el - The element for the selection operation (typically the
  288. # one under the pointer). Must be in numerical form.
  289. proc ::tk::ListboxBeginExtend {w el} {
  290. if {[$w cget -selectmode] eq "extended"} {
  291. if {[$w selection includes anchor]} {
  292. ListboxMotion $w $el
  293. } else {
  294. # No selection yet; simulate the begin-select operation.
  295. ListboxBeginSelect $w $el
  296. }
  297. }
  298. }
  299. # ::tk::ListboxBeginToggle --
  300. #
  301. # This procedure is typically invoked on control-button-1 presses. It
  302. # begins the process of toggling a selection in the listbox. Its
  303. # exact behavior depends on the selection mode currently in effect
  304. # for the listbox; see the Motif documentation for details.
  305. #
  306. # Arguments:
  307. # w - The listbox widget.
  308. # el - The element for the selection operation (typically the
  309. # one under the pointer). Must be in numerical form.
  310. proc ::tk::ListboxBeginToggle {w el} {
  311. variable ::tk::Priv
  312. if {[$w cget -selectmode] eq "extended"} {
  313. set Priv(listboxSelection) [$w curselection]
  314. set Priv(listboxPrev) $el
  315. $w selection anchor $el
  316. if {[$w selection includes $el]} {
  317. $w selection clear $el
  318. } else {
  319. $w selection set $el
  320. }
  321. tk::FireListboxSelectEvent $w
  322. }
  323. }
  324. # ::tk::ListboxAutoScan --
  325. # This procedure is invoked when the mouse leaves an entry window
  326. # with button 1 down. It scrolls the window up, down, left, or
  327. # right, depending on where the mouse left the window, and reschedules
  328. # itself as an "after" command so that the window continues to scroll until
  329. # the mouse moves back into the window or the mouse button is released.
  330. #
  331. # Arguments:
  332. # w - The entry window.
  333. proc ::tk::ListboxAutoScan {w} {
  334. variable ::tk::Priv
  335. if {![winfo exists $w]} return
  336. set x $Priv(x)
  337. set y $Priv(y)
  338. if {$y >= [winfo height $w]} {
  339. $w yview scroll 1 units
  340. } elseif {$y < 0} {
  341. $w yview scroll -1 units
  342. } elseif {$x >= [winfo width $w]} {
  343. $w xview scroll 2 units
  344. } elseif {$x < 0} {
  345. $w xview scroll -2 units
  346. } else {
  347. return
  348. }
  349. ListboxMotion $w [$w index @$x,$y]
  350. set Priv(afterId) [after 50 [list tk::ListboxAutoScan $w]]
  351. }
  352. # ::tk::ListboxUpDown --
  353. #
  354. # Moves the location cursor (active element) up or down by one element,
  355. # and changes the selection if we're in browse or extended selection
  356. # mode.
  357. #
  358. # Arguments:
  359. # w - The listbox widget.
  360. # amount - +1 to move down one item, -1 to move back one item.
  361. proc ::tk::ListboxUpDown {w amount} {
  362. variable ::tk::Priv
  363. $w activate [expr {[$w index active] + $amount}]
  364. $w see active
  365. switch [$w cget -selectmode] {
  366. browse {
  367. $w selection clear 0 end
  368. $w selection set active
  369. tk::FireListboxSelectEvent $w
  370. }
  371. extended {
  372. $w selection clear 0 end
  373. $w selection set active
  374. $w selection anchor active
  375. set Priv(listboxPrev) [$w index active]
  376. set Priv(listboxSelection) {}
  377. tk::FireListboxSelectEvent $w
  378. }
  379. }
  380. }
  381. # ::tk::ListboxExtendUpDown --
  382. #
  383. # Does nothing unless we're in extended selection mode; in this
  384. # case it moves the location cursor (active element) up or down by
  385. # one element, and extends the selection to that point.
  386. #
  387. # Arguments:
  388. # w - The listbox widget.
  389. # amount - +1 to move down one item, -1 to move back one item.
  390. proc ::tk::ListboxExtendUpDown {w amount} {
  391. variable ::tk::Priv
  392. if {[$w cget -selectmode] ne "extended"} {
  393. return
  394. }
  395. set active [$w index active]
  396. if {![info exists Priv(listboxSelection)]} {
  397. $w selection set $active
  398. set Priv(listboxSelection) [$w curselection]
  399. }
  400. $w activate [expr {$active + $amount}]
  401. $w see active
  402. ListboxMotion $w [$w index active]
  403. }
  404. # ::tk::ListboxDataExtend
  405. #
  406. # This procedure is called for key-presses such as Shift-KEndData.
  407. # If the selection mode isn't multiple or extend then it does nothing.
  408. # Otherwise it moves the active element to el and, if we're in
  409. # extended mode, extends the selection to that point.
  410. #
  411. # Arguments:
  412. # w - The listbox widget.
  413. # el - An integer element number.
  414. proc ::tk::ListboxDataExtend {w el} {
  415. set mode [$w cget -selectmode]
  416. if {$mode eq "extended"} {
  417. $w activate $el
  418. $w see $el
  419. if {[$w selection includes anchor]} {
  420. ListboxMotion $w $el
  421. }
  422. } elseif {$mode eq "multiple"} {
  423. $w activate $el
  424. $w see $el
  425. }
  426. }
  427. # ::tk::ListboxCancel
  428. #
  429. # This procedure is invoked to cancel an extended selection in
  430. # progress. If there is an extended selection in progress, it
  431. # restores all of the items between the active one and the anchor
  432. # to their previous selection state.
  433. #
  434. # Arguments:
  435. # w - The listbox widget.
  436. proc ::tk::ListboxCancel w {
  437. variable ::tk::Priv
  438. if {[$w cget -selectmode] ne "extended"} {
  439. return
  440. }
  441. set first [$w index anchor]
  442. set last $Priv(listboxPrev)
  443. if {$last < 0} {
  444. # Not actually doing any selection right now
  445. return
  446. }
  447. if {$first > $last} {
  448. set tmp $first
  449. set first $last
  450. set last $tmp
  451. }
  452. $w selection clear $first $last
  453. while {$first <= $last} {
  454. if {$first in $Priv(listboxSelection)} {
  455. $w selection set $first
  456. }
  457. incr first
  458. }
  459. tk::FireListboxSelectEvent $w
  460. }
  461. # ::tk::ListboxSelectAll
  462. #
  463. # This procedure is invoked to handle the "select all" operation.
  464. # For single and browse mode, it just selects the active element.
  465. # Otherwise it selects everything in the widget.
  466. #
  467. # Arguments:
  468. # w - The listbox widget.
  469. proc ::tk::ListboxSelectAll w {
  470. set mode [$w cget -selectmode]
  471. if {$mode eq "single" || $mode eq "browse"} {
  472. $w selection clear 0 end
  473. $w selection set active
  474. } else {
  475. $w selection set 0 end
  476. }
  477. tk::FireListboxSelectEvent $w
  478. }
  479. # ::tk::FireListboxSelectEvent
  480. #
  481. # Fire the <<ListboxSelect>> event if the listbox is not in disabled
  482. # state.
  483. #
  484. # Arguments:
  485. # w - The listbox widget.
  486. proc ::tk::FireListboxSelectEvent w {
  487. if {[$w cget -state] eq "normal"} {
  488. event generate $w <<ListboxSelect>>
  489. }
  490. }