#!/usr/local/bin/wish -f

# This is a list of the namelists that will appear in the input file.

set default_namelists {
    START
    IFLOW
    SSWICH
    PEPINP
    DUVAL
    CHFCR
    COREL
    FORCNG
}

# For each namelist, we define a list of variables that go in this
# list.  Each variable is defined by a list with five elements:
#	1. The name.  This should not contain spaces, or other strange
#	   characters unless they are properly quoted.
#	2. The type.  This can either be int, bool, or real.
#	3. The minimum number of elements in the value list.
#	4. The maximum number of elements in the value list.
#	5. The default value.  This can actually be zero or more values
#	   (separated by spaces).  They should be of the type given above.

set default_namelist_vars(START) {
    { IEDIT   int  1 1 100 }
    { IRNOPT  int  1 1 13 }
    { ISCOMP  int  1 1 0 }
    { IDRFT   int  1 1 1 }
    { NOTSTP  int  1 1 200 }
    { DELT    real 1 1 0.1 }
    { IAUTO   int  1 1 0 }
    { IHAP    int  1 1 90 }
    { MCHANL  int  1 1 0 }
    { IMCEDT  int  1 1 1000 }
    { IMTSTP  int  1 1 1000 }
    { IMVEDT  int  1 1 1000 }
}

set default_namelist_vars(IFLOW) {
    { IFLO    int 1 1 1 }
}

set default_namelist_vars(SSWICH) {
    { MLTG    real 1 1 1.0 }
    { MLTQQ   real 1 1 400000.0 }
    { MLTMQ   real 1 1 400000.0 }
    { MTHICK  real 1 1 1.0 }
    { RTHICK  real 1 1 0.8 }
    { INPFD   int  1 1 2 }
    { MLTDX   real 1 1 1.0 }
    { MLTDY   real 1 1 1.0 }
    { MLTDZ   real 1 1 1.0 }
    { IPRESS  int  1 1 2 }
    { IRST    int  1 1 1000 }
    { IFRIC   int  1 1 1 }
    { NATRL   int  1 1 0 }
    { ILOCAL  int  1 1 1 }
    { AUTOR   real 1 1 1.0 }
    { HTIGHT  real 1 1 1e-13 }
    { IQULF   int  1 1 0 }
    { ICONTI  int  1 1 2 }
    { IBLAB   int  1 1 2 }
    { IDBG    int  1 1 1 }
    { IFREE   int  1 1 0 }
    { KTMIN   int  1 1 15 }
    { IGENQ   int  1 1 0 }
    { IEDC    int  1 100
	1 0 1 1 1 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1
    }
    { IEDC(34) int 1 1 0 }
    { IEDC(8)  int 1 1 0 }
    { IEDC(60) int 1 1 0 }
}

set default_namelist_vars(PEPINP) {
    { IDHMAX   int  1 1 1 }
    { IDUDT    int  1 1 1 }
    { IDNB     int  1 1 1 }
    { IGOUT    int  1 1 1 }
    { IH(1,1)  int  1 2 2 9 }
    { IH(1,2)  int  1 2 2 13}
    { IUN(1,1) int  1 3 2 9 1}
    { IUN(1,2) int  1 3 2 9 3}
    { IPR(1,1) int  1 2 23 6 }
}

set default_namelist_vars(DUVAL) {
    { IGEOM   int  1 1 2 }
    { RZOR    real 1 1 -1.0 }
    { P       real 1 1 2000.0 }
    { NSFS    int  1 1 -10000 }
    { DXINIT  real 1 1 1.0 }
    { DYINIT  real 1 1 5.0 }
    { DZINIT  real 1 1 0.8 }
    { HINIT   real 1 1 550.0 }
    { HINLET  real 1 1 550.0 }
    { HREVRS  real 1 1 1000.0 }
    { VINIT   real 1 1 400000.0 }
    { UINIT   real 1 1 1000.0 }
    { AC      real 1 1 32.172 }
    { ALPHA   real 1 1 -5.0 }
    { BKINIT  real 1 1 0.0 }
    { DHINIT  real 1 1 -1.0 }
    { QINIT   real 1 1 0.0 }
    { C1ZF    real 1 1 1.4 }
    { C3ZF    real 1 1 0.0 }
    { C4ZF    real 1 1 3.0 }
    { IMIX    int  1 1 1 }
}

