
# File-related commands.
#
# Copyright (c) 1993, 1994 Pacific Marketing and Consulting
# Author:  Wayne A. Christopher, wayne@pmac.com
#          Pacific Marketing and Consulting, Berkeley, California
#

proc options_edit_auto {} {
    global tdv_default app_name
    
    set pages ""
    
    set options ""
    lappend options [list Display options_edit_display_setup \
			 "" \
			 options_edit_display_save \
			 options_edit_auto_reset \
			 configure]
    lappend options [list Editing options_edit_editing_setup \
			 "" \
			 options_edit_editing_save \
			 "" \
			 configure]
    lappend options [list Printing options_edit_printing_setup \
			 "" \
			 "" \
			 "" \
			 print_icon]
    lappend options [list Misc options_edit_misc_setup \
			 "" \
			 "" \
			 "" \
			 configure]
    if {$app_name != "emax"} {
	lappend options [list Libraries "file_libraries_setup_form 1" \
			   file_libraries_init_form \
			   file_libraries_edit_accept \
			   "" \
			   icepak_libraries]
    }
    lappend options [list "Object types" options_edit_auto_graphical_setup \
		       "" \
		       options_edit_auto_graphical_save \
		       options_edit_auto_reset_graph \
		       styles]
    if {$app_name != "iceboard"} {
	lappend options [list Interaction options_edit_auto_interactive_setup \
			     "" \
			     "" \
			     "" \
			     interact_edit]
    }
    lappend options [list "Mouse buttons" tdv_edit_bindings_form \
		       "" \
		       "tdv_edit_bindings_done $tdv_default" \
		       {tdv_reset_bindings "" 0} \
		       mouse_bindings]
    
    lappend options [list "Error reporting" options_error_report_setup \
			 "" \
			 "" \
			 "" \
			 configure]
    
    set defaults ""
    lappend defaults [list Meshing options_edit_meshing_setup \
			 "" \
			 "" \
			 "" \
			 icepak_mesh]
    lappend defaults [list Solution options_edit_solve_setup \
			 "" \
			 "" \
			 "" \
			 icepak_solve]
    lappend defaults [list Postprocessing options_edit_post_setup \
			 "" \
			 "" \
			 "" \
			 icepak_post]
    lappend defaults [list Units unit_edit_setup_form \
			  unit_edit_setup_vars \
			  unit_edit_done \
			  "units_convert_all_objects def" \
			  unit_edit]
    
    lappend pages [list Options "" "" "" "" bw_folder $options]
    lappend pages [list Defaults "" "" "" "" bw_folder $defaults]
    
    set mess "Select \"All projects\", to apply options to all projects and defaults to all newly created projects.  \nSelect \"This project\", to apply options to the current project. \nCheck the user preferences button in the Open panel to use these preferences" 
    option_edit .options "Preferences" options_edit_auto_done $pages $mess
}

proc options_edit_auto_reset {} {
    global default_config_param
    
    foreach p [array names default_config_param] {
	global $p
	if [array exists $p] {
	    array set $p $default_config_param($p)
	} else {
	    set $p $default_config_param($p)
	}
    }
    foreach v {choices data default} {
	global unit_$v preset_unit_$v
	array set unit_$v [array get preset_unit_$v]
    }
}

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

proc options_edit_grab_defaults {} {
    global graph_params config_params config_default_params config_params_2
    global default_config_param
    
    foreach p $graph_params {
	global default_graph_$p graph_$p
	if ![info exists graph_$p] continue
	array set default_graph_$p [array get graph_$p]
    }
    
    foreach p [concat $config_params $config_default_params] {
	global job_$p
	if ![info exists job_$p] continue
	set default_config_param(job_$p) [set job_$p]
    }
    
    foreach p $config_params_2 {
	global $p
	if ![info exists $p] continue
	if [array exists $p] {
	    set default_config_param($p) [array get $p]
	} else {
	    set default_config_param($p) [set $p]
	}
    }
}

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

