
# Stuff to do solver parameters.
# params is a list of { name default title }, title is optional

proc solver_params {title variable params} {
    global $variable saved_$variable solver_params_done solver_params_prev
    
    set w .solver_params
    catch {top_destroy $w}
    catch {unset solver_params_prev}
    
    top_window $w
    label $w.title -text $title
    scrollbar $w.scroll -command "$w.c yview" -relief sunken \
	    -borderwidth 2 -orient v
    canvas $w.c -yscrollcommand "$w.scroll set" -height 500 \
	-relief ridge -borderwidth 2
    frame $w.bot
    button $w.bot.done -text Done -command "set solver_params_done 1"
    button $w.bot.reset -text Reset -command "array set $variable \[array get saved_$variable\]"
    button $w.bot.cancel  -text Cancel -command "set solver_params_done 0"
    
    pack $w.bot.done $w.bot.reset $w.bot.cancel -side left -padx 10 -pady 10
    pack $w.title -side top -fill both
    pack $w.bot -side bottom -fill both
    pack $w.c -side left -fill both -expand 1 -padx 2 -pady 2
    pack $w.scroll -side left -fill y
    frame $w.c.win
    set fitem [$w.c create window 2 2 -window $w.c.win -anchor nw]
    bind $w.c.win <Configure> "solver_params_update $w 500"
    
    set r 0; set c 0
    foreach p $params {
	switch $p {
	    newrow {
		incr r 2
		set c 0
		continue
	    }
	    skip {
		incr c
		continue
	    }
	}
	set n [lindex $p 0]
	set t [lindex $p 1]; if {$t == ""} { set t $n }
	
	# lindex $p 2 is unused for some reason
	
	set geop ""; set entp ""
	set sss [lindex $p 3]
	for {set i 0; set j 1} {$i < [llength $sss]} {incr i 2; incr j 2} {
	    switch -- [lindex $sss $i] {
		-columnspan {
		    lappend geop [lindex $sss $i] [lindex $sss $j]
		}
		-width {
		    lappend entp [lindex $sss $i] [lindex $sss $j]
		}
	    }
	}
	
	if {$t != " "} { label $w.c.win.l$n -text $t }
	eval entry $w.c.win.e$n -width 8 -background white -borderwidth 2 \
	    -relief sunken -textvariable ${variable}($n) $entp
	if ![info exists ${variable}($n)] {
	    set ${variable}($n) [lindex $p 1]
	}
	set saved_${variable}($n) [set ${variable}($n)]
	
	if {$t != " "} { 
	    eval blt_table $w.c.win $w.c.win.l$n $r,$c \
		-padx 1 -pady 1 -anchor w $geop
	}
	eval blt_table $w.c.win $w.c.win.e$n [expr $r + 1],$c -padx 1 -pady 1 \
	    -fill both $geop
	set nsk 1
	if {[lindex $geop 0] == "-columnspan"} { set nsk [lindex $geop 1] }
	incr c $nsk
    }
    tkwait variable solver_params_done
    top_destroy .solver_params
    if !$solver_params_done {
	array set $variable [array get saved_$variable]
    }
    return $solver_params_done
}

proc solver_params_update {w h} {
    global solver_params_prev
    
    set can $w.c
    set width [expr [winfo reqwidth $can.win] + 4]
    set height [expr [winfo reqheight $can.win] + 4]
    if {[info exists solver_params_prev($can)] && \
	    ($solver_params_prev($can) == "$width $height")} return
    set solver_params_prev($can) "$width $height"
    
    $can configure -width $width -height $height \
	-scrollregion "0 0 $width $height"
    $can yview moveto [lindex [$can yview] 0]
}

proc solver_params_load {filename var} {
    global env $var solver_params_order file_name
    
    set file $file_name(transfer_dir)/$filename.params
    if ![file exists $file] {
	cmd_cp $env(ICEM_ACN)/icemcfd/output-interfaces/$filename.params $file
    }
    set solver_params_order($var) ""
    set fp [open $file r]
    while {![eof $fp]} {
	set line [gets $fp]
	set name [lindex $line 0]
	if {($name != "") && ![string match \#* $name]} {
	    set ${var}($name) [lindex $line 1]
	    lappend solver_params_order($var) $name
	} else {
	    lappend solver_params_order($var) $line
	}
    }
    close $fp
    return $file
}

proc solver_params_save {filename var} {
    global env $var solver_params_order file_name
    
    set file $file_name(transfer_dir)/$filename.params
    set fp [open $file w]
    foreach p $solver_params_order($var) {
	if [info exists ${var}($p)] {
	    puts $fp [list $p [set ${var}($p)]]
	    set written($p) 1
	} else {
	    puts $fp $p
	}
    }
    foreach p [array names $var] {
	if ![info exists written($p)] {
	    puts $fp [list $p [set ${var}($p)]]
	}
    }
	    
    close $fp
    return $file
}