set default_namelist_vars(CHFCR) {
    { ICHNL   int  1 1 2 }
    { THANG   real 1 1 5.0 }
    { GAMANG  real 1 1 0.0 }
    { IBETA   int  1 1 5 }
    { NCCF    int  1 1 1 }
    { NCCF2   int  1 1 1 }
    { KCHFOP  int  1 1 0 }
    { ISEDIT  int  1 1 30 }
    { ASYOP   int  1 1 1 }
    { COMBOP  int  1 1 1 }
    { DNBCC1  real 1 1 1.23 }
    { DNBLIM  real 1 1 0.01 }
    { CWIDTH  real 1 1 11.0 }
    { ISEARCH int  1 1 0 }
    { ISTOPP  int  1 1 0 }
    { SRCH1   real 1 9 450.0 -0.3 0.8 1.0 0.005 1.0 0.0005 1.1 0.001 }
    { ISRCH2  int  1 2 10 1 }
    { SRCH3   real 1 1 1.0 }
    { SRCH4   real 1 3 0.001 0.0 0.9 }
    { SRCH5   real 1 1 0.0 }
    { SRCH6   real 1 1 0.85 }
    { SDHMAX  real 1 1 0.05 }
    { SCHFPR  real 1 1 0.0005 }
    { QCHFPR  real 1 1 0.005 }
    { NLAVG   int  1 1 14 }
    { NSAVG   int  1 1 4 }
    { IRSPA   int  1 1 1 }
}

set default_namelist_vars(COREL) {
    { JDRFLX  int  1 1 0 }
    { KFI     int  1 1 1 }
    { KFV     int  1 1 2 }
    { NPDF    int  1 1 0 }
    { KDU     int  1 1 4 }
    { ICORR   int  1 1 0 }
    { IELPR   int  1 1 0 }
    { IPLOP   int  1 1 0 }
    { NREG    int  1 1 2 }
    { BCHF    int  1 1 3 }
    { IPLT    int  1 1 1 }
    { IP1D    int  1 1 4 }
    { CONDUCT real 1 2 8.2 8.2 }
    { SPHEAT  real 1 2 0.1 0.1 }
    { DENMTL  real 1 2 500.0 500.0 }
    { PWFAC   real 1 2 0.0 1.0 }
    { FTHK    real 1 2 0.25 0.25 }
    { PCTMLT  real 1 1 -1.0 }
    { IPLOPM  int  1 1 1 }
    { NREGM   int  1 1 4 }
    { ISYMP   int  1 1 1 }
    { CONDUM  real 1 4 8.2 8.2 8.2 8.2 }
    { SPHEAM  real 1 4 0.1 0.1 0.1 0.1 }
    { DENMM   real 1 4 500.0 500.0 500.0 500.0 }
    { PWFAM   real 1 4 0.0 1.0 1.0 0.0 }
    { FTHKM   real 1 4 0.25 0.25 0.25 0.25 }
    { JSEARCH int  1 1 0 }
}

set default_namelist_vars(FORCNG) {
    { FLXF    real 1 1 0.0 }
}

set title_string "IFLOW SAMPLE PROBLEM"
set option_string "RSWRIT    RSREAD    TFILE     PFILE     CFILE     FILID1"

# ------------------------------------------------------------------------

# Below here is the actual GUI that allows the user to edit a set of values.

# The file has the following format for records:
#   name type minlen maxlen active len value ...

