
# Set up a form with pre-selection, etc.
#
# Copyright (c) 2002 Ansys, Inc, all rights reserved.

proc gui_form_define {title name settings code} {
    set buts {done cancel}
    set d [form_init .$name $title "" $buts]
    if {$d != ""} {
	set row 0
	foreach item $settings {
	    gui_form_item $d $item $name $row
	    incr row
	}
	form_finish bot
    }
    gui_form_preselect $name $settings
    if ![form_wait .$name] return
    
    return [gui_form_eval $name $code]
}

# Use wierd __ names so they don't conflict with the variables the user set.

proc gui_form_eval {__name __code} {
    upvar \#0 $__name __lname
    
    foreach __v [array names __lname] {
	puts "set $__v $__lname($__v)"
	set $__v $__lname($__v)
    }
    return [eval $__code]
}

proc gui_form_item {d item name row} {
    global $name
    
    array set vals {
	-text ""
	-num 0
	-preselect 0
	-default ""
	-options ""
	-active ""
	-new_ok 0
	-filetypes ""
	-always_default 0
    }
    foreach {n v} [lrange $item 1 end] { set vals($n) $v }
    
    switch -- [lindex $item 0] {
	GROUP {
	    set d2 [form_label_frame $d.r$row $vals(-text) "$row 0 -cols -" \
			$vals(-active)]
	    set row2 0
	    foreach item $vals(-items) {
		gui_form_item $d2 $item $name $row2
		incr row2
	    }
	}
	LABEL {
	    form_label $d.r$row $vals(-text) "$row 0 -cols -" $vals(-active)
	}
	default {
	    set vn ${name}([lindex $item 0])
	    if {![info exists $vn] || $vals(-always_default)} {
		set $vn $vals(-default)
	    }
	    
	    set t $vals(-type)
	    switch $t {
		surface - curve - point - material - density - loop {
		    form_label $d.l$row $vals(-text) "$row 0" $vals(-active)
		    form_entry $d.e$row $vn string "$row 1" $vals(-active)
		    form_button $d.b$row Select \
			[list gui_form_select $t $vn $vals(-text) $vals(-num)] \
			"$row 2" $vals(-active)
		}
		location {
		    form_label $d.l$row $vals(-text) "$row 0" $vals(-active)
		    form_entry $d.e$row $vn coords "$row 1" $vals(-active)
		    form_button $d.b$row Select \
			[list gui_form_select $t $vn $vals(-text) 1] \
			"$row 2" $vals(-active)
		}
		color {
		    form_label $d.l$row $vals(-text) "$row 0" $vals(-active)
		    futil_colorentry $d.e$row $vn "$row 1" $vals(-active)
		}
		int - float - string {
		    form_label $d.l$row $vals(-text) "$row 0" $vals(-active)
		    form_entry $d.e$row $vn $vals(-type) \
			"$row 1" $vals(-active)
		}
		options {
		    form_label $d.l$row $vals(-text) "$row 0" $vals(-active)
		    futil_menuentry_labels $d.e$row $vn \
			[concat list $vals(-options)] "$row 1" $vals(-active)
		}
		file {
		    form_label $d.l$row $vals(-text) "$row 0" $vals(-active)
		    file_select_entry $d.e$row $vn 0 $vals(-new_ok) \
			$vals(-text) "$row 1 -cols -" $vals(-active) \
			"" "" "" "" "" "" 1 "" "" $vals(-filetypes)
		}
	    }
	}
    }
}

proc gui_form_select {type var mess maxn} {
    upvar \#0 $var lvar
    switch $type {
	location {
	    set mess "Select the [string tolower $mess] with the left button; middle or right = cancel."
	    set p [get_one_location $mess]
	    if {$p != ""} { set lvar $p }
	}
	surface - curve - point - material - density - loop {
	    set p [select_geometry $type $maxn [string tolower $mess]]
	    if {$p != ""} {
		set lvar ""
		foreach p2 $p {
		    lappend lvar [object_name_of_image $p2]
		}
	    }
	}
    }
}

proc gui_form_preselect {name settings} {
    foreach s $settings {
	set vals(-preselect) 0
	set vals(-num) 0
	foreach {n v} [lrange $s 1 end] { set vals($n) $v }
	if $vals(-preselect) {
	    # Currently this works only for locations and geom objects.
	    gui_form_select $vals(-type) ${name}([lindex $s 0]) \
		$vals(-text) $vals(-num)
	    
	} elseif {[lindex $s 0] == "GROUP"} {
	    gui_form_preselect $name $vals(-items)
	}
    }
}
