
# This code is shared by all the ICEM CFD GUI's.
#
# Copyright (c) 1993 - 1995 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

# Choose one of a list of options.  The extra commands are of the form
# {name label command}, where command is run with $w as an argument.

proc select_one {w msgArgs choices default extras} {
    global select_done

    set select_done {}
    
    set select_maxheight 10
    
    set num [llength $choices]
    if {$num == 0} {set num 1}
    set height $num
    set sb 0
    if {$height > $select_maxheight} {
	set height $select_maxheight
	set sb 1
    }
    
    eval message $w.message -justify left -width 400 $msgArgs
    entry $w.entry -borderwidth 4 -relief raised
    listbox $w.list -borderwidth 4 -relief raised \
	-width 30 -height $height -selectmode single
    frame $w.buttons -borderwidth 2 -relief raised
    if $sb {
	scrollbar $w.scroll -relief raised -command "$w.list yview" \
	    -orient v
	$w.list configure -yscroll "$w.scroll set"
	pack append $w \
	    $w.message {top fill pady 30} \
	    $w.entry {top fillx padx 5 pady 30 frame n} \
	    $w.buttons {bottom fillx} \
	    $w.scroll {right filly} \
	    $w.list {left fill expand}
    } else {
	pack append $w \
	    $w.message {top fill pady 30} \
	    $w.entry {top fillx padx 5 pady 30 frame n} \
	    $w.list {top fill expand} \
	    $w.buttons {top fill}
    }
    
    $w.entry insert 0 $default
    
    pathentry on $w.entry $choices
    
    foreach choice $choices {$w.list insert end $choice}
    
    set extras [concat "{okay Okay {set select_done okay}} \
			    {cancel Cancel {set select_done cancel}} \
			    \"help Help \\\"help_window_path $w\\\"\"" \
				$extras]
    
    foreach extra $extras {
	set bn [lindex $extra 0]
	set lab [lindex $extra 1]
	set com [lindex $extra 2]
	button $w.buttons.$bn -text $lab -command $com
	pack append $w.buttons $w.buttons.$bn {left fill expand}
    }
    bind $w.list <Button-1> " \
                if \[winfo exists %W\] \{
	        %W select clear 0 end ; \
		%W select set \[%W nearest %y\] ;\
		$w.entry delete 0 end ;\
		$w.entry insert 0 \[lindex \[selection get\] 0\] \}; break"
    bind $w.list <Double-Button-1> " \
	        %W select clear 0 end ; \
		%W select set \[%W nearest %y\] ;\
		$w.entry delete 0 end ;\
		$w.entry insert 0 \[lindex \[selection get\] 0\] ;\
		set select_done okay; break"
    bind $w.entry <Double-Button-1> {set select_done okay; break}
    bind $w.entry <Return> {set select_done okay; break}
    set prevfoc [focus]
    focus $w.entry
    
    #putcenter $w
    top_geometry $w +300+300
    
    bind $w <Visibility> "if !\[winfo exists .pathentry_popup\] \"raise $w\""
    
    tkwait variable select_done
    if {$select_done == "okay"} {set value [$w.entry get]}
    if {$select_done == "cancel"} {set value *cancel*}
    if {$value == ""} {
	set value [lfirst $choices]
	$w.entry insert 0 $value
	update
	after 100
    }
    top_destroy $w
    if {$prevfoc != ""} { focus $prevfoc }
    
    update
    return $value
}

proc select_one_lc {w msgArgs choices default extras} {
    set res [select_one $w $msgArgs $choices $default $extras]
    if 0 {
	if {[regexp \[A-Z\] $res] == 1} {
	    infomsg "DDN path names must not contain \
capital letters!  Converting to lower case."
	    set res [string tolower $res]
	}
    }
    return $res
}
