
# 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

# This is a gizmo that lets you select one item out of a list.

set pickone_done {}
set pickone_variable {}

proc pickone {w msgArgs choices command alone} {
    global pickone_done pickone_variable helpCmds
    
    disable_interface
    
    set pickone_done {}
    set pickone_variable "*cancel*"
    
    set fnames {}
    foreach n $choices {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}
    
    if {$msgArgs != ""} {
	eval message $w.message -justify left -width 400 $msgArgs
    }
    frame $w.avail -borderwidth 2 -relief raised
    
    for {set i 0} {$i < $cols} {incr i} {
	frame $w.avail.$i -borderwidth 0 -relief flat
	pack append $w.avail $w.avail.$i {left fill expand}
    }
    
    set i 0
    foreach e $list {
	eval set elt $e
	set c [expr {$i % $cols}]
	eval radiobutton $w.avail.$c.$i -text \"$e\" $pfont -width 3 \
	    -variable pickone_variable -background #ffe4c4 \
	    -anchor nw -value $elt
	set helpCmds($w.avail.$c.$i) "set topic $w.avail"
	pack append $w.avail.$c $w.avail.$c.$i {top fill}
	set buts "$buts $w.avail.$c.$i"
	incr i
    }
    
    frame $w.buttons -borderwidth 2 -relief raised
    button $w.buttons.okay -text Done \
	-command {set pickone_done *okay*}
    button $w.buttons.cancel -text Cancel \
	-command {set pickone_done *cancel*}
    pack append $w.buttons \
	$w.buttons.okay {left fill expand padx 4} \
	$w.buttons.cancel {left fill expand padx 4}
    
    if {$msgArgs != ""} {
	pack append $w $w.message {top fill} \
	    $w.avail {top fill} \
	    $w.buttons {bottom fill}
    } else {
	pack append $w $w.avail {top fill} $w.buttons {bottom fill}
    }
    
    if {$alone} {
	wm geometry $w +300+300
	#putcenter $w
    } else {
	set width [expr {[winfo x $w.buttons] +
	    [winfo width $w.buttons]}]
	set height [expr {[winfo y $w.buttons] +
	    [winfo height $w.buttons]}]
	$w configure -width $width -height $height
    }
    
    # work-a-round
    if {$command != ""} {eval $command}
    
    # uplevel #0 wait variable pickone_done
    tkwait variable pickone_done
    
    if $alone {destroy $w}
    enable_interface
    return $pickone_variable
}
