
# This code creates the user interface for the mesh editor.
#
# Copyright (c) 1997 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com
#

proc file_options {} {
    global option_subtype options is_win env prev_use_xwindows use_xwindows
    
    set prev_use_xwindows $use_xwindows
    
    # fix up possible problems
    array set optnames {
	ds,a82 ANSI-82
	ds,din DIN
	ds,d DIN
	pf,a Ascii
	pf,b Binary
	pf,a Ascii
	pf,b Binary
	pf,a ASCII
	pf,b BINARY
	um,e English
	um,m Metric
    }
    foreach o [array names options] {
	if [info exists optnames($o,$options($o))] {
	    set options($o) $optnames($o,$options($o))
	}
    }
    if ![info exists env(EDITOR)] {
	if $is_win {
	    set env(EDITOR) notepad
	} else {
	    set env(EDITOR) vi
	}
    }
    
    set option_subtype general
    
    before_set_options
    
    set buts {
	{"This project" {file_options_done 0}}
	{"All projects" {file_options_done 1}}
	nl
	{Defaults {project_settings_reset_defaults}}
	cancel
    }
    set d [form_init .options "Options" "" $buts]
    if {$d != ""} {
	
	set noteb [expr {[package versions BWidget] != ""}]

	if $noteb {
	    set notebook [NoteBook $d.nb]
	    pack $d.nb
	    foreach {n t} {
		general General
		display Display
		edit Edit
		ddn DDN
		colors Colors
		memory Memory
		remote Remote
		speed Speed
		solver Solver
	    } {
		set cc$n [$d.nb insert end $n -text $t]
	    }
	    
	} else {
	    form_frame $d.top flat 0 {0 0}
	    form_frame $d.sub ridge 2 {1 0}
	    
	    form_radiobutton $d.top.general General option_subtype general {0 0}
	    form_radiobutton $d.top.display Display option_subtype display {0 1}
	    form_radiobutton $d.top.edit Edit option_subtype edit {0 2}
	    form_radiobutton $d.top.ddn DDN option_subtype ddn {0 3}
	    form_radiobutton $d.top.col Colors option_subtype colors {1 0}
	    form_radiobutton $d.top.mem Memory option_subtype memory {1 1}
	    form_radiobutton $d.top.remote Remote option_subtype remote {1 2}
	    form_radiobutton $d.top.speed Speed option_subtype speed {1 3}
	    form_radiobutton $d.top.solver Solver option_subtype solver {2 0}
	    
	    foreach i {general display edit ddn colors memory remote speed solver} {
		set cc$i $d.top.$i
	    }
	}
	
	# ------------------------------
	
	set cc $ccgeneral
	if !$noteb { form_choiceframe $cc }
	
	form_checkbutton $cc.savenew "Always save data to new files" \
	    options(save_to_new) {0 0 -cols 2}
	form_label $cc.npl "Num processors" {3 0}
	form_entry $cc.np options(n_processors) int {3 1}
	form_label $cc.edl "Text editor" {4 0}
	form_entry $cc.ed env(EDITOR) string {4 1}
	form_label $cc.prev "Num previous project selections" {5 0}
	futil_menuentry $cc.preve options(prev_selections) "list 1 2 3 4 5\
	    6 7 8 9 10 11 12 13 14 15 16" {5 1}
	form_checkbutton $cc.autor "Automatically raise dialog boxes" \
	    auto_raise_forms {6 0 -cols 2}	
	form_checkbutton $cc.emmesh "Save emergency mesh on crash" \
	    med_save_emergency_mesh {7 0 -cols 2}
	form_checkbutton $cc.emtetin "Save emergency tetin on crash" \
	    med_save_emergency_tetin {8 0 -cols 2}
	form_checkbutton $cc.cleanup "Clean up temporary files" \
	    options(clean_up_tmp_files) {9 0 -cols 2}
	form_checkbutton $cc.mouse "Automatically load mouse bindings" \
	    options(auto_load_mouse_bind) {10 0 -cols 2}
	# ------------------------------
	
	set cc $ccdisplay
	if !$noteb { form_choiceframe $cc }
	
	form_radiobutton $cc.nat "Native graphics" use_xwindows 0 {1 0}
	form_radiobutton $cc.x11 "X11 graphics" use_xwindows 1 {1 1}
	form_checkbutton $cc.xhidden "X hidden surface" \
	    options(xhidden_full) {2 0 -cols 2} {{use_xwindows 1}}
	form_checkbutton $cc.dlrem "Use local display list in hexa" \
	    options(use_dlremote) {3 0 -cols 2}
	form_checkbutton $cc.showfactor "Show size params using ref size" \
	    options(show_factor) {14 0 -cols 2}
	form_checkbutton $cc.showtris "Show triangles when selecting surfaces" \
	    options(show_tris) {15 0 -cols 2}
	form_checkbutton $cc.homeafter "Home position after loading data" \
	    options(home_after_load) {16 0 -cols 2}
	form_label $cc.tooldl "Bubble help delay (ms)" {17 0}
	form_entry $cc.toold bubble_delay int {17 1}
	form_label $cc.wdispl "Warn if elements displayed" {20 0}
	form_entry $cc.wdisp disp_options(uns_warn_if_display) int {20 1}
	form_label $cc.precl "Float display precision" {21 0}
	form_entry $cc.prec med_float_precision int {21 1}
	form_label $cc.qal "Quadratic accuracy" {22 0}
	form_entry $cc.qa tdv_quadratic_accuracy int {22 1}
	#form_label $cc.ibl "Node/elem index base" {23 0}
	#form_entry $cc.ib med_index_base int {23 1}
	
	# ------------------------------
	
	set cc $ccedit
	if !$noteb { form_choiceframe $cc }
	
	form_radiobutton $cc.s_all "Select fully enclosed" \
	    options(select_mode) all {10 0 -cols 2}
	form_radiobutton $cc.s_some "Select partially enclosed" \
	    options(select_mode) some {11 0 -cols 2}
	form_label $cc.minaspl "Minimum aspect ratio" {12 0}
	form_entry $cc.minasp mesh_min_moving_element_aspect float {12 1}
	form_checkbutton $cc.wait "Wait for edit confirmation" \
	    options(edit_wait) {13 0 -cols 2}
	form_label $cc.cuttl "Cut delay threshhold" {14 0}
	form_entry $cc.cutt disp_options(uns_cut_delay_count) int {14 1}
	form_label $cc.flal "Flood select angle" {15 0}
	form_entry $cc.fla options(flood_select_angle) float {15 1}
	
	# ------------------------------
	
	set cc $ccddn
	if !$noteb { form_choiceframe $cc }
	
	form_label $cc.dsl "Drafting standard" {0 0}
	form_label $cc.pfl "Part format" {1 0}
	form_label $cc.uml "Measurement units" {2 0}
	form_label $cc.mll "DDN MIF license" {3 0}
	
	futil_menuentry $cc.ds ddn_options(ds) "list ANSI-82 DIN" {0 1}
	futil_menuentry $cc.pf ddn_options(pf) "list Ascii Binary" {1 1}
	futil_menuentry $cc.um ddn_options(um) "list Metric English" {2 1}
	futil_menuentry $cc.ml ddn_options(mif_license) \
	    "list hexa tetra global" {3 1}
	
	# --------------------------------
	
	set cc $cccolors
	if !$noteb { form_choiceframe $cc }
	
	form_label $cc.curl "Mouse cursor" {0 0}
	form_label $cc.redtextl "Error text" {1 0}
	
	futil_colorentry $cc.cur options(mouse_color) {0 1}
	futil_colorentry $cc.redtext options(redtext_color) {1 1}
	
	# --------------------------------
	
	set cc $ccmemory
	if !$noteb { form_choiceframe $cc }
	
	form_label $cc.lab "All values are in units of MB" {0 0 -cols 2}
	
	form_label $cc.dsizel "Max display list size" {1 0}
	form_entry $cc.dsize gsys_display_list_max_size int {1 1}
	
	form_label $cc.undombl "Max memory for undo" {2 0}
	form_entry $cc.undomb undo_max_memory int {2 1}
	
	form_checkbutton $cc.saveh "Enable undo/redo for large operations" \
	    undo_save_whole_mesh {3 0 -cols 2}
	
	form_label $cc.btl "Max binary tetin size" {4 0}
	form_entry $cc.bt options(max_binary_tetin) int {4 1}
	
	form_label $cc.cadml "Max geometry memory" {5 0}
	form_entry $cc.cadm options(max_cad_map_size) int {5 1}
	
	form_label $cc.mshml "Max mesh memory" {6 0}
	form_entry $cc.mshm options(max_mesh_map_size) int {6 1}
	
	# --------------------------------
	
	set cc $ccremote
	if !$noteb { form_choiceframe $cc }
	
	form_label $cc.hostl "Remote host" {0 0}
	form_label $cc.userl "Remote user" {1 0}
	form_label $cc.pathl "Remote directory" {2 0}
	form_label $cc.acnl "Remote ICEM_ACN" {3 0}
	form_label $cc.shl "Remote Shell" {4 0}
	
	form_entry $cc.host options(remote_host) string {0 1}
	form_entry $cc.user options(remote_user) string {1 1}
	form_entry $cc.path options(remote_path) string {2 1}
	form_entry $cc.acn options(remote_acn) string {3 1}
	form_frame $cc.sh flat 0 {4 1}
	form_radiobutton $cc.sh.c csh options(remote_sh) csh {0 0}
	form_radiobutton $cc.sh.s ksh/sh options(remote_sh) sh {0 1}
	
	# --------------------------------
	
	set cc $ccspeed
	if !$noteb { form_choiceframe $cc }
	
	form_label $cc.l "Fast dynamics:" {0 0}
	form_checkbutton $cc.m "Mesh" med_fast_dynamics {0 1}
	form_checkbutton $cc.g "Geom" geom_fast_dynamics {0 2}
	
	form_label $cc.thrl "Threshhold (ms)" {1 0}
	form_entry $cc.thr tdv_dynamics_speed_thresh int {1 1 -cols 2}
	
	# --------------------------------
	
	set cc $ccsolver
	if !$noteb { form_choiceframe $cc }
	
	form_label $cc.sol "Default solver:" {0 0}	
	form_entry $cc.soln options(default_solver) default {0 1}
	form_button $cc.solb "Select" "options_solution_select" {0 2}
	form_checkbutton $cc.solc "Automatically load default solver" \
	    options(auto_load_default_solver) {3 0 -cols 3}
	
	# --------------------------------

	if $noteb {
	    $d.nb compute_size
	    $d.nb raise [$d.nb pages 0]

	} else {
	    futil_setup_switch $d.sub option_subtype
	}
	
	form_finish bot
    }
}