proc options_edit_display_setup {d} {
    global icepak_root app_name
    
    set ice [info exists icepak_root]
    set tdm [expr {$app_name == "tdm"}]
    set emax [expr {$app_name == "emax"}]
    
    if $ice {
	form_frame $d.figs flat 0 {12 0 -cols 3}
	form_label $d.figs.legfigstypel "Color legend data format" {0 0 -cols 2}
	form_radiobutton $d.figs.e "exponential" job_legend_figstype e {1 0}
	form_radiobutton $d.figs.f "float" job_legend_figstype f {1 1}
	form_radiobutton $d.figs.g "general" job_legend_figstype g {1 2}
	
	form_label $d.figs.legfigsl "Numerical display precision" {2 0 -cols 2}
	form_entry $d.figs.legfigs job_legend_sigfigs int {2 2}
    }
    
    form_frame $d.partc flat 0 {13 0 -cols -}
    form_label $d.partc.l "Color objects gray:" {0 0}
    form_radiobutton $d.partc.none "None" shape_color_gray 0 {0 1}
    form_radiobutton $d.partc.uns "Unselected assemblies" shape_color_gray 1 {1 1}
    form_radiobutton $d.partc.all "All" shape_color_gray 2 {2 1}
    
    
    form_label $d.partc.obl "Display object names:" {10 0}
    form_radiobutton $d.partc.ob0 "None" type_visible(names) 0 {10 1}
    form_radiobutton $d.partc.ob2 "Selected object" type_visible(names) 2 {11 1}
    form_radiobutton $d.partc.ob1 "Current assembly" type_visible(names) 1 {12 1}
    
    if !$emax {
	form_frame $d.hdir flat 0 {24 0 -cols - -sticky nw}
	form_label $d.hdir.l "Screen up direction:" {0 0}
	form_radiobutton $d.hdir.y Y job_home_direction -z {0 1}
	form_radiobutton $d.hdir.z Z job_home_direction +y {0 2}
    }
    
    form_frame $d.ms flat 0 {35 0 -cols - -sticky nw}
    form_label $d.ms.l "Display scale:" {0 0}
    form_label $d.ms.xl "X" {0 1}
    form_label $d.ms.yl "Y" {1 1}
    form_label $d.ms.zl "Z" {2 1}
    form_entry $d.ms.x tdv_nonuniform_scale(0) string {0 2}
    form_entry $d.ms.y tdv_nonuniform_scale(1) string {1 2}
    form_entry $d.ms.z tdv_nonuniform_scale(2) string {2 2}
    
    form_button $d.bg "Background color" set_background {50 0}
}

proc options_edit_display_save {d allp} {
    global tdv_home_direction tdv_default
    global job_home_direction 
    
    cmap_update_sigfigs
    
    set tdv_home_direction($tdv_default) $job_home_direction
    tdv_trans_update $tdv_default
    update_type_visible names
}

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

proc options_edit_editing_setup {d} {
    global icepak_root app_name
    global default_families_from_object_names
    
    set ice [info exists icepak_root]
    set tdm [expr {$app_name == "tdm"}]
    set emax [expr {$app_name == "emax"}]
    
    form_frame $d.sort flat 0 {9 0 -cols - -sticky nw}
    form_label $d.sort.dl "Default dimensions:" {2 0}
    form_radiobutton $d.sort.d0 "Start/end" job_diff_flag 0 {2 1}
    form_radiobutton $d.sort.d1 "Start/length" job_diff_flag 1 {2 2}
    
    form_frame $d.scr flat 0 {10 0 -cols -}
    if $ice {
	form_frame $d.key flat 0 {11 0 -cols -}
	form_label $d.key.l "Annotation edit key" {0 0}
	form_radiobutton $d.key.c Control job_ctrlkey Control {0 1}
	form_radiobutton $d.key.s Shift job_ctrlkey Shift {1 1}
	form_radiobutton $d.key.m Meta job_ctrlkey Meta {2 1}
    }
    form_frame $d.vf flat 0 {26 0 -cols -}
    form_label $d.vf.l "Fix values:" {0 0}
    form_radiobutton $d.vf.a "All" job_all_fixvals all {0 1}
    form_radiobutton $d.vf.n "None" job_all_fixvals none {0 2}
    form_radiobutton $d.vf.p "Per-object" job_all_fixvals per {0 3}
    
    if $default_families_from_object_names {
	form_frame $d.ft flat 0 {36 0 -cols - -sticky nw}
	form_label $d.ft.wfl "Default families:" {0 0}
	form_radiobutton $d.ft.wf "Full name" family_name_type full {0 1}
	form_radiobutton $d.ft.wn "First chars" family_name_type nchars {1 1}
	form_radiobutton $d.ft.wa "Alpha prefix" family_name_type alpha {2 1}
	form_entry $d.ft.wnn family_name_nchars int {1 2} \
	    {{family_name_type nchars}}
	form_checkbutton $d.ft.sep "Separate families for surfs, curves, etc" \
	    family_name_sep_dims {3 0 -cols 3}
    }
}