proc save_file {filename} {
    global namelist_val title_string option_string namelist_vars namelists
    global namelist_active
    
    if ![check_namelists] return
    
    set fp [open $filename w]
    puts $fp "ICEM IFLOW INTERFACE"
    puts $fp $title_string
    puts $fp $option_string
    puts $fp [llength $namelists]
    foreach nl $namelists {
	puts $fp "$nl [llength $namelist_vars($nl)]"
	foreach var $namelist_vars($nl) {
	    set vn [lindex $var 0]
	    set len [llength $namelist_val($nl,$vn)]
	    set act $namelist_active($nl,$vn)
	    puts $fp "[lrange $var 0 3] $act $len $namelist_val($nl,$vn)"
	}
    }
    close $fp
}

proc load_file {filename} {
    global namelist_val title_string option_string namelist_active
    global namelist_vars namelists
    
    set fp [open $filename r]
    set line [gets $fp]
    if {$line != "ICEM IFLOW INTERFACE"} {
	error "Incorrect file type"
    }
    set title_string [gets $fp]
    set option_string [gets $fp]
    set num_namelists [gets $fp]
    while {![eof $fp]} {
	set line [gets $fp]
	if {$line == ""} continue
	set nl [lindex $line 0]
	set num [lindex $line 1]
	lappend namelists $nl
	set namelist_vars($nl) ""
	for {set i 0} {$i < $num} {incr i} {
	    if [eof $fp] { error "Short data file" }
	    set line [gets $fp]
	    foreach j {0 1 2 3 4 5} {
		set v [lindex {var type minlen maxlen active len} $j]
		set $v [lindex $line $j]
	    }
	    incr j
	    lappend namelist_vars($nl) [list $var $type $minlen $maxlen]
	    set val [lrange $line $j end]
	    set namelist_val($nl,$var) $val
	    set namelist_active($nl,$var) $active
	}
    }
    close $fp
}

proc check_namelists {} {
    global namelists namelist_vars namelist_val namelist_active

    set int_regexp {^[	]*-?[0-9]+[	]*$}
    set float_regexp {^[	]*-?[0-9]*(.[0-9]*)?([eE][+-]?[0-9]+)?[ 	]*$}
    set warn_mess "\nYou can either continue checking values and\
			ignore the error, or stop and fix the problem without\
			writing the file."
    
    foreach nl $namelists {
	foreach var $namelist_vars($nl) {
	    set vn [lindex $var 0]
	    if {!$namelist_active($nl,$vn)} continue
	    set vals $namelist_val($nl,$vn)
	    set type [lindex $var 1]
	    set minc [lindex $var 2]
	    set maxc [lindex $var 3]
	    set nvals [llength $vals]
	    if {($nvals < $minc) || ($nvals > $maxc)} {
		set mess "Namelist \"$nl\", variable \"$vn\": $nvals values\
			were given, but the minimum should be $minc and the\
			maximum $maxc."
		if ![info exists warned_once] {
		    set warned_once 1
		    append mess $warn_mess
		}
		if [tk_dialog .check "Value problem" $mess "" 0 Continue Stop] {
		    return 0
		}
	    }
	    foreach v $vals {
		case $type in {
		    bool { set tname boolean; set exp {^0|1$} }
		    int  { set tname integer; set exp $int_regexp }
		    real { set tname real;    set exp $float_regexp }
		}
		if ![regexp $exp $v dummy] {
		    set mess "Namelist \"$nl\", variable \"$vn\": the type\
				of this variable is \"$tname\", but a value of\
				\"$v\" was found."
		    if ![info exists warned_once] {
			set warned_once 1
			append mess $warn_mess
		    }
		    if [tk_dialog .check "Value problem" $mess "" 0 Continue Stop] {
			return 0
		    }
		}
	    }
	}
    }
    return 1
}

proc switch_nl {} {
    global current_nl nl_frame
    
    foreach w [pack slaves .middle] { pack forget $w }
    pack $nl_frame($current_nl) -fill none -anchor w
}