proc file_update_expert {} {
    global options
    
    set ex1 [file exists [home_directory]/.icemcfd_expert]
    set ex2 [file exists ./.icemcfd_expert]
    
    if {$options(expert) && !$ex1 && !$ex2} {
	set mess "You have turned on expert mode.\nDo you want to enable it:\n\
- For just this project?\n\
- For all projects in this directory?\n\
- For all projects you create?"
	switch [tk_dialog .expert_enable "Expert mode" $mess "" -1 "This project" "This dir" "All projects"] {
	    0 { }
	    1 {
		close [open ./.icemcfd_expert w]
		mess "The file ./.icemcfd_expert was created\n"
	    }
	    2 {
		close [open [home_directory]/.icemcfd_expert w]
		mess "The file ~/.icemcfd_expert was created\n"
	    }
	}
    }
}

proc file_options_done {save} {
    global options ddn_options form_done_flag use_xwindows prev_use_xwindows
    global tdv_default

    if {$prev_use_xwindows != $use_xwindows} {
	mess "Note that changing the graphics type between OpenGL and X11\
		will affect subprograms started from ICEM CFD, and\
		also the next invocation of ICEM CFD (only if \"All\
		projects\" was selected) but not the current invocation.\n" red
    }
    
    form_done .options
    set form_done_flag(.options) 1

    if $save {
	set file [home_directory]/.icemcfd_options
	set fp [open $file w]
	puts $fp "array set options \{ [array get options] \}"
	puts $fp "array set ddn_options \{ [array get ddn_options] \}"
	puts $fp "set use_xwindows $use_xwindows"
	close $fp
	mess "Saved options to \"$file\"\n"
    }
    after_set_options
    $tdv_default redraw
}