proc options_edit_editing_save {d allp} {
    graph_set_ctrlkey
}

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

proc options_edit_meshing_setup {d} {
    global icepak_root app_name
    
    set ice [info exists icepak_root]
    set tdm [expr {$app_name == "tdm"}]
    set emax [expr {$app_name == "emax"}]
    
    grid_mesh_type_form $d {0 0} {0 1}
    
    if !$tdm {
	form_label $d.ttl "Minimum object separation" {5 0}
	form_label $d.ttxl "X" {5 1}
	form_label $d.ttyl "Y" {6 1}
	form_label $d.ttzl "Z" {7 1}
	
	model_unitentry $d.ttx grid_sep_x length {5 2} "" 8 grid_sep_x_units 1 model_size
	model_unitentry $d.tty grid_sep_y length {6 2} "" 8 grid_sep_y_units 1 model_size
	model_unitentry $d.ttz grid_sep_z length {7 2} "" 8 grid_sep_z_units 1 model_size
    }
}

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

proc options_edit_printing_setup {d} {
    form_label $d.pr1l "Print command for text files" {2 0}
    form_label $d.pr2l "Print command for PS files" {3 0}
    
    form_entry $d.pr1 job_printcmd_text string {2 1}
    form_entry $d.pr2 job_printcmd_ps string {3 1}
}

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

proc options_error_report_setup {d} {
    form_label $d.namel "Your name" {0 0}
    form_label $d.emaill "Your email address" {1 0}
    form_label $d.proxyl "HTTP proxy host (if any)" {2 0}
    form_label $d.proxyportl "HTTP proxy port" {3 0}
    
    form_entry $d.name error_report(name) string {0 1}
    form_entry $d.email error_report(email) string {1 1}
    form_entry $d.proxy error_report(proxy) string {2 1}
    form_entry $d.proxyport error_report(proxyport) string {3 1}
}

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

proc options_edit_solve_setup {d} {
    uns_solve_advanced $d
}

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

proc options_edit_post_setup {d} {
    global icepak_root app_name
    
    set ice [info exists icepak_root]
    set tdm [expr {$app_name == "tdm"}]
    set emax [expr {$app_name == "emax"}]
    
    if $ice {
	form_label $d.ptl "Post-processing tolerance" {6 0}
	form_label $d.prtcl "Surface probe color" {7 0}
	
	form_entry $d.pt job_ptol float {6 1}
	futil_colorentry $d.prtc graph_color(surface_probe) {7 1}
    }
    if $emax {
	form_checkbutton $d.defhost "Use local host for post-processing" \
	    emax_local_visual3 {37 0 -cols -}
    }
}

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

proc options_edit_misc_setup {d} {
    form_label $d.xtoptsl "Xterm options" {2 0}
    form_entry $d.xtopts job_xterm_opts string {2 1}
    form_label $d.bdl "Bubble help delay (ms)" {3 0}
    form_entry $d.bd bubble_delay int {3 1}
}

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

