Update tk to version 8.5.11
[git/jnareb-git.git] / mingw / lib / tk8.5 / listbox.tcl
blobf3434a5c38b88094dc8e546f213d7e6514b7b44f
1 # listbox.tcl --
3 # This file defines the default bindings for Tk listbox widgets
4 # and provides procedures that help in implementing those bindings.
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.
10 # See the file "license.terms" for information on usage and redistribution
11 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 #--------------------------------------------------------------------------
14 # tk::Priv elements used in this file:
16 # afterId - Token returned by "after" for autoscanning.
17 # listboxPrev - The last element to be selected or deselected
18 # during a selection operation.
19 # listboxSelection - All of the items that were selected before the
20 # current selection operation (such as a mouse
21 # drag) started; used to cancel an operation.
22 #--------------------------------------------------------------------------
24 #-------------------------------------------------------------------------
25 # The code below creates the default class bindings for listboxes.
26 #-------------------------------------------------------------------------
28 # Note: the check for existence of %W below is because this binding
29 # is sometimes invoked after a window has been deleted (e.g. because
30 # there is a double-click binding on the widget that deletes it). Users
31 # can put "break"s in their bindings to avoid the error, but this check
32 # makes that unnecessary.
34 bind Listbox <1> {
35 if {[winfo exists %W]} {
36 tk::ListboxBeginSelect %W [%W index @%x,%y] 1
40 # Ignore double clicks so that users can define their own behaviors.
41 # Among other things, this prevents errors if the user deletes the
42 # listbox on a double click.
44 bind Listbox <Double-1> {
45 # Empty script
48 bind Listbox <B1-Motion> {
49 set tk::Priv(x) %x
50 set tk::Priv(y) %y
51 tk::ListboxMotion %W [%W index @%x,%y]
53 bind Listbox <ButtonRelease-1> {
54 tk::CancelRepeat
55 %W activate @%x,%y
57 bind Listbox <Shift-1> {
58 tk::ListboxBeginExtend %W [%W index @%x,%y]
60 bind Listbox <Control-1> {
61 tk::ListboxBeginToggle %W [%W index @%x,%y]
63 bind Listbox <B1-Leave> {
64 set tk::Priv(x) %x
65 set tk::Priv(y) %y
66 tk::ListboxAutoScan %W
68 bind Listbox <B1-Enter> {
69 tk::CancelRepeat
72 bind Listbox <Up> {
73 tk::ListboxUpDown %W -1
75 bind Listbox <Shift-Up> {
76 tk::ListboxExtendUpDown %W -1
78 bind Listbox <Down> {
79 tk::ListboxUpDown %W 1
81 bind Listbox <Shift-Down> {
82 tk::ListboxExtendUpDown %W 1
84 bind Listbox <Left> {
85 %W xview scroll -1 units
87 bind Listbox <Control-Left> {
88 %W xview scroll -1 pages
90 bind Listbox <Right> {
91 %W xview scroll 1 units
93 bind Listbox <Control-Right> {
94 %W xview scroll 1 pages
96 bind Listbox <Prior> {
97 %W yview scroll -1 pages
98 %W activate @0,0
100 bind Listbox <Next> {
101 %W yview scroll 1 pages
102 %W activate @0,0
104 bind Listbox <Control-Prior> {
105 %W xview scroll -1 pages
107 bind Listbox <Control-Next> {
108 %W xview scroll 1 pages
110 bind Listbox <Home> {
111 %W xview moveto 0
113 bind Listbox <End> {
114 %W xview moveto 1
116 bind Listbox <Control-Home> {
117 %W activate 0
118 %W see 0
119 %W selection clear 0 end
120 %W selection set 0
121 event generate %W <<ListboxSelect>>
123 bind Listbox <Shift-Control-Home> {
124 tk::ListboxDataExtend %W 0
126 bind Listbox <Control-End> {
127 %W activate end
128 %W see end
129 %W selection clear 0 end
130 %W selection set end
131 event generate %W <<ListboxSelect>>
133 bind Listbox <Shift-Control-End> {
134 tk::ListboxDataExtend %W [%W index end]
136 bind Listbox <<Copy>> {
137 if {[selection own -displayof %W] eq "%W"} {
138 clipboard clear -displayof %W
139 clipboard append -displayof %W [selection get -displayof %W]
142 bind Listbox <space> {
143 tk::ListboxBeginSelect %W [%W index active]
145 bind Listbox <Select> {
146 tk::ListboxBeginSelect %W [%W index active]
148 bind Listbox <Control-Shift-space> {
149 tk::ListboxBeginExtend %W [%W index active]
151 bind Listbox <Shift-Select> {
152 tk::ListboxBeginExtend %W [%W index active]
154 bind Listbox <Escape> {
155 tk::ListboxCancel %W
157 bind Listbox <Control-slash> {
158 tk::ListboxSelectAll %W
160 bind Listbox <Control-backslash> {
161 if {[%W cget -selectmode] ne "browse"} {
162 %W selection clear 0 end
163 event generate %W <<ListboxSelect>>
167 # Additional Tk bindings that aren't part of the Motif look and feel:
169 bind Listbox <2> {
170 %W scan mark %x %y
172 bind Listbox <B2-Motion> {
173 %W scan dragto %x %y
176 # The MouseWheel will typically only fire on Windows and Mac OS X.
177 # However, someone could use the "event generate" command to produce
178 # one on other platforms.
180 if {[tk windowingsystem] eq "aqua"} {
181 bind Listbox <MouseWheel> {
182 %W yview scroll [expr {- (%D)}] units
184 bind Listbox <Option-MouseWheel> {
185 %W yview scroll [expr {-10 * (%D)}] units
187 bind Listbox <Shift-MouseWheel> {
188 %W xview scroll [expr {- (%D)}] units
190 bind Listbox <Shift-Option-MouseWheel> {
191 %W xview scroll [expr {-10 * (%D)}] units
193 } else {
194 bind Listbox <MouseWheel> {
195 %W yview scroll [expr {- (%D / 120) * 4}] units
199 if {"x11" eq [tk windowingsystem]} {
200 # Support for mousewheels on Linux/Unix commonly comes through mapping
201 # the wheel to the extended buttons. If you have a mousewheel, find
202 # Linux configuration info at:
203 # http://www.inria.fr/koala/colas/mouse-wheel-scroll/
204 bind Listbox <4> {
205 if {!$tk_strictMotif} {
206 %W yview scroll -5 units
209 bind Listbox <5> {
210 if {!$tk_strictMotif} {
211 %W yview scroll 5 units
216 # ::tk::ListboxBeginSelect --
218 # This procedure is typically invoked on button-1 presses. It begins
219 # the process of making a selection in the listbox. Its exact behavior
220 # depends on the selection mode currently in effect for the listbox;
221 # see the Motif documentation for details.
223 # Arguments:
224 # w - The listbox widget.
225 # el - The element for the selection operation (typically the
226 # one under the pointer). Must be in numerical form.
228 proc ::tk::ListboxBeginSelect {w el {focus 1}} {
229 variable ::tk::Priv
230 if {[$w cget -selectmode] eq "multiple"} {
231 if {[$w selection includes $el]} {
232 $w selection clear $el
233 } else {
234 $w selection set $el
236 } else {
237 $w selection clear 0 end
238 $w selection set $el
239 $w selection anchor $el
240 set Priv(listboxSelection) {}
241 set Priv(listboxPrev) $el
243 event generate $w <<ListboxSelect>>
244 # check existence as ListboxSelect may destroy us
245 if {$focus && [winfo exists $w] && [$w cget -state] eq "normal"} {
246 focus $w
250 # ::tk::ListboxMotion --
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.
256 # Arguments:
257 # w - The listbox widget.
258 # el - The element under the pointer (must be a number).
260 proc ::tk::ListboxMotion {w el} {
261 variable ::tk::Priv
262 if {$el == $Priv(listboxPrev)} {
263 return
265 set anchor [$w index anchor]
266 switch [$w cget -selectmode] {
267 browse {
268 $w selection clear 0 end
269 $w selection set $el
270 set Priv(listboxPrev) $el
271 event generate $w <<ListboxSelect>>
273 extended {
274 set i $Priv(listboxPrev)
275 if {$i eq ""} {
276 set i $el
277 $w selection set $el
279 if {[$w selection includes anchor]} {
280 $w selection clear $i $el
281 $w selection set anchor $el
282 } else {
283 $w selection clear $i $el
284 $w selection clear anchor $el
286 if {![info exists Priv(listboxSelection)]} {
287 set Priv(listboxSelection) [$w curselection]
289 while {($i < $el) && ($i < $anchor)} {
290 if {[lsearch $Priv(listboxSelection) $i] >= 0} {
291 $w selection set $i
293 incr i
295 while {($i > $el) && ($i > $anchor)} {
296 if {[lsearch $Priv(listboxSelection) $i] >= 0} {
297 $w selection set $i
299 incr i -1
301 set Priv(listboxPrev) $el
302 event generate $w <<ListboxSelect>>
307 # ::tk::ListboxBeginExtend --
309 # This procedure is typically invoked on shift-button-1 presses. It
310 # begins the process of extending a selection in the listbox. Its
311 # exact behavior depends on the selection mode currently in effect
312 # for the listbox; see the Motif documentation for details.
314 # Arguments:
315 # w - The listbox widget.
316 # el - The element for the selection operation (typically the
317 # one under the pointer). Must be in numerical form.
319 proc ::tk::ListboxBeginExtend {w el} {
320 if {[$w cget -selectmode] eq "extended"} {
321 if {[$w selection includes anchor]} {
322 ListboxMotion $w $el
323 } else {
324 # No selection yet; simulate the begin-select operation.
325 ListboxBeginSelect $w $el
330 # ::tk::ListboxBeginToggle --
332 # This procedure is typically invoked on control-button-1 presses. It
333 # begins the process of toggling a selection in the listbox. Its
334 # exact behavior depends on the selection mode currently in effect
335 # for the listbox; see the Motif documentation for details.
337 # Arguments:
338 # w - The listbox widget.
339 # el - The element for the selection operation (typically the
340 # one under the pointer). Must be in numerical form.
342 proc ::tk::ListboxBeginToggle {w el} {
343 variable ::tk::Priv
344 if {[$w cget -selectmode] eq "extended"} {
345 set Priv(listboxSelection) [$w curselection]
346 set Priv(listboxPrev) $el
347 $w selection anchor $el
348 if {[$w selection includes $el]} {
349 $w selection clear $el
350 } else {
351 $w selection set $el
353 event generate $w <<ListboxSelect>>
357 # ::tk::ListboxAutoScan --
358 # This procedure is invoked when the mouse leaves an entry window
359 # with button 1 down. It scrolls the window up, down, left, or
360 # right, depending on where the mouse left the window, and reschedules
361 # itself as an "after" command so that the window continues to scroll until
362 # the mouse moves back into the window or the mouse button is released.
364 # Arguments:
365 # w - The entry window.
367 proc ::tk::ListboxAutoScan {w} {
368 variable ::tk::Priv
369 if {![winfo exists $w]} return
370 set x $Priv(x)
371 set y $Priv(y)
372 if {$y >= [winfo height $w]} {
373 $w yview scroll 1 units
374 } elseif {$y < 0} {
375 $w yview scroll -1 units
376 } elseif {$x >= [winfo width $w]} {
377 $w xview scroll 2 units
378 } elseif {$x < 0} {
379 $w xview scroll -2 units
380 } else {
381 return
383 ListboxMotion $w [$w index @$x,$y]
384 set Priv(afterId) [after 50 [list tk::ListboxAutoScan $w]]
387 # ::tk::ListboxUpDown --
389 # Moves the location cursor (active element) up or down by one element,
390 # and changes the selection if we're in browse or extended selection
391 # mode.
393 # Arguments:
394 # w - The listbox widget.
395 # amount - +1 to move down one item, -1 to move back one item.
397 proc ::tk::ListboxUpDown {w amount} {
398 variable ::tk::Priv
399 $w activate [expr {[$w index active] + $amount}]
400 $w see active
401 switch [$w cget -selectmode] {
402 browse {
403 $w selection clear 0 end
404 $w selection set active
405 event generate $w <<ListboxSelect>>
407 extended {
408 $w selection clear 0 end
409 $w selection set active
410 $w selection anchor active
411 set Priv(listboxPrev) [$w index active]
412 set Priv(listboxSelection) {}
413 event generate $w <<ListboxSelect>>
418 # ::tk::ListboxExtendUpDown --
420 # Does nothing unless we're in extended selection mode; in this
421 # case it moves the location cursor (active element) up or down by
422 # one element, and extends the selection to that point.
424 # Arguments:
425 # w - The listbox widget.
426 # amount - +1 to move down one item, -1 to move back one item.
428 proc ::tk::ListboxExtendUpDown {w amount} {
429 variable ::tk::Priv
430 if {[$w cget -selectmode] ne "extended"} {
431 return
433 set active [$w index active]
434 if {![info exists Priv(listboxSelection)]} {
435 $w selection set $active
436 set Priv(listboxSelection) [$w curselection]
438 $w activate [expr {$active + $amount}]
439 $w see active
440 ListboxMotion $w [$w index active]
443 # ::tk::ListboxDataExtend
445 # This procedure is called for key-presses such as Shift-KEndData.
446 # If the selection mode isn't multiple or extend then it does nothing.
447 # Otherwise it moves the active element to el and, if we're in
448 # extended mode, extends the selection to that point.
450 # Arguments:
451 # w - The listbox widget.
452 # el - An integer element number.
454 proc ::tk::ListboxDataExtend {w el} {
455 set mode [$w cget -selectmode]
456 if {$mode eq "extended"} {
457 $w activate $el
458 $w see $el
459 if {[$w selection includes anchor]} {
460 ListboxMotion $w $el
462 } elseif {$mode eq "multiple"} {
463 $w activate $el
464 $w see $el
468 # ::tk::ListboxCancel
470 # This procedure is invoked to cancel an extended selection in
471 # progress. If there is an extended selection in progress, it
472 # restores all of the items between the active one and the anchor
473 # to their previous selection state.
475 # Arguments:
476 # w - The listbox widget.
478 proc ::tk::ListboxCancel w {
479 variable ::tk::Priv
480 if {[$w cget -selectmode] ne "extended"} {
481 return
483 set first [$w index anchor]
484 set last $Priv(listboxPrev)
485 if {$last eq ""} {
486 # Not actually doing any selection right now
487 return
489 if {$first > $last} {
490 set tmp $first
491 set first $last
492 set last $tmp
494 $w selection clear $first $last
495 while {$first <= $last} {
496 if {[lsearch $Priv(listboxSelection) $first] >= 0} {
497 $w selection set $first
499 incr first
501 event generate $w <<ListboxSelect>>
504 # ::tk::ListboxSelectAll
506 # This procedure is invoked to handle the "select all" operation.
507 # For single and browse mode, it just selects the active element.
508 # Otherwise it selects everything in the widget.
510 # Arguments:
511 # w - The listbox widget.
513 proc ::tk::ListboxSelectAll w {
514 set mode [$w cget -selectmode]
515 if {$mode eq "single" || $mode eq "browse"} {
516 $w selection clear 0 end
517 $w selection set active
518 } else {
519 $w selection set 0 end
521 event generate $w <<ListboxSelect>>