listbox.tcl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 (c) 1994 The Regents of the University of California.
  7. # Copyright (c) 1994-1995 Sun Microsystems, Inc.
  8. # Copyright (c) 1998 by 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 <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-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-1> {
  51. tk::ListboxBeginExtend %W [%W index @%x,%y]
  52. }
  53. bind Listbox <Control-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 <2> {
  163. %W scan mark %x %y
  164. }
  165. bind Listbox <B2-Motion> {
  166. %W scan dragto %x %y
  167. }
  168. # The MouseWheel will typically only fire on Windows and Mac OS X.
  169. # However, someone could use the "event generate" command to produce
  170. # one on other platforms.
  171. if {[tk windowingsystem] eq "aqua"} {
  172. bind Listbox <MouseWheel> {
  173. %W yview scroll [expr {- (%D)}] units
  174. }
  175. bind Listbox <Option-MouseWheel> {
  176. %W yview scroll [expr {-10 * (%D)}] units
  177. }
  178. bind Listbox <Shift-MouseWheel> {
  179. %W xview scroll [expr {- (%D)}] units
  180. }
  181. bind Listbox <Shift-Option-MouseWheel> {
  182. %W xview scroll [expr {-10 * (%D)}] units
  183. }
  184. } else {
  185. bind Listbox <MouseWheel> {
  186. %W yview scroll [expr {- (%D / 120) * 4}] units
  187. }
  188. bind Listbox <Shift-MouseWheel> {
  189. %W xview scroll [expr {- (%D / 120) * 4}] units
  190. }
  191. }
  192. if {"x11" eq [tk windowingsystem]} {
  193. # Support for mousewheels on Linux/Unix commonly comes through mapping
  194. # the wheel to the extended buttons. If you have a mousewheel, find
  195. # Linux configuration info at:
  196. # http://www.inria.fr/koala/colas/mouse-wheel-scroll/
  197. bind Listbox <4> {
  198. if {!$tk_strictMotif} {
  199. %W yview scroll -5 units
  200. }
  201. }
  202. bind Listbox <Shift-4> {
  203. if {!$tk_strictMotif} {
  204. %W xview scroll -5 units
  205. }
  206. }
  207. bind Listbox <5> {
  208. if {!$tk_strictMotif} {
  209. %W yview scroll 5 units
  210. }
  211. }
  212. bind Listbox <Shift-5> {
  213. if {!$tk_strictMotif} {
  214. %W xview scroll 5 units
  215. }
  216. }
  217. }
  218. # ::tk::ListboxBeginSelect --
  219. #
  220. # This procedure is typically invoked on button-1 presses. It begins
  221. # the process of making a selection in the listbox. Its exact behavior
  222. # depends on the selection mode currently in effect for the listbox;
  223. # see the Motif documentation for details.
  224. #
  225. # Arguments:
  226. # w - The listbox widget.
  227. # el - The element for the selection operation (typically the
  228. # one under the pointer). Must be in numerical form.
  229. proc ::tk::ListboxBeginSelect {w el {focus 1}} {
  230. variable ::tk::Priv
  231. if {[$w cget -selectmode] eq "multiple"} {
  232. if {[$w selection includes $el]} {
  233. $w selection clear $el
  234. } else {
  235. $w selection set $el
  236. }
  237. } else {
  238. $w selection clear 0 end
  239. $w selection set $el
  240. $w selection anchor $el
  241. set Priv(listboxSelection) {}
  242. set Priv(listboxPrev) $el
  243. }
  244. tk::FireListboxSelectEvent $w
  245. # check existence as ListboxSelect may destroy us
  246. if {$focus && [winfo exists $w] && [$w cget -state] eq "normal"} {
  247. focus $w
  248. }
  249. }
  250. # ::tk::ListboxMotion --
  251. #
  252. # This procedure is called to process mouse motion events while
  253. # button 1 is down. It may move or extend the selection, depending
  254. # on the listbox's selection mode.
  255. #
  256. # Arguments:
  257. # w - The listbox widget.
  258. # el - The element under the pointer (must be a number).
  259. proc ::tk::ListboxMotion {w el} {
  260. variable ::tk::Priv
  261. if {$el == $Priv(listboxPrev)} {
  262. return
  263. }
  264. set anchor [$w index anchor]
  265. switch [$w cget -selectmode] {
  266. browse {
  267. $w selection clear 0 end
  268. $w selection set $el
  269. set Priv(listboxPrev) $el
  270. tk::FireListboxSelectEvent $w
  271. }
  272. extended {
  273. set i $Priv(listboxPrev)
  274. if {$i eq ""} {
  275. set i $el
  276. $w selection set $el
  277. }
  278. if {[$w selection includes anchor]} {
  279. $w selection clear $i $el
  280. $w selection set anchor $el
  281. } else {
  282. $w selection clear $i $el
  283. $w selection clear anchor $el
  284. }
  285. if {![info exists Priv(listboxSelection)]} {
  286. set Priv(listboxSelection) [$w curselection]
  287. }
  288. while {($i < $el) && ($i < $anchor)} {
  289. if {[lsearch $Priv(listboxSelection) $i] >= 0} {
  290. $w selection set $i
  291. }
  292. incr i
  293. }
  294. while {($i > $el) && ($i > $anchor)} {
  295. if {[lsearch $Priv(listboxSelection) $i] >= 0} {
  296. $w selection set $i
  297. }
  298. incr i -1
  299. }
  300. set Priv(listboxPrev) $el
  301. tk::FireListboxSelectEvent $w
  302. }
  303. }
  304. }
  305. # ::tk::ListboxBeginExtend --
  306. #
  307. # This procedure is typically invoked on shift-button-1 presses. It
  308. # begins the process of extending a selection in the listbox. Its
  309. # exact behavior depends on the selection mode currently in effect
  310. # for the listbox; see the Motif documentation for details.
  311. #
  312. # Arguments:
  313. # w - The listbox widget.
  314. # el - The element for the selection operation (typically the
  315. # one under the pointer). Must be in numerical form.
  316. proc ::tk::ListboxBeginExtend {w el} {
  317. if {[$w cget -selectmode] eq "extended"} {
  318. if {[$w selection includes anchor]} {
  319. ListboxMotion $w $el
  320. } else {
  321. # No selection yet; simulate the begin-select operation.
  322. ListboxBeginSelect $w $el
  323. }
  324. }
  325. }
  326. # ::tk::ListboxBeginToggle --
  327. #
  328. # This procedure is typically invoked on control-button-1 presses. It
  329. # begins the process of toggling a selection in the listbox. Its
  330. # exact behavior depends on the selection mode currently in effect
  331. # for the listbox; see the Motif documentation for details.
  332. #
  333. # Arguments:
  334. # w - The listbox widget.
  335. # el - The element for the selection operation (typically the
  336. # one under the pointer). Must be in numerical form.
  337. proc ::tk::ListboxBeginToggle {w el} {
  338. variable ::tk::Priv
  339. if {[$w cget -selectmode] eq "extended"} {
  340. set Priv(listboxSelection) [$w curselection]
  341. set Priv(listboxPrev) $el
  342. $w selection anchor $el
  343. if {[$w selection includes $el]} {
  344. $w selection clear $el
  345. } else {
  346. $w selection set $el
  347. }
  348. tk::FireListboxSelectEvent $w
  349. }
  350. }
  351. # ::tk::ListboxAutoScan --
  352. # This procedure is invoked when the mouse leaves an entry window
  353. # with button 1 down. It scrolls the window up, down, left, or
  354. # right, depending on where the mouse left the window, and reschedules
  355. # itself as an "after" command so that the window continues to scroll until
  356. # the mouse moves back into the window or the mouse button is released.
  357. #
  358. # Arguments:
  359. # w - The entry window.
  360. proc ::tk::ListboxAutoScan {w} {
  361. variable ::tk::Priv
  362. if {![winfo exists $w]} return
  363. set x $Priv(x)
  364. set y $Priv(y)
  365. if {$y >= [winfo height $w]} {
  366. $w yview scroll 1 units
  367. } elseif {$y < 0} {
  368. $w yview scroll -1 units
  369. } elseif {$x >= [winfo width $w]} {
  370. $w xview scroll 2 units
  371. } elseif {$x < 0} {
  372. $w xview scroll -2 units
  373. } else {
  374. return
  375. }
  376. ListboxMotion $w [$w index @$x,$y]
  377. set Priv(afterId) [after 50 [list tk::ListboxAutoScan $w]]
  378. }
  379. # ::tk::ListboxUpDown --
  380. #
  381. # Moves the location cursor (active element) up or down by one element,
  382. # and changes the selection if we're in browse or extended selection
  383. # mode.
  384. #
  385. # Arguments:
  386. # w - The listbox widget.
  387. # amount - +1 to move down one item, -1 to move back one item.
  388. proc ::tk::ListboxUpDown {w amount} {
  389. variable ::tk::Priv
  390. $w activate [expr {[$w index active] + $amount}]
  391. $w see active
  392. switch [$w cget -selectmode] {
  393. browse {
  394. $w selection clear 0 end
  395. $w selection set active
  396. tk::FireListboxSelectEvent $w
  397. }
  398. extended {
  399. $w selection clear 0 end
  400. $w selection set active
  401. $w selection anchor active
  402. set Priv(listboxPrev) [$w index active]
  403. set Priv(listboxSelection) {}
  404. tk::FireListboxSelectEvent $w
  405. }
  406. }
  407. }
  408. # ::tk::ListboxExtendUpDown --
  409. #
  410. # Does nothing unless we're in extended selection mode; in this
  411. # case it moves the location cursor (active element) up or down by
  412. # one element, and extends the selection to that point.
  413. #
  414. # Arguments:
  415. # w - The listbox widget.
  416. # amount - +1 to move down one item, -1 to move back one item.
  417. proc ::tk::ListboxExtendUpDown {w amount} {
  418. variable ::tk::Priv
  419. if {[$w cget -selectmode] ne "extended"} {
  420. return
  421. }
  422. set active [$w index active]
  423. if {![info exists Priv(listboxSelection)]} {
  424. $w selection set $active
  425. set Priv(listboxSelection) [$w curselection]
  426. }
  427. $w activate [expr {$active + $amount}]
  428. $w see active
  429. ListboxMotion $w [$w index active]
  430. }
  431. # ::tk::ListboxDataExtend
  432. #
  433. # This procedure is called for key-presses such as Shift-KEndData.
  434. # If the selection mode isn't multiple or extend then it does nothing.
  435. # Otherwise it moves the active element to el and, if we're in
  436. # extended mode, extends the selection to that point.
  437. #
  438. # Arguments:
  439. # w - The listbox widget.
  440. # el - An integer element number.
  441. proc ::tk::ListboxDataExtend {w el} {
  442. set mode [$w cget -selectmode]
  443. if {$mode eq "extended"} {
  444. $w activate $el
  445. $w see $el
  446. if {[$w selection includes anchor]} {
  447. ListboxMotion $w $el
  448. }
  449. } elseif {$mode eq "multiple"} {
  450. $w activate $el
  451. $w see $el
  452. }
  453. }
  454. # ::tk::ListboxCancel
  455. #
  456. # This procedure is invoked to cancel an extended selection in
  457. # progress. If there is an extended selection in progress, it
  458. # restores all of the items between the active one and the anchor
  459. # to their previous selection state.
  460. #
  461. # Arguments:
  462. # w - The listbox widget.
  463. proc ::tk::ListboxCancel w {
  464. variable ::tk::Priv
  465. if {[$w cget -selectmode] ne "extended"} {
  466. return
  467. }
  468. set first [$w index anchor]
  469. set last $Priv(listboxPrev)
  470. if {$last eq ""} {
  471. # Not actually doing any selection right now
  472. return
  473. }
  474. if {$first > $last} {
  475. set tmp $first
  476. set first $last
  477. set last $tmp
  478. }
  479. $w selection clear $first $last
  480. while {$first <= $last} {
  481. if {[lsearch $Priv(listboxSelection) $first] >= 0} {
  482. $w selection set $first
  483. }
  484. incr first
  485. }
  486. tk::FireListboxSelectEvent $w
  487. }
  488. # ::tk::ListboxSelectAll
  489. #
  490. # This procedure is invoked to handle the "select all" operation.
  491. # For single and browse mode, it just selects the active element.
  492. # Otherwise it selects everything in the widget.
  493. #
  494. # Arguments:
  495. # w - The listbox widget.
  496. proc ::tk::ListboxSelectAll w {
  497. set mode [$w cget -selectmode]
  498. if {$mode eq "single" || $mode eq "browse"} {
  499. $w selection clear 0 end
  500. $w selection set active
  501. } else {
  502. $w selection set 0 end
  503. }
  504. tk::FireListboxSelectEvent $w
  505. }
  506. # ::tk::FireListboxSelectEvent
  507. #
  508. # Fire the <<ListboxSelect>> event if the listbox is not in disabled
  509. # state.
  510. #
  511. # Arguments:
  512. # w - The listbox widget.
  513. proc ::tk::FireListboxSelectEvent w {
  514. if {[$w cget -state] eq "normal"} {
  515. event generate $w <<ListboxSelect>>
  516. }
  517. }