proc resize_window {} {
    global namelists current_nl    

    set maxw 0
    set maxh 0
    foreach nl $namelists {
	set current_nl $nl
	switch_nl
	update idle
	set w [winfo reqwidth .middle]; if {$w > $maxw} { set maxw $w }
	set h [winfo reqheight .middle]; if {$h > $maxh} { set maxh $h }
    }
    
    pack propagate .middle 1
    .middle configure -width $maxw -height [expr {$maxh + 20}]
    pack propagate .middle 0    
}

proc save_values {} {
    global filename variable_modified
    
    if [info exists filename] {
	save_file $filename
    }
    set variable_modified 0
}

proc revert_values {} {
    global namelist_val saved_namelist_val variable_modified 
    
    foreach i [array names namelist_val] {
	set namelist_val($i) $saved_namelist_val($i)
    }
    set variable_modified 0
}

proc add_namelist {} {
    global namelist_vars namelists tmp_namelist_name tmp_namelist_done
    global current_nl variable_modified
    
    set tmp_namelist_name ""
    toplevel .q
    label .q.l -text "Name for new namelist:"
    entry .q.e -textvar tmp_namelist_name -relief sunken \
	-background white -borderwidth 2
    button .q.c -text "Cancel" -command "set tmp_namelist_done 0"
    pack .q.l .q.e .q.c -side top -fill both -padx 4 -pady 4
    bind .q.e <Return> "set tmp_namelist_done 1"
    wm geometry .q +200+200
    wm title .q "Add namelist"
    focus .q.e
    
    tkwait variable tmp_namelist_done
    destroy .q
    if {!$tmp_namelist_done || ($tmp_namelist_name == "")} return
    set nl $tmp_namelist_name
    if [info exists namelist_vars($nl)] {
	set current_nl $nl
	switch_nl
	return
    }
    lappend namelists $nl
    set namelist_vars($nl) ""
    create_nlform $nl
    repack_top
    set current_nl $nl
    switch_nl
    set variable_modified 1
}

proc delete_namelist {} {
    global namelist_vars namelists tmp_namelist_name tmp_namelist_done
    global current_nl variable_modified
    
    if [tk_dialog .delete Delete "Delete namelist \"$current_nl\"?" "" 1 "Yes" "No"] {
	return
    }
    set nl $current_nl
    
    set nnl ""
    foreach n $namelists { if {$n != $nl} { lappend nnl $n } }
    set namelists $nnl
    set current_nl [lindex $namelists 0]
    switch_nl
    
    unset namelist_vars($nl)
    delete_nlform $nl
    set variable_modified 1
}

proc delete_nlform {nl} {
    global nl_frame nl_radio
    
    destroy $nl_frame($nl)
    destroy $nl_radio($nl)
    repack_top
}

