1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
# swaplist.tcl --
#
# A dialog which allows a user to move options between two lists
#
# Copyright (c) 2005 Aaron Faupell <afaupell@users.sourceforge.net>
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: swaplist.tcl,v 1.6 2008/02/06 07:15:16 afaupell Exp $
package require Tk
package provide swaplist 0.2
namespace eval swaplist {
namespace export swaplist
}
if {[tk windowingsystem] == "win32"} {
option add *Swaplist*Button.width -10 widgetDefault
option add *Swaplist*Button.padX 1m widgetDefault
option add *Swaplist*Border.borderWidth 2 widgetDefault
option add *Swaplist*Border*Listbox.borderWidth 0 widgetDefault
} else {
option add *Swaplist.borderWidth 1 widgetDefault
option add *Swaplist*Button.width 5 widgetDefault
}
proc ::swaplist::swaplist {w var list1 list2 args} {
array set options {
-title "Configuration"
}
parseOpts options {{-llabel {}} {-rlabel {}} {-title {}} -embed \
{-reorder boolean} {-geometry {}} {-lbuttontext {}} \
{-rbuttontext {}} {-ubuttontext {}} {-dbuttontext {}}} \
$args
if {[info exists options(-embed)]} {
frame $w
unset options(-embed)
return [eval [list ::swaplist::createSwaplist $w $var $list1 $list2] [array get options]]
}
catch {destroy $w}
set focus [focus]
set grab [grab current .]
toplevel $w -class Swaplist -relief raised
wm title $w $options(-title)
wm protocol $w WM_DELETE_WINDOW {set ::swaplist::whichButton 0}
wm transient $w [winfo toplevel [winfo parent $w]]
eval [list ::swaplist::createSwaplist $w ::swaplist::selectedList $list1 $list2] [array get options]
frame $w.oc -pady 7
button $w.oc.ok -default active -text "OK" -command {set ::swaplist::whichButton 1}
button $w.oc.cancel -text "Cancel" -command {set ::swaplist::whichButton 0}
pack $w.oc.cancel -side right -padx 7
pack $w.oc.ok -side right
grid $w.oc -columnspan 4 -row 2 -column 0 -sticky ew -columnspan 4
bind $w <Return> [list $w.oc.ok invoke]
bind $w <Escape> [list $w.oc.cancel invoke]
bind $w <Destroy> {set ::swaplist::whichButton 0}
#SetButtonState $w
wm withdraw $w
update idletasks
if {[info exists options(-geometry)]} {
wm geometry $w $options(-geometry)
} elseif {[winfo parent $w] == "."} {
set x [expr {[winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 - [winfo vrootx $w]}]
set y [expr {[winfo screenheight $w]/2 - [winfo reqheight $w]/2 - [winfo vrooty $w]}]
wm geometry $w +$x+$y
} else {
set t [winfo toplevel [winfo parent $w]]
set x [expr {[winfo width $t]/2 - [winfo reqwidth $w]/2 - [winfo vrootx $w]}]
set y [expr {[winfo height $t]/2 - [winfo reqheight $w]/2 - [winfo vrooty $w]}]
wm geometry $w +$x+$y
}
wm deiconify $w
grab $w
tkwait variable ::swaplist::whichButton
upvar $var results
set results $::swaplist::selectedList
bind $w <Destroy> {}
grab release $w
destroy $w
focus -force $focus
if {$grab != ""} {grab $grab}
update idletasks
return $::swaplist::whichButton
}
proc ::swaplist::createSwaplist {w var list1 list2 args} {
array set options {
-reorder 1
-llabel "Available:"
-rlabel "Selected:"
-lbuttontext "<<"
-rbuttontext ">>"
-ubuttontext "Move Up"
-dbuttontext "Move Down"
}
parseOpts options {{-llabel {}} {-rlabel {}} {-title {}} \
{-reorder boolean} {-lbuttontext {}} {-geometry {}}\
{-rbuttontext {}} {-ubuttontext {}} {-dbuttontext {}}} \
$args
set olist $list1
# remove items in list2 from list1
foreach x $list2 {
if {[set i [lsearch $list1 $x]] >= 0} {
set list1 [lreplace $list1 $i $i]
}
}
label $w.heading1 -text $options(-llabel) -anchor w
label $w.heading2 -text $options(-rlabel) -anchor w
foreach x {list1 list2} {
frame $w.$x -class Border -relief sunken
scrollbar $w.$x.scrolly -orient v -command [list $w.$x.list yview]
scrollbar $w.$x.scrollx -orient h -command [list $w.$x.list xview]
listbox $w.$x.list -selectmode extended -yscrollcommand [list $w.$x.scrolly set] -xscrollcommand [list $w.$x.scrollx set]
grid $w.$x.list -row 0 -column 0 -sticky nesw
grid $w.$x.scrolly -row 0 -column 1 -sticky ns
grid $w.$x.scrollx -row 1 -column 0 -sticky ew
grid columnconfigure $w.$x 0 -weight 1
grid rowconfigure $w.$x 0 -weight 1
}
$w.list2.list configure -listvariable $var
$w.list2.list delete 0 end
eval [list $w.list1.list insert end] $list1
eval [list $w.list2.list insert end] $list2
set width [min 5 $options(-lbuttontext) $options(-rbuttontext)]
frame $w.lr
button $w.lr.left -width $width -text $options(-lbuttontext) -command [list ::swaplist::ShiftL $w $olist]
if {$options(-reorder)} {
button $w.lr.right -width $width -text $options(-rbuttontext) -command [list ::swaplist::ShiftRNormal $w $olist]
} else {
button $w.lr.right -width $width -text $options(-rbuttontext) -command [list ::swaplist::ShiftRNoReorder $w $olist]
}
grid $w.lr.right -pady 4
grid $w.lr.left -pady 4
grid columnconfigure $w.lr 0 -uniform 1
set width [min 3 $options(-ubuttontext) $options(-dbuttontext)]
frame $w.ud
button $w.ud.up -width $width -text $options(-ubuttontext) -command [list ::swaplist::ShiftUD $w.list2.list u]
button $w.ud.down -width $width -text $options(-dbuttontext) -command [list ::swaplist::ShiftUD $w.list2.list d]
pack $w.ud.up -side top -pady 4
pack $w.ud.down -side bottom -pady 4
grid $w.heading1 -row 0 -column 0 -sticky ew -padx {3 0} -pady 3
grid $w.heading2 -row 0 -column 2 -sticky ew -padx {0 3} -pady 3
grid $w.list1 -row 1 -column 0 -sticky nesw -padx {3 0}
grid $w.lr -row 1 -column 1 -padx 7
grid $w.list2 -row 1 -column 2 -sticky nesw -padx {0 3}
if {$options(-reorder)} {
grid $w.ud -row 1 -column 3 -padx {2 5}
}
grid columnconfigure $w {0 2} -weight 1
grid rowconfigure $w 1 -weight 1
bind $w <Key-Up> [list ::swaplist::UpDown %W %K]
bind $w <Key-Down> [list ::swaplist::UpDown %W %K]
bind $w.list1.list <Double-Button-1> [list ::swaplist::Double %W]
bind $w.list2.list <Double-Button-1> [list ::swaplist::Double %W]
#bind $w.list1.list <<ListboxSelect>> [list ::swaplist::SetButtonState %W]
#bind $w.list2.list <<ListboxSelect>> [list ::swaplist::SetButtonState %W]
if {![catch {package present autoscroll}]} {
::autoscroll::autoscroll $w.list1.scrollx
::autoscroll::autoscroll $w.list1.scrolly
::autoscroll::autoscroll $w.list2.scrollx
::autoscroll::autoscroll $w.list2.scrolly
}
#SetButtonState $w
return $w
}
proc ::swaplist::parseOpts {var opts input} {
upvar $var output
for {set i 0} {$i < [llength $input]} {incr i} {
for {set a 0} {$a < [llength $opts]} {incr a} {
if {[lindex $opts $a 0] == [lindex $input $i]} { break }
}
if {$a == [llength $opts]} { error "unknown option [lindex $input $i]" }
set opt [lindex $opts $a]
if {[llength $opt] > 1} {
foreach {opt type} $opt {break}
if {[incr i] >= [llength $input]} { error "$opt requires an argument" }
if {$type != "" && ![string is $type -strict [lindex $input $i]]} { error "$opt requires argument of type $type" }
set output($opt) [lindex $input $i]
} else {
set output($opt) {}
}
}
}
# return the min unless string1 or string2 is longer, if so return length of the longer one
proc ::swaplist::min {min s1 s2} {
if {[string length $s1] > $min || [string length $s2] > $min} {
return [expr {
([string length $s1] > [string length $s2]) \
? [string length $s1] \
: [string length $s2]
}]
} else {
return $min
}
}
# return a list in reversed order
proc ::swaplist::lreverse {list} {
set new {}
foreach x $list {set new [linsert $new 0 $x]}
return $new
}
# binding for "move left" button
proc ::swaplist::ShiftL {w olist} {
set from $w.list2.list
set to $w.list1.list
if {[set cur [$from curselection]] == ""} { return }
foreach x [lreverse $cur] {
set name [$from get $x]
$from delete $x
set i [FindPos $olist [$to get 0 end] $name]
$to insert $i $name
$to selection set $i
}
if {[llength $cur] == 1} {$to see $i}
if {[lindex $cur 0] == 0} {
$from selection set 0
} elseif {[lindex $cur 0] == [$from index end]} {
$from selection set end
} else {
$from selection set [lindex $cur 0]
}
}
# binding for "move right" button if -reorder is true
proc ::swaplist::ShiftRNormal {w olist} {
set from $w.list1.list
set to $w.list2.list
if {[set cur [$from curselection]] == ""} { return }
$to selection clear 0 end
foreach x $cur {
$to insert end [$from get $x]
$to selection set end
}
foreach x [lreverse $cur] {
$from delete $x
}
$to see end
}
# binding for "move right" button if -reorder is false
proc ::swaplist::ShiftRNoReorder {w olist} {
set from $w.list1.list
set to $w.list2.list
if {[set cur [$from curselection]] == ""} { return }
foreach x $cur {
set name [$from get $x]
set pos [FindPos $olist [$to get 0 end] $name]
$to insert $pos $name
lappend new $pos
}
foreach x [lreverse $cur] { $from delete $x }
if {[$from index end] == 0} {
foreach x $new {$to selection set $x}
} elseif {[lindex $cur 0] == 0} {
$from selection set 0
} elseif {[lindex $cur 0] == [$from index end]} {
$from selection set end
} else {
$from selection set [lindex $cur 0]
}
}
# binding for "move up" and "move down" buttons
proc ::swaplist::ShiftUD {w dir} {
if {[set sel [$w curselection]] == ""} { return }
set list {}
# delete in reverse order so shifting indexes dont bite us
foreach x [lreverse $sel] {
# make a list in correct order with the items index and contents
set list [linsert $list 0 [list $x [$w get $x]]]
$w delete $x
}
if {$dir == "u"} {
set n 0
foreach x $list {
set i [lindex $x 0]
if {[incr i -1] < $n} {set i $n}
$w insert $i [lindex $x 1]
$w selection set $i
incr n
}
$w see [expr {[lindex $list 0 0] - 1}]
}
if {$dir == "d"} {
set n [$w index end]
foreach x $list {
set i [lindex $x 0]
if {[incr i] > $n} {set i $n}
$w insert $i [lindex $x 1]
$w selection set $i
incr n
}
$w see $i
}
}
# find the position $el should have in $curlist, by looking at $olist
# $curlist should be a subset of $olist
proc ::swaplist::FindPos {olist curlist el} {
set orig [lsearch $olist $el]
set end [llength $curlist]
for {set i 0} {$i < $end} {incr i} {
if {[lsearch $olist [lindex $curlist $i]] > $orig} { break }
}
return $i
}
# binding for the up and down arrow keys, just dispatch and have tk
# do the right thing
proc ::swaplist::UpDown {w key} {
if {[winfo toplevel $w] != $w} {return}
if {[set cur [$w.list2.list curselection]] != ""} {
tk::ListboxUpDown $w.list2.list [string map {Up -1 Down 1} $key]
} elseif {[set cur [$w.list1.list curselection]] != ""} {
tk::ListboxUpDown $w.list1.list [string map {Up -1 Down 1} $key]
} else {
return
}
}
# binding for double click, just invoke the left or right button
proc ::swaplist::Double {w} {
set top [winfo toplevel $w]
if {[string match *.list1.* $w]} {
$top.lr.right invoke
} elseif {[string match *.list2.* $w]} {
$top.lr.left invoke
}
}
proc ::swaplist::SetButtonState {w} {
set top [winfo toplevel $w]
if {[$top.list2.list curselection] != ""} {
$top.lr.left configure -state normal
$top.lr.right configure -state disabled
} elseif {[$top.list1.list curselection] != ""} {
$top.lr.left configure -state disabled
$top.lr.right configure -state normal
} else {
$top.lr.left configure -state disabled
$top.lr.right configure -state disabled
}
if {[set cur [$top.list2.list curselection]] == ""} {
$top.ud.up configure -state disabled
$top.ud.down configure -state disabled
} elseif {$cur == 0} {
$top.ud.up configure -state disabled
$top.ud.down configure -state normal
} elseif {$cur == ([$top.list2.list index end] - 1)} {
$top.ud.up configure -state normal
$top.ud.down configure -state disabled
} else {
$top.ud.up configure -state normal
$top.ud.down configure -state normal
}
}
|