proc before_set_options {} {
    global options
    
    set options(max_mesh_map_size) [ic_set_max_map_size mesh]
    set options(max_cad_map_size) [ic_set_max_map_size cad]
}

proc after_set_options {} {
    global options mess_colors cursor_color
    global pj_max_cad_map_size pj_max_mesh_map_size
    global batch_mode tdv_default solver_info solution

    if !$batch_mode {
	$tdv_default configure -xhidden $options(xhidden_full)
	set mess_colors(red) $options(redtext_color)
	set cursor_color $options(mouse_color)
	set_cursor $tdv_default [$tdv_default get -cursor]
    }
    
    if {[info exists pj_max_cad_map_size ] == 0 || \
		$pj_max_cad_map_size != $options(max_cad_map_size)} {
	ic_set_max_map_size cad $options(max_cad_map_size)
    }
    if {[info exists pj_max_mesh_map_size ] == 0 || \
		$pj_max_mesh_map_size != $options(max_mesh_map_size)} {
	ic_set_max_map_size mesh $options(max_mesh_map_size)
    }
    file_update_paths
    ic_set_clean_up_tmp_files $options(clean_up_tmp_files)
    if !$batch_mode {
	$tdv_default invalidate_caches all
        if ![info exists solver_info] { get_solver_info }
        if {$solution(solver) != "$options(default_solver)" && \
            $options(default_solver) != "" && \
            $options(auto_load_default_solver)} {
	        solution_set_solver $options(default_solver) 1
	}
    }
    
    b array set options [array get options]
}