proc options_edit_auto_graphical_setup {d} {
    global tdv_default tdv_viewer
    global graph_types graph_color graph_width graph_shadepolicy type_visible
    global graph_fancy graph_font grid_display object_type_title
    global type_name icepak_root background_color
    
    set ice [info exists icepak_root]
    
    form_label $d.typel "Objects" {1 1}
    form_label $d.colorl "Color" {1 3}
    form_label $d.widthl "Width" {1 5}
    form_label $d.shadel "Shading" {1 7 -cols 2}
    if $ice {
	form_label $d.fancyl "Decoration" {1 10}
	form_label $d.fontl "Font" {1 12}
	form_label $d.visl "Visible" {1 14}
	foreach c {0 2 4 6 9 11 13 15} { form_colwidth $d $c 6 }
	foreach c {1 3 5 7 10 12 14} {
	    form_underline $d.under$c "2 [expr {($c == 7) ? "$c -cols 2" : $c}]"
	}
    } else {
	foreach c {0 2 4 6 9} { form_colwidth $d $c 6 }
	foreach c {1 3 5 7} {
	    form_underline $d.under$c "2 [expr {($c == 7) ? "$c -cols 2" : $c}]"
	}
    }
    form_rowheight $d 3 6
    
    if $ice {
	set shades "list view wire flat gouraud"
    } else {
	set shades "list view wire solid hidden"
    }
    set r 4
    foreach type $graph_types {
	set tn $type
	if [info exists object_type_title($tn)] {
	    set tn $object_type_title($tn)
	}
	if {$tn == "vent"} {continue}
	form_label $d.name$r $tn "$r 1 -pady 0" "" 0
	
	if [info exists graph_color($type)] {
	    futil_colorentry $d.color$r graph_color($type) \
		"$r 3 -pady 0"
	}
	if {$graph_width($type) != ""} {
	    form_entry $d.width$r graph_width($type) int_blank "$r 5 -pady 0"
	    $d.width$r configure -width 4
	}
	if {$graph_shadepolicy($type) != ""} {
	    set e1 [futil_menuentry $d.shade$r graph_shadepolicy($type) \
			$shades "$r 7 -pady 0"]
	    $e1 configure -width 5
	}
	if $ice {
	    if {$graph_fancy($type) != ""} {
		form_checkbutton $d.fancy$r "" graph_fancy($type) \
		    "$r 10 -sticky ew -pady 0"
	    }
	    if {$graph_font($type) != ""} {
		form_entry $d.font$r graph_font($type) string \
		    "$r 12 -sticky ew -pady 0"
	    }
	    if {[info exists type_visible($type)]} { 
		if {$type_visible($type) != ""} {
		    form_checkbutton $d.visi$r "" type_visible($type) \
			"$r 14 -sticky ew -pady 0"
		}
	    }
	}
	foreach b "name$r color$r width$r shade$r fancy$r font$r" {
	    catch {$d.$b configure -pady 0}
	}
	incr r
    }
}

proc options_edit_auto_graphical_save {d allp} {
    global tdv_default
    
    model_graph_options_changed
    
    grid_update_images 0 0
    
    tdv_toggle_title $tdv_default
    tdv_toggle_date $tdv_default
}

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

proc options_edit_auto_interactive_setup {d} {
    global type_name type_name_button tdv_nonuniform_scale
    
    set dn domain
    if [info exists type_name(domain)] { set dn $type_name(domain) }
    if [info exists type_name_button(domain)] { set dn $type_name_button(domain) }
    
    form_frame $d.ma flat 0 {0 0 -cols -}
    form_label $d.ma.alab "Motion allowed in direction: " {0 0}
    form_checkbutton $d.ma.ax X shape_x_motion {0 1}
    form_checkbutton $d.ma.ay Y shape_y_motion {0 2}
    form_checkbutton $d.ma.az Z shape_z_motion {0 3}
    
    form_checkbutton $d.ind "Restrict movement to $dn" \
	shape_indomain {1 0 -cols 4}
    form_checkbutton $d.nop "Objects can't penetrate each other" \
	shape_nopenetrate {2 0 -cols 4}
    form_checkbutton $d.ent "Move object also moves group" \
	shape_movegroup {3 0 -cols 4}
    form_checkbutton $d.snap "Move object snaps to other objects" \
	shape_snap_objects {4 0 -cols 4}
    
    form_frame $d.snp flat 0 {5 0 -cols -}
    form_label $d.snp.l "Snap Tolerance" {5 0 -cols 2}
    form_entry $d.snp.e shape_snap_tol integer {5 2}
    form_label $d.snp.p "pixels" {5 3}
    
    form_frame $d.g flat 0 {10 0 -cols -}
    form_checkbutton $d.g.xo "X grid" shape_x_grid_on {10 0}
    form_checkbutton $d.g.yo "Y grid" shape_y_grid_on {11 0}
    form_checkbutton $d.g.zo "Z grid" shape_z_grid_on {12 0}
    form_entry $d.g.x shape_x_grid float {10 1 -cols 2}
    form_entry $d.g.y shape_y_grid float {11 1 -cols 2}
    form_entry $d.g.z shape_z_grid float {12 1 -cols 2}
    
    form_frame $d.ns flat 0 {20 0 -cols -}
    form_label $d.ns.l "New object size factor" {20 0 -cols 2}
    form_entry $d.ns.e new_shape_factor float {20 2}
    
    foreach ul {10 11 12} {
	form_label_variable $d.g.un$ul unit_default(length) "$ul 3"
    }
}

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

