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 NewList
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 NewList public init {w} {
38 $self next
39
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 scrollbar $sb_ -relief groove -bd 2 -command "$canvas_ yview"
48
49 # don't pack scrollbar until needed
50 pack $canvas_ -side right -fill both -expand yes
51
52 bind $w <Configure> "$self fix-scrollbar 1"
53 }
54
55 # Map the scrollbar if it is needed, unmap it if it is not needed.
56 NewList private fix-scrollbar {{update 0}} {
57 $self instvar canvas_ sb_
58 if {$update != 0} { update }
59 set yv [$canvas_ yview]
60 if {[lindex $yv 0] != 0 || [lindex $yv 1] != 1} {
61 pack $sb_ -side right -before $canvas_ -fill y
62 } else {
63 pack forget $sb_
64 }
65 }
66
67 # Like the insert method on a regular Tk listbox; inserts an item
68 # with the text <i>item</i> right before the <i>i</i>th entry or at
69 # the end if <i>i</i> is the string "end". Arranges for the Tcl
70 # command <i>callback</i> to be invoked when this item is clicked.
71 NewList public insert {i item callback} {
72 $self instvar ids_ canvas_ bottom_
73 set l [llength $ids_]
74 if {$i >= $l } { set i "end" }
75
76 # find the top coordinate for this item
77 if {$i == 0} {
78 set top 2
79 } else {
80 if {$i == "end"} {
81 set last [lindex $ids_ "end"]
82 } else {
83 set last [lindex $ids_ [expr $i-1]]
84 }
85 set top [expr [lindex [$canvas_ bbox $last] 3] + 2]
86 }
87
88 # create the new item (for now it overlaps with others)
89 set id [$canvas_ create text 5 $top -text $item -anchor nw]
90 set bb [$canvas_ bbox $id]
91 set height [expr [lindex $bb 3] - [lindex $bb 1] + 2]
92
93 # insert the new item in to ids_ and move everything down
94 set ids_ [linsert $ids_ $i $id]
95 if {$i != "end"} {
96 incr i
97 set l [llength $ids_]
98 while {$i < $l} {
99 $canvas_ move [lindex $ids_ $i] 0 $height
100 incr i
101 }
102 }
103
104 # set up bindings
105 # FIXME would be nicer if this made a background to highlight this
106 # item rather than changing the fill color
107 $canvas_ bind $id <Enter> "$canvas_ itemconfigure $id -fill \#ff3030"
108 $canvas_ bind $id <Leave> "$canvas_ itemconfigure $id -fill black"
109 $canvas_ bind $id <Button-1> $callback
110
111 set bottom [lindex [$canvas_ bbox [lindex $ids_ end]] 3]
112 #FIXME
113 $canvas_ config -scrollregion "0 0 2.5i $bottom"
114 $self fix-scrollbar
115 }
116
117 # Like the delete method on a regular Tk listbox; removes the <i>i</i>th
118 # entry or the last one if <i>i</i> is the string "end".
119 NewList public delete {i} {
120 $self instvar ids_ canvas_
121 set id [lindex $ids_ $i]
122 set ids_ [lreplace $ids_ $i $i]
123
124 set bb [$canvas_ bbox $id]
125 set height [expr [lindex $bb 3] - [lindex $bb 1] + 2]
126 $canvas_ delete $id
127
128 set l [llength $ids_]
129 while {$i < $l} {
130 $canvas_ move [lindex $ids_ $i] 0 -$height
131 incr i
132 }
133
134 if { [llength $ids_] == 0 } {
135 return
136 }
137
138 set bottom [lindex [$canvas_ bbox [lindex $ids_ end]] 3]
139 #FIXME
140 $canvas_ config -scrollregion "0 0 2.5i $bottom"
141 $self fix-scrollbar
142 }
143
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.