proc add_entry {} {
    global namelist_vars namelists tmp_namelist_name tmp_namelist_done
    global current_nl tmp_namelist_type tmp_namelist_min tmp_namelist_max
    global tmp_namelist_val namelist_active namelist_val
    global saved_namelist_val variable_modified
    
    set tmp_namelist_name ""
    if ![info exists tmp_namelist_type] {
	set tmp_namelist_type real
	set tmp_namelist_min 1
	set tmp_namelist_max 1
	set tmp_namelist_val 0	
    }
    
    toplevel .q
    
    label .q.namel -text "Entry name:"
    entry .q.name -textvariable tmp_namelist_name -relief sunken \
	-background white
    
    label .q.typel -text "Value type:"
    radiobutton .q.typei -variable tmp_namelist_type -value int -text Integer
    radiobutton .q.typef -variable tmp_namelist_type -value real -text Real
    radiobutton .q.typeb -variable tmp_namelist_type -value bool -text Boolean
    
    label .q.minl -text "Min # values:"
    entry .q.min -textvariable tmp_namelist_min -relief sunken \
	-background white
    
    label .q.maxl -text "Max # values:"
    entry .q.max -textvariable tmp_namelist_max -relief sunken \
	-background white
    
    label .q.vall -text "Initial value:"
    entry .q.val -textvariable tmp_namelist_val -relief sunken \
	-background white
    
    frame .q.b
    button .q.b.done -text Done -command "set tmp_namelist_done 1"
    button .q.b.cancel -text Cancel -command "set tmp_namelist_done 0"
    pack .q.b.done .q.b.cancel -side left -fill both -expand 1
    
    blt_table .q .q.namel 1,1
    blt_table .q .q.typel 2,1
    blt_table .q .q.minl  3,1
    blt_table .q .q.maxl  4,1
    blt_table .q .q.vall  5,1
    
    blt_table .q .q.name 1,2 -columnspan 3 -fill both
    blt_table .q .q.typei 2,2 -fill both
    blt_table .q .q.typef 2,3 -fill both
    blt_table .q .q.typeb 2,4 -fill both
    blt_table .q .q.min  3,2 -columnspan 3 -fill both
    blt_table .q .q.max  4,2 -columnspan 3 -fill both
    blt_table .q .q.val  5,2 -columnspan 3 -fill both
    
    foreach i {name min max val} { bind .q.$i <Return> "set tmp_namelist_done 1" }
    
    blt_table .q .q.b 7,1 -columnspan 4 -fill both -padx 2 -pady 2
    blt_table column .q configure 5 -width 4
    blt_table row .q configure 0 -height 4
    blt_table row .q configure 6 -height 4
    blt_table row .q configure 8 -height 4
    
    wm geometry .q +200+200
    wm title .q "Add name entry"
    focus .q.name
    
    tkwait variable tmp_namelist_done
    destroy .q
    
    if !$tmp_namelist_done return
    if {$tmp_namelist_name == ""} return
    
    set nl $current_nl
    set vn $tmp_namelist_name
    if [info exists namelist_val($nl,$vn)] { blt_bell 0; return }
    
    set var "$vn $tmp_namelist_type $tmp_namelist_min $tmp_namelist_max\
		$tmp_namelist_val"
    lappend namelist_vars($nl) $var
    set namelist_val($nl,$vn) $tmp_namelist_val
    set saved_namelist_val($nl,$vn) $tmp_namelist_val
    set namelist_active($nl,$vn) 1
    
    create_nlentry $nl $var
    sort_form $nl
    set variable_modified 1
    #resize_window
    #set current_nl $nl
    #switch_nl
}

proc delete_entry {} {
    global namelist_vars namelists 
    global current_nl entry_name nl_frame
    global namelist_active namelist_val
    global saved_namelist_val variable_modified
    
    # Hmm, this is a bit tricky.
    set nl $current_nl
    set wid [focus]
    foreach e [array names entry_name] {
	set ewid $nl_frame($nl).e_$entry_name($e)
	if {$ewid == $wid} {
	    set xx [split $e ,]
	    if {[lindex $xx 0] == $nl} {
		set vn [lindex $xx 1]
		break
	    }
	}
    }
    if ![info exists vn] {
	error "You must click in a variable entry before\
	performing the delete operation."
    }
    if [tk_dialog .delete Delete "Delete variable \"$vn\"?" "" 1 "Yes" "No"] {
	return
    }
    
    set nnv ""
    foreach nv $namelist_vars($nl) { if {[lindex $nv 0] != $vn} { lappend nnv $nv } }
    set namelist_vars($nl) $nnv
    unset namelist_val($nl,$vn)
    unset saved_namelist_val($nl,$vn)
    unset namelist_active($nl,$vn)
    
    set en $entry_name($nl,$vn)    
    set f $nl_frame($nl)
    destroy $f.l_$en
    destroy $f.e_$en
    unset entry_name($nl,$vn) 

    sort_form $nl
    
    set variable_modified 1
}

proc create_nlform {nl} {
    global namelist_vars nl_frame nl_radio

    for {set i 0} {[winfo exists .top.b$i]} {incr i} {}
    set b .top.b$i
    set nl_radio($nl) $b
    
    radiobutton $b -text $nl -variable current_nl -value $nl \
	-command switch_nl -anchor w
    
    set vars $namelist_vars($nl)
    set nvars [llength $vars]
    
    frame .middle.f$i
    set nl_frame($nl) .middle.f$i

    foreach var $namelist_vars($nl) {
	create_nlentry $nl $var
    }
    sort_form $nl
}