proc file_option_read_old_modals {} {
    global options ddn_options env
    
    set mf [home_directory]/.iceman_modals
    if [file exists $mf] {
	source $mf
	
	if [info exists units] {
	    set ddn_options(um) $units
	}
	if [info exists draft] {
	    set ddn_options(ds) $draft
	}
	if [info exists partform] {
	    set ddn_options(pf) $partform
	}
	if [info exists ddn_mesher_license] {
	    set ddn_options(mif_license) $ddn_mesher_license
	}
	if [info exists editor] {
	    set env(EDITOR) $editor
	}
	if [info exists n_processors] {
	    set options(n_processors) $n_processors
	}
	if [info exists max_binary_tetin] {
	    set options(max_binary_tetin) $max_binary_tetin
	}
    }
}

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

proc file_edit_paths {} {
    global program_path program_path_list
    
    set buts {
	{"This project" {file_paths_done 0}}
	{"All projects" {file_paths_done 1}}
	reset cancel
    }
    
    set d [form_init .program_paths "Program paths" "" $buts]
    if {$d != ""} {
	form_label $d.lab "You can modify the locations that ICEM CFD\nwill\
look for programs by entering a pathname\nbelow.  A blank means that the\
default location\nunder ICEM_ACN will be used." {0 0}
	set scr [form_scroller $d.scr 400 {1 0}]
	set r 0
	foreach p [lsort $program_path_list] {
	    form_label $scr.l$r $p "$r 0"
	    form_entry $scr.r$r program_path($p) string "$r 1"
	    $scr.r$r configure -width 22
	    incr r
	}
	
	form_finish +300+300
	form_scroller_hack $d.scr
    }
}

