]> www.wagner.pp.ru Git - oss/ck.git/blob - library/listbox.tcl
Ck console graphics toolkit
[oss/ck.git] / library / listbox.tcl
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 #
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 # ckPriv elements used in this file:
14 #
15 # listboxPrev -         The last element to be selected or deselected
16 #                       during a selection operation.
17 # listboxSelection -    All of the items that were selected before the
18 #                       current selection operation (such as a mouse
19 #                       drag) started;  used to cancel an operation.
20 #--------------------------------------------------------------------------
21
22 #-------------------------------------------------------------------------
23 # The code below creates the default class bindings for listboxes.
24 #-------------------------------------------------------------------------
25
26 bind Listbox <Up> {
27     ckListboxUpDown %W -1
28 }
29 bind Listbox <Down> {
30     ckListboxUpDown %W 1
31 }
32 bind Listbox <Left> {
33     %W xview scroll -1 units
34 }
35 bind Listbox <Right> {
36     %W xview scroll 1 units
37 }
38 bind Listbox <Prior> {
39     %W yview scroll -1 pages
40     %W activate @0,0
41 }
42 bind Listbox <Next> {
43     %W yview scroll 1 pages
44     %W activate @0,0
45 }
46 bind Listbox <Home> {
47     %W xview moveto 0
48 }
49 bind Listbox <End> {
50     %W xview moveto 1
51 }
52 bind Listbox <space> {
53     ckListboxBeginSelect %W [%W index active]
54 }
55 bind Listbox <Select> {
56     ckListboxBeginSelect %W [%W index active]
57 }
58 bind Listbox <Escape> {
59     ckListboxCancel %W
60 }
61 bind Listbox <Button-1> {
62     focus %W
63     ckListboxBeginSelect %W [%W index @0,%y]
64 }
65
66 # ckListboxBeginSelect --
67 #
68 # This procedure is typically invoked on space presses.  It begins
69 # the process of making a selection in the listbox.  Its exact behavior
70 # depends on the selection mode currently in effect for the listbox;
71 # see the Motif documentation for details.
72 #
73 # Arguments:
74 # w -           The listbox widget.
75 # el -          The element for the selection operation (typically the
76 #               one under the pointer).  Must be in numerical form.
77
78 proc ckListboxBeginSelect {w el} {
79     global ckPriv
80     if {[$w cget -selectmode] == "multiple"} {
81         if [$w selection includes $el] {
82             $w selection clear $el
83         } else {
84             $w selection set $el
85         }
86     } else {
87         $w activate $el
88         $w selection clear 0 end
89         $w selection set $el
90         $w selection anchor $el
91         set ckPriv(listboxSelection) {}
92         set ckPriv(listboxPrev) $el
93     }
94 }
95
96 # ckListboxUpDown --
97 #
98 # Moves the location cursor (active element) up or down by one element,
99 # and changes the selection if we're in browse or extended selection
100 # mode.
101 #
102 # Arguments:
103 # w -           The listbox widget.
104 # amount -      +1 to move down one item, -1 to move back one item.
105
106 proc ckListboxUpDown {w amount} {
107     global ckPriv
108     $w activate [expr [$w index active] + $amount]
109     $w see active
110     switch [$w cget -selectmode] {
111         browse {
112             $w selection clear 0 end
113             $w selection set active
114         }
115         extended {
116             $w selection clear 0 end
117             $w selection set active
118             $w selection anchor active
119             set ckPriv(listboxPrev) [$w index active]
120             set ckPriv(listboxSelection) {}
121         }
122     }
123 }
124
125 # ckListboxCancel
126 #
127 # This procedure is invoked to cancel an extended selection in
128 # progress.  If there is an extended selection in progress, it
129 # restores all of the items between the active one and the anchor
130 # to their previous selection state.
131 #
132 # Arguments:
133 # w -           The listbox widget.
134
135 proc ckListboxCancel w {
136     global ckPriv
137     if {[$w cget -selectmode] != "extended"} {
138         return
139     }
140     set first [$w index anchor]
141     set last $ckPriv(listboxPrev)
142     if {$first > $last} {
143         set tmp $first
144         set first $last
145         set last $tmp
146     }
147     $w selection clear $first $last
148     while {$first <= $last} {
149         if {[lsearch $ckPriv(listboxSelection) $first] >= 0} {
150             $w selection set $first
151         }
152         incr first
153     }
154 }