proc repack_top {} {
    global namelists nl_radio
    set i 0
    foreach nl $namelists {
	set b $nl_radio($nl)
	set rr [expr {$i / 6 + 1}]
	set cc [expr {$i % 6 + 1}]
	blt_table [winfo parent $b] $b $rr,$cc -padx 4 -fill both
	incr i
    }
}

proc create_nlentry {nl var} {
    global entry_name num_entry_forms namelist_vars nl_frame
    
    set vn [lindex $var 0]
    
    if ![info exists num_entry_forms] {
	set num_entry_forms 1
    } else {
	incr num_entry_forms
    }
    set entry_name($nl,$vn) $num_entry_forms
    
    set f $nl_frame($nl)
    
    set ll $f.l_$num_entry_forms
    set ee $f.e_$num_entry_forms
    checkbutton $ll -text $vn -variable namelist_active($nl,$vn) -command \
	"toggle_active $ee $nl $vn" -relief flat -anchor w
    entry $ee -textvariable namelist_val($nl,$vn) \
	-relief sunken -background white -borderwidth 2 -width 16
    
    toggle_active $ee $nl $vn
}

proc sort_form {nl} {
    global entry_name namelist_vars nl_frame
    
    set form $nl_frame($nl)
    if ![catch {blt_table slaves $form} slaves] {
	foreach s $slaves { blt_table forget $s }
    }
    
    set nvars [llength $namelist_vars($nl)]
    set nr [expr {int(($nvars + 2) / 3)}]
    
    set vc 1
    set vr 1
    foreach var $namelist_vars($nl) {
	set vn [lindex $var 0]
	set ename $entry_name($nl,$vn)
	blt_table $form $form.l_$ename $vr,[expr {$vc * 2}] -fill both
	blt_table $form $form.e_$ename $vr,[expr {$vc * 2 + 1}] -fill both
	
	incr vr
	if {$vr > $nr} { incr vc; set vr 1 }
    }
    if $nvars {
	blt_table row $form configure 0 -height 4
    }
}

proc quit {} {
    global variable_modified
    
    if $variable_modified {
	set ch [tk_dialog .modified Modified \
		"Some values have been modified.  Save before exiting?" \
		    "" 0 "Save" "Don't save" "Cancel quit"]
	case $ch in {
	    0 save_values
	    2 return
	}
    }
    exit 0
}

proc toggle_active {ent nl vn} {
    global namelist_active
    
    if $namelist_active($nl,$vn) {
	$ent configure -state normal -foreground black
    } else {
	$ent configure -state disabled -foreground gray
    }
}

# This function was swiped from the standard tk_dialog...

