
# 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 dialog box that lets the user specify a set of parameters.

set run_dialog_done 0

proc run_dialog {w msgArgs stuff} {
    global run_dialog_done helpCmds
    
    disable_interface
    
    set run_dialog_done 0
    
    eval message $w.message -justify left -width 600 $msgArgs
    frame $w.things -borderwidth 0 -relief flat
    
    frame $w.buttons -borderwidth 2 -relief raised
    button $w.buttons.okay -text Done \
	-command {set run_dialog_done *okay*}
    button $w.buttons.cancel -text Cancel \
	-command {set run_dialog_done *cancel*}
    pack append $w.buttons \
	$w.buttons.okay {left fill expand padx 4} \
	$w.buttons.cancel {left fill expand padx 4}
    
    # Figure out some widths.
    set yesw 0
    set now 0
    set msgw 0
    set defw 0
    foreach item $stuff {
	set y [expr {[string length [lindex $item 0]] * 4 / 4}]
	set n [expr {[string length [lindex $item 1]] * 4 / 4}]
	set m [expr {[string length [lindex $item 2]] * 4 / 4}]
	set p [expr {[string length [lindex $item 3]]}]
	if {$y > $yesw} {set yesw $y}
	if {$n > $now}  {set now  $n}
	if {$m > $msgw} {set msgw $m}
	if {$p > $defw} {set defw $p}
    }
    
    set number 0
    foreach item $stuff {
	set yes [lindex $item 0]
	set no [lindex $item 1]
	set pathmsg [lindex $item 2]
	set pathdef [lindex $item 3]
	# Kind of wierd.
	set maybe [lindex $item 4]
	
	if {($yes != "") && ($no != "")} {
	    uplevel #0 set run_dialog_var_$number 1
	    frame $w.things.choice_$number \
		-borderwidth 0 -relief flat
	    if {$maybe == ""} {
		radiobutton $w.things.choice_$number.yes \
		    -text $yes -variable run_dialog_var_$number \
		    -value 1 -width $yesw -anchor nw
		radiobutton $w.things.choice_$number.no \
		    -text $no -variable run_dialog_var_$number \
		    -value 0 -width $now -anchor nw
		pack append $w.things.choice_$number \
		    $w.things.choice_$number.yes \
		    {left fill expand} \
		    $w.things.choice_$number.no \
		    {right fill expand}
	    } else {
		radiobutton $w.things.choice_$number.yes \
		    -text $yes -variable run_dialog_var_$number \
		    -value 1 -anchor nw
		radiobutton $w.things.choice_$number.no \
		    -text $no -variable run_dialog_var_$number \
		    -value 0 -anchor nw
		radiobutton $w.things.choice_$number.maybe \
		    -text $maybe -variable run_dialog_var_$number \
		    -value 2 -anchor nw
		pack append $w.things.choice_$number \
		    $w.things.choice_$number.yes \
		    {left fill expand} \
		    $w.things.choice_$number.no \
		    {left fill expand} \
		    $w.things.choice_$number.maybe \
		    {left fill expand}
	    }
	    
	    if {$pathmsg != ""} then {
		pack append $w.things \
		    $w.things.choice_$number \
		    {top fillx pady 3 frame s}
	    } else {
		pack append $w.things \
		    $w.things.choice_$number \
		    {top fillx pady 6}
	    }
	}
	if {$pathmsg != ""} {
	    frame $w.things.path_$number \
		-borderwidth 2 -relief raised
	    label $w.things.path_$number.label -text $pathmsg \
		-width $msgw -anchor nw
	    entry $w.things.path_$number.entry \
		-width $defw
	    bind $w.things.path_$number.entry <Return> {set run_dialog_done *okay*}
	    $w.things.path_$number.entry insert 0 $pathdef
	    pathentry on $w.things.path_$number.entry
	    pack append $w.things.path_$number \
		$w.things.path_$number.label {left fill expand} \
		$w.things.path_$number.entry {right fill expand}
	    if {($yes != "") && ($no != "")} then {
		pack append $w.things \
		    $w.things.path_$number \
		    {top fillx pady 3 frame n}
		uplevel #0 trace variable run_dialog_var_$number w \
		    \"run_dialog_alter $w.things.path_$number\"
	    } else {
		pack append $w.things \
		    $w.things.path_$number \
		    {top fillx pady 6}
	    }
	}
	
	incr number
    }
    
    
    pack append $w \
	$w.message {top fill} \
	$w.things {top fill} \
	$w.buttons {top fill}
    
    wm geometry $w +300+300
    #putcenter $w
    
    tkwait variable run_dialog_done
    
    set number 0
    foreach item $stuff {
	catch {uplevel #0 trace vdelete run_dialog_var_$number w \
	    \"run_dialog_alter $w.things.path_$number\"}
	incr number
    }
    
    if {$run_dialog_done == "*cancel*"} then {
	set result "*cancel*"
    } else {
	set result {}
	set number 0
	foreach item $stuff {
	    set yes [lindex $item 0]
	    set no [lindex $item 1]
	    set pathmsg [lindex $item 2]
	    set val 0
	    set path 0
	    if {($yes != "") && ($no != "")} {
		set val [uplevel #0 set run_dialog_var_$number]
	    }
	    if {$pathmsg != ""} {
		set path [$w.things.path_$number.entry get]
	    }
	    
	    lappend result "$val $path"
	    
	    incr number
	}
    }
    
    destroy $w
    enable_interface
    return $result
}

proc run_dialog_alter {base name1 name2 what} {
    global $name1
    set val [set $name1]
    if $val then {
	$base.label configure -foreground black
	$base.entry configure -foreground bisque
    } else {
	$base.label configure -foreground "#bbbbbb"
	$base.entry configure -foreground "#bbbbbb"
    }
}