proc file_paths_done {save} {
    global program_path form_done_flag
    
    form_done .program_paths
    set form_done_flag(.program_paths) 1
    
    if ![info exists program_path] return
    
    foreach i [ array names program_path ] {
        if { $program_path($i) != "" } {
            set pth [ glob -nocomplain [ string trim $program_path($i) ] ]
            if { $pth == "" || $pth == "." } {
                mess "value $program_path($i) for $i is not valid\n" red
            }
        }
    }
    if $save {
	set file [home_directory]/.icemcfd_paths
	set fp [open $file w]
	
	puts $fp "array set program_path {"
	foreach p [array names program_path] {
	    set pp [string trim $program_path($p)]
	    if {$pp != ""} {
		puts $fp "    [list $p $pp]"
	    }
	}
	puts $fp "}"
	close $fp
	mess "Saved pathnames to \"$file\"\n"
	mess "These changes will be applied every time you run ICEM CFD from any project.\n"
    } else {
	mess "The new pathnames will be active until you exit ICEM CFD.\n"
    }
    
    file_update_paths
}

proc file_edit_dirs {} {
    global file_name 
    
    set buts {
	{"This project" {file_paths_done 0}}
	{"All projects" {file_paths_done 1}}
	reset cancel
    }
    
    set dir_path_list [ array names file_name ]
    foreach i { $dir_path_list } {
	mess "$i\n"
    }

    set d [form_init .dir_paths "Directory paths" "" $buts]
    if {$d != ""} {
	form_label $d.lab "You can modify the locations that ICEM CFD\nwill\
look for data by entering a pathname\nbelow.  A blank means that the\
default location\nunder ICEM_ACN will be used." {0 0}
	set scr [form_scroller $d.scr 400 {1 0}]
	set r 0
	foreach p [lsort $dir_path_list] {
	    form_label $scr.l$r $p "$r 0"
	    form_entry $scr.r$r file_name($p) string "$r 1"
	    $scr.r$r configure -width 50
	    incr r
	}
	
	form_finish +300+300
	form_scroller_hack $d.scr
    }
}

proc file_dirs_done {save} {
    global file_name form_done_flag
    
    form_done .dir_paths
    set form_done_flag(.program_paths) 1
    
    if ![info exists file_name] return
    
    if $save {
	mess "Not implemented\n"
    } else {
	mess "The new pathnames will be active until you exit ICEM CFD.\n"
    }
}

proc file_update_paths {} {
    global program_path
    
    set vals ""
    foreach p [array names program_path] {
	set pp [string trim $program_path($p)]
	if {$pp != ""} {
	    lappend vals $p $pp
	}
    }
    ic_set_program_paths $vals
}

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