proc tk_dialog {w title text bitmap default args} {
    global tk_priv

    # 1. Create the top-level window and divide it into top
    # and bottom parts.

    catch {destroy $w}
    toplevel $w -class Dialog
    wm title $w $title
    wm iconname $w Dialog
    frame $w.top -relief raised -bd 1
    pack $w.top -side top -fill both
    frame $w.bot -relief raised -bd 1
    pack $w.bot -side bottom -fill both

    message $w.msg -width 3i -text $text
    pack $w.msg -in $w.top -side right -expand 1 -fill both -padx 5m -pady 5m
    if {$bitmap != ""} {
	label $w.bitmap -bitmap $bitmap
	pack $w.bitmap -in $w.top -side left -padx 5m -pady 5m
    }

    set i 0
    foreach but $args {
	button $w.button$i -text $but -command "set tk_priv(button) $i"
	if {$i == $default} {
	    frame $w.default -relief sunken -bd 1
	    raise $w.button$i $w.default
	    pack $w.default -in $w.bot -side left -expand 1 -padx 3m -pady 2m
	    pack $w.button$i -in $w.default -padx 2m -pady 2m \
		    -ipadx 2m -ipady 1m
	    bind $w <Return> "$w.button$i flash; set tk_priv(button) $i"
	} else {
	    pack $w.button$i -in $w.bot -side left -expand 1 \
		    -padx 3m -pady 3m -ipadx 2m -ipady 1m
	}
	incr i
    }

    wm withdraw $w
    update idletasks
    set x [expr [winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
	    - [winfo vrootx [winfo parent $w]]]
    set y [expr [winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
	    - [winfo vrooty [winfo parent $w]]]
    wm geom $w +$x+$y
    wm deiconify $w

    set oldFocus [focus]
    grab $w
    focus $w

    tkwait variable tk_priv(button)
    destroy $w
    focus $oldFocus
    return $tk_priv(button)
}

# -------------------------------------------------------------------------

set tfont -*-helvetica-bold-r-normal--18*
set lfont -*-helvetica-bold-r-normal--14*
set efont -*-helvetica-medium-r-normal--14*
option add *font $lfont
option add Entry*font $efont

# Read in the input file if any.

set filename test
if {$argc > 0} {
    set filename [lindex $argv 0]
    if [file exists $filename] {
	load_file $filename
    }
}

if ![info exists namelist_val] {
    # Initialize all the variables to their default values.
    
    set namelists $default_namelists
    foreach nl $namelists {
	set namelist_vars($nl) $default_namelist_vars($nl)
	foreach var $namelist_vars($nl) {
	    set vn [lindex $var 0]
	    set namelist_val($nl,$vn) [lrange $var 4 end]
	    set namelist_active($nl,$vn) 1
	}
    }
}

trace variable namelist_val w "set variable_modified 1; list"

foreach i [array names namelist_val] {
    set saved_namelist_val($i) $namelist_val($i)
}

# Put up the editing GUI.

frame .title
frame .top
frame .middle -relief sunken -borderwidth 2
frame .bottom

foreach nl $namelists {
    create_nlform $nl
}
repack_top

label .title.ident -text "ICEM CFD IFLOW SCALAR FILE INTERFACE" -font $tfont
label .title.titlel -text "Title:"
entry .title.title -textvariable title_string -relief sunken \
    -background white -width 80
label .title.optionl -text "Options:"
entry .title.option -textvariable option_string -relief sunken \
    -background white -width 80
label .title.filel -text "Filename:"
entry .title.file -textvariable filename -relief sunken \
    -background white -width 80

blt_table .title .title.ident 1,1 -columnspan 2 -anchor c -pady 4
blt_table .title .title.titlel 2,1
blt_table .title .title.title 2,2
blt_table .title .title.optionl 3,1
blt_table .title .title.option 3,2
blt_table .title .title.filel 4,1
blt_table .title .title.file 4,2

button .bottom.addnl -text "Add namelist" -command add_namelist
button .bottom.delnl -text "Delete namelist" -command delete_namelist
button .bottom.addent -text "Add entry" -command add_entry
button .bottom.delent -text "Delete entry" -command delete_entry

button .bottom.save -text Save -command save_values
button .bottom.revert -text Revert -command revert_values
button .bottom.quit -text Quit -command quit

pack .bottom.addnl .bottom.delnl .bottom.addent .bottom.delent \
    .bottom.save .bottom.revert .bottom.quit -side left -padx 2
pack .title -side top -padx 4 -pady 4 -anchor w
pack .top -side top -padx 4 -pady 4 -fill none -anchor w
pack .middle -side top -padx 4 -pady 4 -fill both -expand 1 -anchor w
pack .bottom -side top -padx 4 -pady 4 -fill none -anchor w

# Now make sure the size of the region doesn't jump around.

wm withdraw .
resize_window

set current_nl [lindex $namelists 0]
switch_nl

set variable_modified 0

wm geometry . +0+0
wm minsize . [winfo reqwidth .] [winfo reqheight .]
wm title . "ICEM CFD IFLOW"
wm deiconify .
