listbox.tcl 14 KB

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