proc project_settings_save {pdir} {
    global file_name saved_params data_available
    global program_path AppName env

    if [ic_wb2_is_running] {
        foreach p [array names file_name] {    
            set n [aie_get_global file_name $p]
            if {$n != "___NA___"} {
                set file_name($p) $n
            }
        }
	    set saved_params [aie_get_global saved_params]
    } else {
        if {$file_name(settings) == ""} {
	        set file_name(settings) $file_name(project_dir)/icemcfd.prj
	    }
    }
    
    set pref [file dirname $file_name(settings)]/
    mess "Saving project settings to \"$file_name(settings)\"\n"
    
    set data ""
    foreach p $saved_params {
	    global $p
        if [ic_wb2_is_running] {
            if ![info exists $p] {
                set p [aie_get_global $p]
            }
        } 
	    if {$p == "file_name"} {
	        lappend data "array set $p {"
	        foreach n [array names $p] {
		        set v [set ${p}($n)]
		        if [string match ${pref}?* $v] {
		            set v [string range $v [string length $pref] end]
		        }
		        lappend data "    [list $n $v]"
	        }
	        lappend data "}"    
	    } elseif {[array names $p] == ""} {
	        if ![info exists $p] { continue }
	        set v [project_settings_make_relative $pdir $p [set $p] 1]
	        lappend data [list set $p $v]
	    } else {
	        if ![info exists $p] { continue }
	        lappend data "array set $p {"
	        foreach n [array names $p] {
		        set v [project_settings_make_relative $pdir ${p}($n) [set ${p}($n)] 1]
		        lappend data "    [list $n $v]"
	        }
	        lappend data "}"
	    }
    }
    if [info exists program_path] {
	    lappend data "array set program_path {"
	    foreach p [array names program_path] {
	        set pp [string trim $program_path($p)]
	        if {$pp != ""} {
		        lappend data "    [list $p $pp]"
	        }
	    }
	    lappend data "}"
    }
    if [ic_wb2_is_running] {
        set input [ic_wb2_get_parameters input]
        if {$input != ""} {
            lappend data "array set wb_parameter_input {"
            foreach "n v" $input {
                lappend data "    [list $n $v]"
            }
            lappend data "}"
        }
        set output [ic_wb2_get_parameters output]
        if {$output != ""} {
            lappend data "array set wb_parameter_output {"
            foreach "n v" $output {
                lappend data "    [list $n $v]"
            }
            lappend data "}"
        }
        set user_defined [ic_wb2_get_parameters user_defined]
        if {$user_defined != ""} {
            lappend data "array set wb_parameter_user_defined {"
            foreach "n v" $user_defined {
                lappend data "    [list $n $v]"
            }
            lappend data "}"
        }
        set wboptions [ic_wb2_get_parameters options]
        if {$wboptions != ""} {
            lappend data "array set wb_parameter_options {"
            foreach "n v" $wboptions {
                lappend data "    [list $n $v]"
            }
            lappend data "}"
        }
        if ![info exists AppName] {
            set AppName [aie_get_global AppName]
        }
    }
    ic_save_project_file $file_name(settings) $data $AppName
}

proc save_project_file {pdir} {
    global file_name saved_params data_available
    global program_path AppName env

	set file_name(settings) $pdir
    set pref [file dirname $file_name(settings)]/
    mess "Saving project settings to \"$file_name(settings)\"\n"
    set data ""
    
    if [ic_wb2_is_running] {
        foreach p [array names file_name] {    
            set n [aie_get_global file_name $p]
            if {$n != "___NA___"} {
                set file_name($p) $n
            }
        }
    }
    foreach p $saved_params {
	    global $p
	    if {$p == "file_name"} {
	        lappend data "array set $p {"
	        foreach n [array names $p] {
		        set v [set ${p}($n)]
		        if [string match ${pref}?* $v] {
		            set v [string range $v [string length $pref] end]
		        }
		        lappend data "    [list $n $v]"
	        }
	        lappend data "}"    
	    } elseif {[array names $p] == ""} {
	        if ![info exists $p] { continue }
	        set v [project_settings_make_relative $pdir $p [set $p] 1]
	        lappend data [list set $p $v]
	    } else {
	        if ![info exists $p] { continue }
	        lappend data "array set $p {"
	        foreach n [array names $p] {
		        set v [project_settings_make_relative $pdir ${p}($n) [set ${p}($n)] 1]
		        lappend data "    [list $n $v]"
	        }
	        lappend data "}"
	    }
    }
    if [info exists program_path] {
	    lappend data "array set program_path {"
	    foreach p [array names program_path] {
	        set pp [string trim $program_path($p)]
	        if {$pp != ""} {
		        lappend data "    [list $p $pp]"
	        }
	    }
	    lappend data "}"
    }
    return $data
}