proc options_edit_auto_reset_graph {} {
    global graph_params graph_types
    
    foreach p $graph_params {
	global default_graph_$p graph_$p
	array set graph_$p [array get default_graph_$p]
    }
}

proc unused_options_docolor {but type} {
    global graph_color
    
    if [winfo exists .color_edit] {
	mess "Please finish editing the previous color.\n" red
	return
    }
    set color [edit_one_color "Define a new color for $type objects.\n" \
	       $graph_color($type)]
    set graph_color($type) $color
    $but configure -background $color -activebackground $color
}

proc options_edit_auto_done {d allprojects} {
    global tdv_default icepak_root
    global job_listsort job_listsort_save tdv_home_direction job_home_direction
    global global_pref_file global_def_file config_params config_default_params
    global graph_types library_path error_report
    global unit_default unit_choices unit_data post_units bubble_delay
    global tdv_mouse_bindings job_legend_sigfigs job_legend_figstype
    global tdv_nonuniform_scale grid_mesher_params grid_type type_visible
    
    if $allprojects {
	if [catch {open $global_pref_file w} fp] {
	    user_error $fp
	    return
	}
	foreach p $config_params {
	    global job_$p
	    puts $fp [list set job_$p [set job_$p]]
	}
	foreach p $graph_types {
	    foreach t {color width shadepolicy fancy font} {
		global graph_$t
		set v [set "graph_${t}($p)"]
		puts $fp [list set graph_${t}($p) $v]
	    }
	}
	foreach u [array names tdv_mouse_bindings] {
	    puts $fp [list set tdv_mouse_bindings($u) $tdv_mouse_bindings($u)]
	}
	foreach u {name email proxy proxyport} {
	    puts $fp [list set error_report($u) $error_report($u)]
	}
	foreach v {x_grid_on y_grid_on z_grid_on x_grid y_grid z_grid} {
	    global shape_$v
	    puts $fp [list set shape_$v [set shape_$v]]
	}
	if [info exists tdv_nonuniform_scale] {
	    foreach v {0 1 2} {
		puts $fp [list set tdv_nonuniform_scale($v) $tdv_nonuniform_scale($v)]
	    }
	}
	puts $fp [list set type_visible(names) $type_visible(names)]
	
	puts $fp "set bubble_delay $bubble_delay"
	set libp ""
	set root [string trimright $icepak_root /]/
	set rootl [string length $root]
	foreach lib $library_path {
	    if [string match $root* $lib] {
		set lib \$ICEPAK_ROOT/[string range $lib $rootl end]
		lappend libp $lib
		puts "lib $lib"
	    } else {
		lappend libp $lib
	    }
	}
	puts $fp [list set library_path $libp]
	puts $fp "catch \{tdv_changebg current \{[tdv_getbg current]\}\}"
	close $fp
	mess "Saved user preferences to $global_pref_file\n"
	
	# --------------------------------------------------
	
	if [catch {open $global_def_file w} fp] {
	    user_error $fp
	    return
	}
	foreach p $config_default_params {
	    global job_$p
	    puts $fp [list set job_$p [set job_$p]]
	}
	foreach u [array names unit_default] {
	    puts $fp [list set unit_default($u) $unit_default($u)]
	}
	foreach u [array names unit_choices] {
	    puts $fp [list set unit_choices($u) $unit_choices($u)]
	}
	foreach u [array names unit_data] {
	    puts $fp [list set unit_data($u) $unit_data($u)]
	}
	puts $fp [list set grid_mesher_params(cartesian) \
		      $grid_mesher_params(cartesian)]
	puts $fp [list set grid_type $grid_type]
	
	foreach v {
	    convergence_frequency convergence_check solve_precis
	} {
	    global $v
	    puts $fp [list set $v [set $v]]
	}
	
	foreach v {
	    uns_disc_order uns_under_rel uns_solver_type uns_term_crit uns_resid_tol
	} {
	    global $v
	    puts $fp [list array set $v [array get $v]]
	}
	
	close $fp
	mess "Saved defaults for new projects to $global_def_file\n"
    }
    db_dirty model
}
