1 # ui-list.tcl --
2 #
3 # Defines a listbox widget
4 #
5 # Copyright (c) 1998-2002 The Regents of the University of California.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 #
11 # A. Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 # B. Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 # C. Neither the names of the copyright holders nor the names of its
17 # contributors may be used to endorse or promote products derived from this
18 # software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
24 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 # Similar to the Tk listbox widget.
32 Class VEListbox
33
34 # Instantiate a new listbox widget. The window <i>w</i> should already
35 # exists and be otherwise empty (this is different from the regular Tk
36 # convention for widget creation).
37
38 VEListbox public init {w} {
39 $self next
40 $self instvar canvas_ sb_
41 set canvas_ $w.c
42 set sb_ $w.s
43
44 $self set ids_ {}
45
46 canvas $canvas_ -relief groove -bd 0 -yscrollcommand "$sb_ set"\
47 -height 200 -width 200
48 scrollbar $sb_ -relief groove -bd 2 -command "$canvas_ yview"
49
50 # don't pack scrollbar until needed
51 pack $canvas_ -side right -fill both -expand yes
52
53 bind $w <Configure> "$self fix-scrollbar 1"
54 }
55
56 # Map the scrollbar if it is needed, unmap it if it is not needed.
57
58 VEListbox private fix-scrollbar {{update 0}} {
59 $self instvar canvas_ sb_
60 if {$update != 0} { update }
61 set yv [$canvas_ yview]
62 if {[lindex $yv 0] != 0 || [lindex $yv 1] != 1} {
63 pack $sb_ -side right -before $canvas_ -fill y
64 } else {
65 pack forget $sb_
66 }
67 }
68
69 # Like the insert method on a regular Tk listbox; inserts an item
70 # with the text <i>item</i> right before the <i>i</i>th entry or at
71 # the end if <i>i</i> is the string "end". Arranges for the Tcl
72 # command <i>callback</i> to be invoked when this item is clicked.
73
74 VEListbox public insert {i item callback} {
75 $self instvar ids_ canvas_ bottom_
76 set l [llength $ids_]
77 if {$i >= $l } { set i "end" }
78 # find the top coordinate for this item
79 if {$i == 0} {
80 set top 2
81 } else {
82 if {$i == "end"} {
83 set last [lindex $ids_ "end"]
84 } else {
85 set last [lindex $ids_ [expr $i-1]]
86 }
87 set top [expr [lindex [$canvas_ bbox $last] 3] + 2]
88 }
89
90 # create the new item (for now it overlaps with others)
91 set id [$canvas_ create text 5 $top -text $item -anchor nw]
92 set bb [$canvas_ bbox $id]
93 set height [expr [lindex $bb 3] - [lindex $bb 1] + 2]
94
95 # insert the new item in to ids_ and move everything down
96 set ids_ [linsert $ids_ $i $id]
97 if {$i != "end"} {
98 incr i
99 set l [llength $ids_]
100 while {$i < $l} {
101 $canvas_ move [lindex $ids_ $i] 0 $height
102 incr i
103 }
104 }
105 # set up bindings
106 $canvas_ bind $id <Enter> "$canvas_ itemconfigure $id -fill \#ff3030"
107 $canvas_ bind $id <Leave> "$canvas_ itemconfigure $id -fill black"
108 $canvas_ bind $id <Button-1> $callback
109
110 set bottom [lindex [$canvas_ bbox [lindex $ids_ end]] 3]
111
112 $canvas_ config -scrollregion "0 0 2.5i $bottom"
113 $self fix-scrollbar
114 }
115
116 # Like the delete method on a regular Tk listbox; removes the <i>i</i>th
117 # entry or the last one if <i>i</i> is the string "end".
118
119 VEListbox public delete {i} {
120 $self instvar ids_ canvas_
121 set id [lindex $ids_ $i]
122 set ids_ [lreplace $ids_ $i $i]
123 set bb [$canvas_ bbox $id]
124 set height [expr [lindex $bb 3] - [lindex $bb 1] + 2]
125 $canvas_ delete $id
126
127 set l [llength $ids_]
128 while {$i < $l} {
129 $canvas_ move [lindex $ids_ $i] 0 -$height
130 incr i
131 }
132
133 set bottom [lindex [$canvas_ bbox [lindex $ids_ end]] 3]
134
135 if {$ids_ == {}} {
136 set bottom 0
137 }
138
139 $canvas_ config -scrollregion "0 0 2.5i $bottom"
140 $self fix-scrollbar
141 }
142
143
144
145
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.