proc project_settings_load {file} {
    global file_name options solution program_path project_file_names
    global use_xwindows
    
    set ux $use_xwindows  ;# save since we don't want to reset this
    
    if [file exists $file] {
	mess "Loading project settings from $file\n"
	if [catch {uplevel \#0 [list source $file]} res ] {
	    mess "error in project_settings\n" red
	    mess "$res\n" red
	}
    }
    foreach v [array names project_file_names] {
	global [lindex [split $v \(] 0]
	set $v [project_settings_make_relative $file_name(project_dir) $v [set $v] 0]
	
    }
    if {$solution(solver) != ""} {
	mess "Current solver is $solution(solver)\n"
    }
    if [info exists program_path] {
	mess "[llength [array names program_path]] non-default program paths\n"
    }
    
    after_set_options
    
    set use_xwindows $ux
}

proc project_settings_make_relative {pdir varname val rel} {
    global project_file_names
    
    if {$pdir == ""} { return $val }
    if ![info exists project_file_names($varname)] { return $val }
    
    if $rel {
	set p2 $pdir
	if {[file pathtype $pdir] == "relative"} { set p2 [pwd]/$p2 }
	regsub -all {/\./} $p2 / p2
	
	set first [string range $val 0 [expr [string length $pdir] - 1]]
	set first2 [string range $val 0 [expr [string length $p2] - 1]]
	
	if ![string compare $first $pdir] {
	    set nval [string range $val [expr [string length $first] + 1] end]
	} elseif ![string compare $first2 $p2] {
	    set nval [string range $val [expr [string length $first2] + 1] end]
	} else {
	    return $val
	}
	
    } else {
	if {($val == "") || ([file pathtype $val] != "relative")} { return $val }
	if [string match */* $val] {
	    set nval $pdir/$val
	} else {
	    set nval $pdir/$project_file_names($varname)/$val
	}
    }
    #puts "$val -> $nval"
    return $nval
}

proc project_settings_save_defaults {} {
    global saved_params saved_params_reset_cmd saved_params_no_reset_defaults
    
    set saved_params_reset_cmd ""
    foreach p $saved_params {
	if [info exists saved_params_no_reset_defaults($p)] continue
	global $p
	if {[array names $p] == ""} {
	    append saved_params_reset_cmd "[list set $p [set $p]] ; "
	} else {
	    append saved_params_reset_cmd "array set $p { "
	    foreach n [array names $p] {
		append saved_params_reset_cmd "[list $n [set ${p}($n)]] "
	    }
	    append saved_params_reset_cmd "} ; "
	}
    }
}

proc project_settings_reset_defaults {} {    
    global program_path saved_params_reset_cmd
    global tdv_default
    uplevel \#0 $saved_params_reset_cmd
    catch {unset program_path}
    tdv_reset_bindings $tdv_default 1
}

proc reset_options {} {
    global use_xwindows program_path
    
    set ux $use_xwindows  ;# save since we don't want to reset this
    foreach file {.icemcfd_options .med_options} {
	set optf [home_directory]/$file
	if [file exists $optf] {
	    if [catch {uplevel \#0 [list source $optf]} res] {
		mess "$res\n" red
	    }
	    break
	}
    }
    set use_xwindows $ux
    
    foreach file {.icemcfd_paths} {
	set optf [home_directory]/$file
	if [file exists $optf] {
	    catch {unset program_path}
	    if [catch {uplevel \#0 [list source $optf]} res] {
		mess "$res\n" red
	    }
	    break
	}
    }
    file_update_paths
    ##    reset_project_file_names
}

proc options_solution_select {} {
    global solution solver_info options
        
    if ![info exists solver_info] {
        get_solver_info
    }
    set solvers [lsort [array names solver_info]]  
    set ch [choose_one "Select a solver." $solvers $solution(solver)]
    if {$ch == ""} return
    if {$ch == "Generic"} {
	mess "Current solver is undefined (generic)\n"
	return
    }    
    if ![info exists solver_info($ch)] {
	mess "Unknown solver $ch in options_solution_select\n" red
	return
    }
    set si $solver_info($ch)
    set sol [lindex $si 1]
    set tdir [lindex $si 4]    
    if ![file exists $tdir/$sol.tcl] {
	if [file exists ./$sol.tcl] {
	    set tdir .
	} else {
	    error "GUI file for $sol doesn't exist in either . or $tdir"
	}
    }
    set options(default_solver) $ch
}
