
# This code creates the ICEM CFD Manager.
#
# Copyright (c) 1993 - 1996 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

# Pick a set of objects from a list with the ordering preserved.

set pickord_done {}
set pickord_chosen ""

proc pickord {w msgArgs names} {
    global pickord_done helpCmds
    global pickord_status pickord_labels pickord_num pickord_buttons
    
    disable_interface
    
    set pickord_done {}
    
    set fnames {}
    foreach n $names {set fnames "$fnames \"[format "%3d" $n]\""}
    set list [lsort $fnames]
    set buts {}
    
    set num [llength $list]
    if {$num < 150} {
	set pfont ""
	set maxcols 8
    } else {
	set pfont "-font 6x10"
	set maxcols 12
    }
    
    if {$num <= 10} {set cols 1}
    if {($num > 10) && ($num <= $maxcols * $maxcols)} \
	{set cols [expr {$num / $maxcols + 1}]}
    if {$num > $maxcols * $maxcols} {set cols $maxcols}
    
    frame $w.from -borderwidth 2 -relief raised
    frame $w.to -borderwidth 2 -relief raised
    
    for {set i 0} {$i < $cols} {incr i} {
	frame $w.from.$i -borderwidth 0 -relief flat
	pack append $w.from $w.from.$i {left fill expand}
	frame $w.to.$i -borderwidth 0 -relief flat
	pack append $w.to $w.to.$i {left fill expand}
    }
    
    catch {unset pickord_status}
    catch {unset pickord_buttons}
    set i 0
    foreach e $list {
	eval set elt $e
	set pickord_status($elt) 0
	set c [expr {$i % $cols}]
	
	eval checkbutton $w.from.$c.$i -text \"$e\" $pfont \
	    -variable pickord_status($elt) -background #ffe4c4
	set helpCmds($w.from.$c.$i) "set topic $w.from"
	pack append $w.from.$c $w.from.$c.$i {top fill}
	set buts "$buts $w.from.$c.$i"
	
	eval label $w.to.$c.$i -width 3 $pfont -borderwidth 1 \
		-relief raised -text {" "}
	set helpCmds($w.to.$c.$i) "set topic $w.to"
	pack append $w.to.$c $w.to.$c.$i {top fillx pady 2}
	set pickord_labels($i) $w.to.$c.$i
	bind $w.to.$c.$i <ButtonRelease-1> "pickord_press %W"
	
	lappend pickord_buttons $w.to.$c.$i
	incr i
    }
    set pickord_num $i
    
    foreach e $list {
	eval set elt $e
	trace variable pickord_status($elt) w pickord_change
    }
    
    frame $w.buttons -borderwidth 2 -relief raised
    button $w.buttons.okay -text Done \
	-command {set pickord_done *okay*}
    button $w.buttons.cancel -text Cancel \
	-command {set pickord_done *cancel*}
    button $w.buttons.all -text "Add all" \
	-command "pickord_set_all 1"
    button $w.buttons.none -text "Remove all" \
	-command "pickord_set_all 0"

    pack append $w.buttons \
	$w.buttons.okay {left fill expand padx 4} \
	$w.buttons.all {left fill expand padx 4} \
	$w.buttons.none {left fill expand padx 4} \
	$w.buttons.cancel {left fill expand padx 4}
    
    if {$msgArgs != ""} {
	eval message $w.message -justify left -width 400 $msgArgs
	pack append $w $w.message {top fill}
    }
    pack append $w \
	$w.buttons {bottom fill} \
	$w.from {left fill expand} \
	$w.to {right fill expand}
    
    wm geometry $w +300+300
    #putcenter $w
    
    tkwait variable pickord_done
    if {$pickord_done == "*cancel*"} then {
	set return *cancel*
    } else {
	set return {}
	for {set i 0} {$i < $pickord_num} {incr i} {
	    set lab $pickord_labels($i)
	    set text [llast [$lab configure -text]]
	    if {$text != " "} {append return " " $text} break
	}
    }
    
    destroy $w
    enable_interface
    return $return
}

proc pickord_set_all {on} {
    global pickord_status pickord_buttons
    foreach s [lsort -integer [array names pickord_status]] {
	set pickord_status($s) $on
    }
    if !$on { pickord_clear }
}

proc pickord_change {varname index op} {
    global pickord_status
    set newval $pickord_status($index)
    if $newval {pickord_insert $index} {pickord_remove $index}
    pickord_clear_chosen
}

proc pickord_insert {index} {
    global pickord_labels pickord_num
    pickord_clear_chosen
    for {set i 0} {$i < $pickord_num} {incr i} {
	set lab $pickord_labels($i)
	set val [llast [$lab configure -text]]
	if {$val == $index} {return}
	if {$val == " "} break
    }
    $lab configure -text $index
}

proc pickord_insert_label {index label} {
    global pickord_labels pickord_num
    pickord_clear_chosen
    for {set i 0} {$i < $pickord_num} {incr i} {
	set lab $pickord_labels($i)
	set val [llast [$lab configure -text]]
	if {($val == " ") || ($lab == $label)} break
    }
    for {set n [expr {$pickord_num - 1}]} {$n > $i} {incr n -1} {
	set lab $pickord_labels($n)
	set nlab $pickord_labels([expr {$n - 1}])
	set nval [llast [$nlab configure -text]]
	$lab configure -text $nval
    }
    
    $label configure -text $index
}

proc pickord_remove {index} {
    global pickord_labels pickord_num
    pickord_clear_chosen
    set foundit 0
    for {set i 0} {$i < $pickord_num} {incr i} {
	set lab $pickord_labels($i)
	set val [llast [$lab configure -text]]
	if {$val == $index} {set foundit 1}
	if $foundit {
	    if {$i + 1 < $pickord_num} then {
		set nlab $pickord_labels([expr {$i + 1}])
		set val [llast [$nlab configure -text]]
	    } else {
		set val " "
	    }
	    $lab configure -text $val
	}
	if {$val == " "} break
    }
}

proc pickord_clear {} {
    global pickord_labels pickord_num
    pickord_clear_chosen
    for {set i 0} {$i < $pickord_num} {incr i} {
	set lab $pickord_labels($i)
	$lab configure -text " "
    }
}

proc pickord_press {lab} {
    global pickord_chosen
    if {$pickord_chosen == ""} then {
	set ind [llast [$lab configure -text]]
	if {$ind == " "} return
	set pickord_chosen $lab
	$lab configure -background lavender
    } else {
	set ind [llast [$pickord_chosen configure -text]]
	set nind [llast [$lab configure -text]]
	if {($pickord_chosen != $lab) && ($nind != " ")} {
	    pickord_remove $ind
	    pickord_insert_label $ind $lab
	}
	pickord_clear_chosen
    }
}

proc pickord_clear_chosen {} {
    global pickord_chosen
    if {$pickord_chosen != ""} {
	$pickord_chosen configure -background "#ffeedd"
	set pickord_chosen ""
    }
}

#proc enable_interface {} {}
#proc disable_interface {} {}
#proc llast {list} {lindex $list [expr {[llength $list] - 1}]}

#set inums {}
#for {set i 0} {$i < 10} {incr i} {set inums "$inums $i"}
#toplevel .isel
#set picked [pickord .isel +100+100 {-text "Select the domains"} $inums]
#puts stdout "picked $picked"
