#!/bin/sh
#ChangeVariables  -*- TCL -*- Created: ramsan Jan-1997, Modified: ramsan Jan-1997


#----------------------ChangeVariables ALONE---------------------------
# the next line restarts using wish \
	exec wish4.2 "$0" "$@"

set ChangeVariablesRunningAlone 0
# catch {
#     if {$argv0 == [info script] } {
#         set ChangeVariablesRunningAlone 1
#         set tcl_interactive 0
#         set tk_strictMotif 1
	
#         if { [catch { set GIDDEFAULT $env(GIDDEFAULT)}]!=1 } {
#             set GIDDEFAULTTCL $GIDDEFAULT
#             catch { set GIDDEFAULTTCL $env(GIDDEFAULTTCL) }
#             set auto_path "$GIDDEFAULTTCL"
#             source $GIDDEFAULTTCL/init.tcl
#             source $GIDDEFAULTTCL/tk.tcl
#         }
#         wm withdraw .
#     }
# }

#  LOOK AT THE END OF THE FILE !!!
#-------------------------------------------------------------------

proc processBgColor {color fm} {
    
    set colorStr " "
    
    if {$fm == "gidToTcl"} {
	# Format RRR#GGG#BBB to #2x2x2x
	scan [string range $color 0 end] %3i%c%3i%c%3i col1 sp1 col2 sp2 col3
	set colorStr [format %c%2x%2x%2x $sp1 $col1 $col2 $col3]

    } elseif {$fm == "tclToGid"} {
	# Format #2x2x2x to RRR#GGG#BBB
	puts $color
	scan [string range $color 0 end] %c%2x%2x%2x sp1 col1 col2 col3
	
	puts $col1
	puts $col2 
	puts $col3
	set colorStr [format %03i%c%03i%c%03i $col1 $sp1 $col2 $sp1 $col3]
    }
    
    return $colorStr
}


proc DefineFuenteSiNoEsta { nombre_fnt} {
    if { [ lsearch [ font names] $nombre_fnt] == -1 } {
	#default: -adobe-helvetica-medium-r-normal--12-120-75-75-p-67-iso8859-1
	#Helvetica 12 normal roman
	font create $nombre_fnt
	set com_font [ .central.s info variable $nombre_fnt]
	# WarnWinText "$com_font"
	# WarnWinText "font conf CommentsFont [ font actual $com_font]"
	eval [ concat font conf $nombre_fnt [ font actual $com_font]]
	# ajustamos tamaño
	if { [ regexp {^-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-([0-9]+)-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*$} $com_font dum fnt_size]} {
	    font conf $nombre_fnt -size [ font metrics $com_font -ascent]
	}
    }
}

proc AcceptGiDFont { nombre_fnt} {
    global ChangeVarInfo GidPriv tcl_platform GIDDEFAULTTCL
    
    if { $tcl_platform(platform) != "windows" } {
	set wght [ font actual $nombre_fnt -weight]
	if { "$wght" == "normal"} { set wght medium}
	set slant [ string index [ font actual $nombre_fnt -slant] 0]
	set fnt_family [ font actual $nombre_fnt -family]
	#set fnt_size [ font actual $nombre_fnt -size]
	set fnt_size [ font metrics $nombre_fnt -ascent]
	set com_fnt "-*-$fnt_family-$wght-$slant-*-*-$fnt_size-*-*-*-*-*-*-*"
    } else {
	set com_fnt ""
	lappend com_fnt [ font actual $nombre_fnt -family]
	lappend com_fnt [ font actual $nombre_fnt -size]
	lappend com_fnt [ font actual $nombre_fnt -weight] 
	lappend com_fnt [ font actual $nombre_fnt -slant]
    }
    
    .central.s process escape escape escape escape utilities variables $nombre_fnt \"$com_fnt\"\
	    escape escape
}

proc FillPrefValuesFromGid {} {
    global ChangeVarInfo GidPriv

    if { $ChangeVarInfo(type) == "GENERAL" } {

	set ChangeVarInfo(Dialogbrowser) [.central.s info variable DialogWrowser]

	set ChangeVarInfo(SplashWindow) [.central.s info variable SplashWindow]

	set ChangeVarInfo(ViewMenu) $GidPriv(haveviewmenu)

	set input [.central.s info variable CreateAlwaysNewPoint]
	if { $input == 1 } { 
	    set ChangeVarInfo(Createnewpoint) Always
	} elseif { $input == 0 } { 
	    set ChangeVarInfo(Createnewpoint) Ask 
	} elseif { $input == -2 } { 
	    set ChangeVarInfo(Createnewpoint) Never
	} else {set ChangeVarInfo(Createnewpoint) No }

	set ChangeVarInfo(Displaycoordinates) \
		[.central.s info variable DisplayCoordinates]

	set input [.central.s info variable AutomaticRedraw]
	if { $input == 0 } {
	    set ChangeVarInfo(AutomaticRedraw) "None"
	} elseif { $input == 1 } { 
	    set ChangeVarInfo(AutomaticRedraw) "All windows" 
	} elseif { $input == 2 } {
	    set ChangeVarInfo(AutomaticRedraw) "Current window"
	} else { set ChangeVarInfo(AutomaticRedraw) "Current window" }
	
	set input [.central.s info variable UseMoreWindows]
	if { $input == 1 } { 
	    set ChangeVarInfo(Usemorewindows) yes 
	} elseif { $input == 2 } {
	    set ChangeVarInfo(Usemorewindows) beginner
	} else { set ChangeVarInfo(Usemorewindows) no }

	set ChangeVarInfo(Sound) [.central.s info variable Sound]
	set ChangeVarInfo(SmallWinSelecting) [.central.s info variable SmallWinSelecting]

	set input [.central.s info variable BatchFileToWrite]
	if { $input == "UNKNOWN" } {
	    set ChangeVarInfo(WriteBatchCheck) 0
	    set ChangeVarInfo(WriteBatchFile) ""
	} else {
	    set ChangeVarInfo(WriteBatchCheck) 1
	    set ChangeVarInfo(WriteBatchFile) [lindex $input 0]
	}
	set ChangeVarInfo(WriteRotationsInBatch) \
		[.central.s info variable WriteRotationsInBatch]
	
	set input [.central.s info variable BackUpMinutes]
	set ChangeVarInfo(BackupTime) $input
	if { $input == 0 } {
	    set ChangeVarInfo(BackupCheck) 0
	} else {
	    set ChangeVarInfo(BackupCheck) 1
	}

	set ChangeVarInfo(Language) [::GidUtils::GetCurrentLanguage]

    } elseif { $ChangeVarInfo(type) == "GRAPH" } {

	set ChangeVarInfo(Lightsmoothedelements) \
		[.central.s info variable LightSmoothedElems]

	set ChangeVarInfo(AngleSmoothed) [format %g \
		[expr 57.295779513082323*acos([.central.s info variable \
		CosSmoothedElems])]]

	set input [.central.s info variable DrawSurfaceMode]
	switch $input {
	    0 { set ChangeVarInfo(DrawSurfaceMode) "None" }
	    1 { set ChangeVarInfo(DrawSurfaceMode) "Boundary lines" }
	    2 { set ChangeVarInfo(DrawSurfaceMode) "Isoparametric lines" }
	    3 { set ChangeVarInfo(DrawSurfaceMode) "Both modes" }
	}

	set ChangeVarInfo(DrawQuadratic) [.central.s info variable DrawQuadraticElements]
	
	set ChangeVarInfo(CurvePrecision) \
		[.central.s info variable CurvePrecision]

	set ChangeVarInfo(SurfacePrecision) \
		[.central.s info variable SurfacePrecision]

	foreach i "BackgroundColor ColorPoints ColorLines ColorPolyLines \
		ColorSurfaces ColorSurfsIsoparametric ColorVolumes \
		ColorNodes ColorElements" {
	    set bgColor [.central.s info variable $i]
	    set col1 [string range $bgColor 0 2]
	    set col2 [string range $bgColor 4 6]
	    set col3 [string range $bgColor 8 10]
	    scan $col1 %i icol1
	    scan $col2 %i icol2
	    scan $col3 %i icol3
	    set bgXColor [format %02x%02x%02x $icol1 $icol2 $icol3]
	    set ChangeVarInfo($i) "#$bgXColor"
	}              

	set ChangeVarInfo(GUIAppearance) $GidPriv(GUIAppearance)

	
	set ChangeVarInfo(FastRotation) \
		[.central.s info variable FastRotation(Active)]
	if { $ChangeVarInfo(FastRotation) == "" } { set ChangeVarInfo(FastRotation) 0 }

	set ChangeVarInfo(FRPoints) \
		[.central.s info variable FastRotation(Points)]
	if { $ChangeVarInfo(FRPoints) == "" } { set ChangeVarInfo(FRPoints) 1 }
	
	set ChangeVarInfo(FRLines) \
		[.central.s info variable FastRotation(Lines)]
	if { $ChangeVarInfo(FRLines) == "" } { set ChangeVarInfo(FRLines) 1 }
	
	set ChangeVarInfo(FRSurfaces) \
		[.central.s info variable FastRotation(Surfaces)]
	if { $ChangeVarInfo(FRSurfaces) == "" } { set ChangeVarInfo(FRSurfaces) 1 }
	
	set ChangeVarInfo(FRVolumes) \
		[.central.s info variable FastRotation(Volumes)]
	if { $ChangeVarInfo(FRVolumes) == "" } { set ChangeVarInfo(FRVolumes) 1 }
	set ChangeVarInfo(AlwaysGeom) \
		[.central.s info variable FastRotation(UseAlwaysGeom)]
	
	set ChangeVarInfo(DrawGraphicObjects) \
		[.central.s info variable FastRotation(DrawGraphicObjects)]
	
	set ChangeVarInfo(FRCurvePrecision) \
		[.central.s info variable FastRotation(CurvePrecision)]
	if { $ChangeVarInfo(FRCurvePrecision) == "" } {
	    set ChangeVarInfo(FRCurvePrecision) 0.8
	}
	
    } elseif { $ChangeVarInfo(type) == "MESHING" } {

	set ChangeVarInfo(SizeTransitions) \
		[.central.s info variable SizeTransitionsFactor]

#         set ChangeVarInfo(VolumeMesher) \
#                 [.central.s info variable VolumeMesher]

	set ChangeVarInfo(MeshUntilEnd) \
		[.central.s info variable MeshUntilEnd]

	set ChangeVarInfo(AllowAutomaticStructured) \
		[.central.s info variable AllowAutomaticStructured]

	set ChangeVarInfo(NoMeshFrozenLayers) \
		[.central.s info variable NoMeshFrozenLayers]

	set ChangeVarInfo(AutomaticCorrectSizes) \
		[.central.s info variable AutomaticCorrectSizes]

	set ChangeVarInfo(SurfaceMesher) \
		[.central.s info variable SurfaceMesher]

	set input [.central.s info variable ForceMeshEntities]
	if { $input & 1 } {
	    set ChangeVarInfo(ForceMeshPoints) 1
	} else {
	    set ChangeVarInfo(ForceMeshPoints) 0
	}
	if { $input & 2 } {
	    set ChangeVarInfo(ForceMeshLines) 1
	} else {
	    set ChangeVarInfo(ForceMeshLines) 0
	}
	if { $input & 4 } {
	    set ChangeVarInfo(ForceMeshSurfaces) 1
	} else {
	    set ChangeVarInfo(ForceMeshSurfaces) 0
	}
	
	set input [.central.s info variable HighQualitySmoothing]
	if { $input == 0 } { 
	    set ChangeVarInfo(HighQualitySmoothing) Normal
	} elseif { $input == 1 } { 
	    set ChangeVarInfo(HighQualitySmoothing) HighAngle
	} elseif { $input == 2 } { 
	    set ChangeVarInfo(HighQualitySmoothing) HighGeom
	} 
	#else {set ChangeVarInfo(HighQualitySmoothing) Normal }
	

    } elseif { $ChangeVarInfo(type) == "IMPORT" } {

	set ChangeVarInfo(AutoCollapseAfterImport) \
		[.central.s info variable AutoCollapseAfterImport]

	set ChangeVarInfo(ImportTolerance) \
		[.central.s info variable ImportTolerance]

	set ChangeVarInfo(CollapseIgnoringLayers) \
		[.central.s info variable CollapseIgnoringLayers]

	set ChangeVarInfo(IGESIgnoreInvisible) \
		[.central.s info variable IGESIgnoreInvisible]

      set ChangeVarInfo(IGESCurveOnSurfaceFrom3D) \
		[.central.s info variable IGESCurveOnSurfaceFrom3D]

	set ChangeVarInfo(AutoImportTolerance) \
		[.central.s info variable AutoImportTolerance]

    } elseif { $ChangeVarInfo(type) == "FONTS" } {

	# the Comments font
	DefineFuenteSiNoEsta CommentsFont
	DefineFuenteSiNoEsta LabelsFont

	if { ![info exists ChangeVarInfo(fontfamilies)] } {
	    set ChangeVarInfo(fontfamilies) [lsort [font families]]
	}
	foreach i "NormalFont FixedFont BigFont CommentsFont LabelsFont" {
	    if { [$ChangeVarInfo($i,combo) listbox size] == 0 } {
		foreach j $ChangeVarInfo(fontfamilies) {
		    if { $i == "FixedFont" && \
		            ![font metrics [list $j] -fixed] } { continue }
		    $ChangeVarInfo($i,combo) add $j
		}
	    }
	    set ChangeVarInfo($i,family) [font conf $i -family]
	    set ChangeVarInfo($i,size) [font conf $i -size]
	    set ChangeVarInfo($i,weight) [font conf $i -weight]
	    set ChangeVarInfo($i,slant) [font conf $i -slant]
	}


    } elseif { $ChangeVarInfo(type) == "FORMAT" } {

	foreach fmt "PostLegendFormat PostScalarFormat PostVectorFormat PostMatrixFormat" {
	    set ChangeVarInfo($fmt) [ .central.s info variable $fmt]
	}

	set ChangeVarInfo(RealFormat,Default) $GidPriv(RealFormat,Default)

    } elseif { $ChangeVarInfo(type) == "POSTPROCESS" } {

	#foreach fmt "PostLegendFormat PostScalarFormat PostVectorFormat PostMatrixFormat" {
	#    set ChangeVarInfo($fmt) [ .central.s info variable $fmt]
	#}
	#
	#set GidPriv(RealFormat,Default) $GidPriv(RealFormat,Default)

    }
}


proc FillValuesDefault {} {
    global ChangeVarInfo

    if { $ChangeVarInfo(type) == "GENERAL" } {
	set ChangeVarInfo(Dialogbrowser) 1
	set ChangeVarInfo(SplashWindow) 1
	set ChangeVarInfo(ViewMenu) 1
	set ChangeVarInfo(Createnewpoint) Ask
	set ChangeVarInfo(Displaycoordinates) 1
	set ChangeVarInfo(Automaticredraw) "All windows"
	set ChangeVarInfo(Usemorewindows) yes
	set ChangeVarInfo(Sound) 0
	set ChangeVarInfo(SmallWinSelecting) 0
	set ChangeVarInfo(WriteBatchCheck) 0
	set ChangeVarInfo(WriteBatchFile) ""                        
	set ChangeVarInfo(BackupTime) 20
	set ChangeVarInfo(BackupCheck) 1
	set ChangeVarInfo(WriteRotationsInBatch) 0
	#is best to find the local language, not fix english
	set ChangeVarInfo(Language) "en"
    } elseif { $ChangeVarInfo(type) == "GRAPH" } {
	set ChangeVarInfo(Lightsmoothedelements) 1
	set ChangeVarInfo(AngleSmoothed) 40
	set ChangeVarInfo(DrawSurfaceMode) "Boundary lines"
	set ChangeVarInfo(DrawQuadratic) No
	set ChangeVarInfo(NLightingSubdiv) 8
	set ChangeVarInfo(CurvePrecision) 0.8
	set ChangeVarInfo(SurfacePrecision) 0.6
	set ChangeVarInfo(FastRotation) 0
	
	set ChangeVarInfo(FRPoints) 1
	set ChangeVarInfo(FRLines) 1
	set ChangeVarInfo(FRSurfaces) 1 
	set ChangeVarInfo(FRVolumes) 1
	set ChangeVarInfo(AlwaysGeom) 0
	set ChangeVarInfo(FRCurvePrecision) 0.8
	set ChangeVarInfo(DrawGraphicObjects) 1
	#mpc 05/08/98
	set bgColor [.central.s info RGBDefaultBackground]
	set col1 [string range $bgColor 0 2]
	set col2 [string range $bgColor 4 6]
	set col3 [string range $bgColor 8 10]
	scan $col1 %i icol1
	scan $col2 %i icol2
	scan $col3 %i icol3
	set bgXColor [format %02x%02x%02x $icol1 $icol2 $icol3]
	set ChangeVarInfo(BackgroundColor) "#$bgXColor"
	set ChangeVarInfo(ColorPoints) "#000000"
	set ChangeVarInfo(ColorLines) "#0000ff"
	set ChangeVarInfo(ColorPolyLines) "#5f8f2f"
	set ChangeVarInfo(ColorSurfaces) "#ff00ff"
	set ChangeVarInfo(ColorSurfsIsoparametric) "#ffff00"
	set ChangeVarInfo(ColorVolumes) "#00ffff"
	# set ChangeVarInfo(ColorVolumes) "#5050fb"
	set ChangeVarInfo(ColorNodes) "#000000"
	set ChangeVarInfo(ColorElements) "#5f8f2f"
	set ChangeVarInfo(GUIAppearance) 1
	# 0 normal (color standard windows)
	# 1 color algo más apagado 
      # mpc
    } elseif { $ChangeVarInfo(type) == "MESHING" } {
	
	set ChangeVarInfo(SizeTransitions) 0.6
	set ChangeVarInfo(SurfaceMesher) 0
	#  set ChangeVarInfo(VolumeMesher) 0
	set ChangeVarInfo(MeshUntilEnd) 1
	set ChangeVarInfo(AllowAutomaticStructured) 0
	set ChangeVarInfo(NoMeshFrozenLayers) 1
	set ChangeVarInfo(AutomaticCorrectSizes) 1
	set ChangeVarInfo(ForceMeshPoints) 0
	set ChangeVarInfo(ForceMeshLines) 0
	set ChangeVarInfo(ForceMeshSurfaces) 0
	set ChangeVarInfo(HighQualitySmoothing) Normal
	
    } elseif { $ChangeVarInfo(type) == "IMPORT" } {
	
	set ChangeVarInfo(AutoCollapseAfterImport) 1
	set ChangeVarInfo(ImportTolerance) 1.0e-4
	set ChangeVarInfo(CollapseIgnoringLayers) 1
	set ChangeVarInfo(IGESIgnoreInvisible) 1
	set ChangeVarInfo(IGESCurveOnSurfaceFrom3D) 1
	set ChangeVarInfo(AutoImportTolerance) 1

    }  elseif { $ChangeVarInfo(type) == "FONTS" } {
	font delete NormalFont FixedFont BoldFont BigFont SmallFont
	SetFontsDefault        

	# the Comments font
	set com_font [ .central.s info variable CommentsFont]
	if { [ lsearch [ font names] CommentsFont] == -1 } {
	    font create CommentsFont
	}
	eval [ concat font conf CommentsFont [ font actual $com_font]]

	set com_font [ .central.s info variable LabelsFont]
	if { [ lsearch [ font names] LabelsFont] == -1 } {
	    font create LabelsFont
	}
	eval [ concat font conf LabelsFont [ font actual $com_font]]

	foreach i "NormalFont FixedFont BigFont CommentsFont LabelsFont" {
	    set ChangeVarInfo($i,family) [font conf $i -family]
	    set ChangeVarInfo($i,size) [font conf $i -size]
	    set ChangeVarInfo($i,weight) [font conf $i -weight]
	    set ChangeVarInfo($i,slant) [font conf $i -slant]
	}
    } elseif { $ChangeVarInfo(type) == "FORMAT" } {
	set ChangeVarInfo(PostLegendFormat) "%.5g"
	set ChangeVarInfo(PostScalarFormat) "%.5g"
	set ChangeVarInfo(PostVectorFormat) "%.4g"
	set ChangeVarInfo(PostMatrixFormat) "%.4g"
	set ChangeVarInfo(RealFormat,Default) "%.6g"
    } elseif { $ChangeVarInfo(type) == "FORMAT" } {
	#set postprefs [.central.s info postprocess get results_options]
	#
	## the default values:
	#set postncols [ lindex $postprefs 0]
	#set ChangeVarInfo(NumColors) [ lindex $postncols 0]
	#set ChangeVarInfo(IntvWidth) [ lindex $postprefs 1]
	#set postcontlimits [ lindex $postprefs 2]
	#set ChangeVarInfo(ContourLimMin) [ lindex $postcontlimits 0]
	#set ChangeVarInfo(ContourLimMax) [ lindex $postcontlimits 1]
	#set postvoff [ lindex $postprefs 4]
	#set ChangeVarInfo(VectorOffset) [ lindex $postvoff 0]
	#
	#set borderangles [ join [ .central.s info postprocess get criterio_bordes]]
	#set ChangeVarInfo(BordeAngle) [ lindex $borderangles 0]
	#
	#set postvcols [ lindex $postprefs 6]
	#set ChangeVarInfo(VectNumColors) [ lindex $postvcols 0]
	
    }
}

proc UpdateWidgetsLanguage { } {
    global GidPriv
    .gid.phantom.display3dbutton delete 0 end ;#to re-create the 3rd button menu
    SetAllMenusVariables 
    #CreateTopMenus
    ChangeTopMenusAndWindows
    ChangeVariables
    if { [info exists GidPriv(InitWindowList)] } {
	foreach geomwinname $GidPriv(InitWindowList) {
	    if { [lsearch {PrePostTopMenuWindow} $geomwinname] != "-1" } {
		continue
	    }
	    if { [info exists GidPriv($geomwinname)] && [winfo exists $GidPriv($geomwinname)] } {
		catch { eval $GidPriv($geomwinname,InitComm) }
	    }
	}
    }
}       

proc AcceptValues { w } {
    global ChangeVarInfo GidPriv tcl_platform GIDDEFAULTTCL
    MakeNoise changesareaccepted
    set res ""
    
    if { $ChangeVarInfo(type) == "GENERAL" } {
	if { $ChangeVarInfo(WriteBatchCheck) == 1 && \
		$ChangeVarInfo(WriteBatchFile)  == "" } {
	    tk_dialogRAM $w.tempwin [_ "Error window"] \
		[_ "If batch file is on, one file name must be given"] error 0 [_ "OK"]
	    return
	}

	if { $ChangeVarInfo(ViewMenu) != $GidPriv(haveviewmenu)} {
	    set GidPriv(haveviewmenu) $ChangeVarInfo(ViewMenu)
	    SetAllMenusVariables
	    CreateTopMenus
	    #ChangeTopMenusAndWindows
	}

	#activate trace function
	set ChangeVarInfo(BackupCheck) $ChangeVarInfo(BackupCheck) 

	if { $ChangeVarInfo(Dialogbrowser) != \
		[.central.s info variable DialogWrowser] } {
	    lappend res DialogWrowser $ChangeVarInfo(Dialogbrowser)
	}

	if { $ChangeVarInfo(SplashWindow) != \
		[.central.s info variable SplashWindow] } {
	    lappend res SplashWindow $ChangeVarInfo(SplashWindow)
	}

	switch $ChangeVarInfo(Createnewpoint) {
	    Always {
		set rr 1
	    }
	    Ask {
		set rr 0
	    }
	    Never {
		set rr -2
	    }
	    No { 
		set rr -1
	    }
	    default {
		# problems
		set rr -3 
	    }
	}
	if { $rr != [.central.s info variable CreateAlwaysNewPoint] } {
	    lappend res CreateAlwaysNewPoint $rr
	}
	if { $ChangeVarInfo(Displaycoordinates) != \
		[.central.s info variable DisplayCoordinates] } {
	    lappend res DisplayCoordinates $ChangeVarInfo(Displaycoordinates)
	    if { !$ChangeVarInfo(Displaycoordinates) } {
		pack forget .gid.comm.fcoord
	    } else {
		pack forget .gid.comm.fle
		pack forget .gid.comm.fwarn
		pack .gid.comm.fcoord -fill y -side right
		pack .gid.comm.fle -fill x -side bottom -expand yes -pady 2 -padx 2
		pack .gid.comm.fwarn -fill both -side top -expand yes
	    }
	}
	      
	switch $ChangeVarInfo(Automaticredraw) {
	    "All windows" {
		set rr 1 
	    }
	    "Current window" {
		set rr 2
	    }
	    "None" {
		set rr 0
	    }
	}
	if { $rr != [.central.s info variable AutomaticRedraw] } {
	    lappend res AutomaticRedraw $rr
	}

	switch $ChangeVarInfo(Usemorewindows) {
	    yes {
		set rr 1 
	    }
	    beginner {
		set rr 2
	    }
	    no {
		set rr 0
	    }
	}
	if { $rr != [.central.s info variable UseMoreWindows] } {
	    lappend res UseMoreWindows $rr
	}

	if { $ChangeVarInfo(Sound) != \
		[.central.s info variable Sound] } {
	    lappend res Sound $ChangeVarInfo(Sound)
	}
	if { $ChangeVarInfo(SmallWinSelecting) != \
		[.central.s info variable SmallWinSelecting] } {
	    lappend res SmallWinSelecting $ChangeVarInfo(SmallWinSelecting)
	}

	regsub -all {\\} $ChangeVarInfo(WriteBatchFile) {/} \
		ChangeVarInfo(WriteBatchFile)

	set input [lindex [.central.s info variable BatchFileToWrite] 0]
	if { $input == "UNKNOWN" && $ChangeVarInfo(WriteBatchCheck)} {
	    lappend res BatchFileToWrite "\"$ChangeVarInfo(WriteBatchFile)\""
	} elseif { $input != "UNKNOWN" && !$ChangeVarInfo(WriteBatchCheck) } {
	    lappend res BatchFileToWrite UNKNOWN
	} elseif { $input != "UNKNOWN" && $ChangeVarInfo(WriteBatchCheck) && \
		$input != $ChangeVarInfo(WriteBatchFile) } {
	    lappend res BatchFileToWrite "\"$ChangeVarInfo(WriteBatchFile)\""
	}
     
	set input [.central.s info variable BackUpMinutes]
	if { $input != $ChangeVarInfo(BackupTime) } {
	    lappend res BackUpMinutes $ChangeVarInfo(BackupTime)
	}

	if { $ChangeVarInfo(WriteRotationsInBatch) != \
		[.central.s info variable WriteRotationsInBatch] } {
	    lappend res WriteRotationsInBatch $ChangeVarInfo(WriteRotationsInBatch)
	}
	
	if { $ChangeVarInfo(Language) != [::GidUtils::GetCurrentLanguage] } {
	    #WarnWin [_ "Must restart %s to use the new language" $GidPriv(ProgName)]
	    ::GidUtils::SetCurrentLanguage $ChangeVarInfo(Language) 1      
	}

    }
    if { $ChangeVarInfo(type) == "GRAPH" } {

	if { $ChangeVarInfo(Lightsmoothedelements) != \
		[.central.s info variable LightSmoothedElems] } {
	    lappend res LightSmoothedElems $ChangeVarInfo(Lightsmoothedelements)
	}
	set CosAngleSmoothed [format %g [expr cos($ChangeVarInfo(AngleSmoothed)*\
		0.017453292519943295)]]
	set CosAngleSmoothedS [format %g [.central.s info variable \
		CosSmoothedElems]]
	if { $CosAngleSmoothed != $CosAngleSmoothedS } {
	    lappend res CosSmoothedElems $CosAngleSmoothed
	}
	
	switch $ChangeVarInfo(DrawSurfaceMode) {
	    "None" { set var 0 }
	    "Boundary lines" { set var 1 }
	    "Isoparametric lines" { set var 2 }
	    "Both modes" { set var 3 }
	}
	if { [.central.s info variable DrawSurfaceMode] != $var } {
	    lappend res DrawSurfaceMode $var
	}

	if { "$ChangeVarInfo(DrawQuadratic)" != "[.central.s info variable DrawQuadraticElements]"} {
	    lappend res DrawQuadraticElements $ChangeVarInfo(DrawQuadratic)
	}

	if { $ChangeVarInfo(CurvePrecision) != \
		[.central.s info variable CurvePrecision] } {
	    lappend res CurvePrecision $ChangeVarInfo(CurvePrecision)
	}

	if { $ChangeVarInfo(SurfacePrecision) != \
		[.central.s info variable SurfacePrecision] } {
	    lappend res SurfacePrecision $ChangeVarInfo(SurfacePrecision)
	}


	foreach i "BackgroundColor ColorPoints ColorLines ColorPolyLines \
		ColorSurfaces ColorSurfsIsoparametric ColorVolumes \
		ColorNodes ColorElements" {
	    set bgColor  $ChangeVarInfo($i)
	    scan [string range $bgColor  0 end] %c%2x%2x%2x sp1 col1 col2 col3
	    set bgColor [format %03i%c%03i%c%03i $col1 $sp1 $col2 $sp1 $col3]
	    if { $bgColor != [.central.s info variable $i] } {
		lappend res $i $bgColor
	    }
	}

	if { $ChangeVarInfo(GUIAppearance) != $GidPriv(GUIAppearance) } {
	    if { $ChangeVarInfo(GUIAppearance) == 0 } {    
		WarnWin [_ "Must restart %s to update the change\
		         of GUI appearance to standard color" $GidPriv(ProgName)]
	    } else {
		WarnWin [_ "Must restart %s to update the change\
		        of GUI appearance to attenuated color" $GidPriv(ProgName)]
	    }
	    set GidPriv(GUIAppearance) $ChangeVarInfo(GUIAppearance)   
	}
	
	
	
	if { $ChangeVarInfo(FastRotation) != \
		[.central.s info variable FastRotation(Active)] } {
	    lappend res FastRotation(Active) $ChangeVarInfo(FastRotation)
	}
	if { $ChangeVarInfo(FRPoints) != \
		[.central.s info variable FastRotation(Points)] } {
	    lappend res FastRotation(Points) $ChangeVarInfo(FRPoints)
	}
	if { $ChangeVarInfo(FRLines) != \
		[.central.s info variable FastRotation(Lines)] } {
	    lappend res FastRotation(Lines) $ChangeVarInfo(FRLines)
	}
	if { $ChangeVarInfo(FRSurfaces) != \
		[.central.s info variable FastRotation(Surfaces)] } {
	    lappend res FastRotation(Surfaces) $ChangeVarInfo(FRSurfaces)
	}
	if { $ChangeVarInfo(FRVolumes) != \
		[.central.s info variable FastRotation(Volumes)] } {
	    lappend res FastRotation(Volumes) $ChangeVarInfo(FRVolumes)
	}
	if { [.central.s info variable FastRotation(CurvePrecision)] != "" && \
		$ChangeVarInfo(FRCurvePrecision) != \
		[.central.s info variable FastRotation(CurvePrecision)] } {
	    lappend res FastRotation(CurvePrecision) $ChangeVarInfo(FRCurvePrecision)
	}
	if { $ChangeVarInfo(AlwaysGeom) != \
		[.central.s info variable FastRotation(UseAlwaysGeom)] } {
	    lappend res FastRotation(UseAlwaysGeom) $ChangeVarInfo(AlwaysGeom)
	}
	if { $ChangeVarInfo(DrawGraphicObjects) != \
		[.central.s info variable FastRotation(DrawGraphicObjects)] } {
	    lappend res FastRotation(DrawGraphicObjects) $ChangeVarInfo(DrawGraphicObjects)
	}
    }
    
    if { $res != "" } {
	eval [concat .central.s process escape escape escape escape utilities variables \
		$res escape escape]
	ChangeWarnLineVar [_ "Accepted new %s preferences" $ChangeVarInfo(type)]
    }

    if { $ChangeVarInfo(type) == "MESHING" } {

	if { $ChangeVarInfo(SizeTransitions) != \
		[.central.s info variable SizeTransitionsFactor] } {
	    lappend res SizeTransitionsFactor $ChangeVarInfo(SizeTransitions)
	}

	if { $ChangeVarInfo(SurfaceMesher) != \
		[.central.s info variable SurfaceMesher] } {
	    lappend res SurfaceMesher $ChangeVarInfo(SurfaceMesher)
	}

	#         if { $ChangeVarInfo(VolumeMesher) != \
		#                 [.central.s info variable VolumeMesher] } {
	    #             lappend res VolumeMesher $ChangeVarInfo(VolumeMesher)
	#         }
	
	if { $ChangeVarInfo(MeshUntilEnd) != \
		[.central.s info variable MeshUntilEnd] } {
	    lappend res MeshUntilEnd $ChangeVarInfo(MeshUntilEnd)
	}

	if { $ChangeVarInfo(AllowAutomaticStructured) != \
		[.central.s info variable AllowAutomaticStructured] } {
	    lappend res AllowAutomaticStructured $ChangeVarInfo(AllowAutomaticStructured)
	}

	if { $ChangeVarInfo(NoMeshFrozenLayers) != \
		[.central.s info variable NoMeshFrozenLayers] } {
	    lappend res NoMeshFrozenLayers $ChangeVarInfo(NoMeshFrozenLayers)
	}

	if { $ChangeVarInfo(AutomaticCorrectSizes) != \
		[.central.s info variable AutomaticCorrectSizes] } {
	    lappend res AutomaticCorrectSizes $ChangeVarInfo(AutomaticCorrectSizes)
	}
	set ForceMeshEntities 0
	if { $ChangeVarInfo(ForceMeshPoints) } {
	  set ForceMeshEntities [expr $ForceMeshEntities +1]
	}
	if { $ChangeVarInfo(ForceMeshLines) } {
	  set ForceMeshEntities [expr $ForceMeshEntities +2]
	}
	if { $ChangeVarInfo(ForceMeshSurfaces) } {
	  set ForceMeshEntities [expr $ForceMeshEntities +4]
	}
	if { $ForceMeshEntities != \
		    [.central.s info variable ForceMeshEntities] } {
		lappend res ForceMeshEntities $ForceMeshEntities
	}

	if { $ChangeVarInfo(HighQualitySmoothing)=="Normal" } {
	    set HighQualitySmoothing 0
	} elseif { $ChangeVarInfo(HighQualitySmoothing)=="HighAngle" } {
	    set HighQualitySmoothing 1
	} else {
	    set HighQualitySmoothing 2
	}
	if { $HighQualitySmoothing != [.central.s info variable HighQualitySmoothing] } {
	    lappend res HighQualitySmoothing $HighQualitySmoothing
	}
	
	if { $res != "" } {
	    eval [concat .central.s process escape escape escape escape utilities \
		    variables $res escape escape]
	    ChangeWarnLineVar "Accepted new $ChangeVarInfo(type) preferences"
	}
    }

    set res ""
    if { $ChangeVarInfo(type) == "IMPORT" } {

	if { $ChangeVarInfo(AutoCollapseAfterImport) != \
		[.central.s info variable AutoCollapseAfterImport] } {
	    lappend res AutoCollapseAfterImport $ChangeVarInfo(AutoCollapseAfterImport)
	}

	if { $ChangeVarInfo(ImportTolerance) != \
		[.central.s info variable ImportTolerance] } {
	    lappend res ImportTolerance $ChangeVarInfo(ImportTolerance)
	}

	if { $ChangeVarInfo(CollapseIgnoringLayers) != \
		[.central.s info variable CollapseIgnoringLayers] } {
	    lappend res CollapseIgnoringLayers $ChangeVarInfo(CollapseIgnoringLayers)
	}

	if { $ChangeVarInfo(IGESIgnoreInvisible) != \
		[.central.s info variable IGESIgnoreInvisible] } {
	    lappend res IGESIgnoreInvisible $ChangeVarInfo(IGESIgnoreInvisible)
	}

	if { $ChangeVarInfo(IGESCurveOnSurfaceFrom3D) != \
		[.central.s info variable IGESCurveOnSurfaceFrom3D] } {
	    lappend res IGESCurveOnSurfaceFrom3D $ChangeVarInfo(IGESCurveOnSurfaceFrom3D)
	}

	if { $ChangeVarInfo(AutoImportTolerance) != \
		[.central.s info variable AutoImportTolerance] } {
	    lappend res AutoImportTolerance $ChangeVarInfo(AutoImportTolerance)
	}

	if { $res != "" } {
	    eval [concat .central.s process escape escape escape escape utilities \
		    variables $res escape escape]
	    ChangeWarnLineVar "Accepted new $ChangeVarInfo(type) preferences"
	}
    }
    if { $ChangeVarInfo(type) == "FONTS" } {
	foreach i "NormalFont FixedFont BigFont BoldFont CommentsFont LabelsFont" {
	    set fv $i
	    if { $i == "BoldFont" } {
		set fv NormalFont
		set weight bold
	    } else { set weight $ChangeVarInfo($fv,weight) }
	    font configure $i -family $ChangeVarInfo($fv,family) \
		    -size $ChangeVarInfo($fv,size) \
		    -weight $weight \
		    -slant $ChangeVarInfo($fv,slant)
	}

	AcceptGiDFont CommentsFont
	AcceptGiDFont LabelsFont
    }
    set res ""
    if { $ChangeVarInfo(type) == "FORMAT" } {

	foreach fmt "PostLegendFormat PostScalarFormat PostVectorFormat PostMatrixFormat" {
	    set gid_val [ .central.s info variable $fmt]
	    if { $ChangeVarInfo($fmt) != $gid_val} {
		.central.s process escape escape escape escape \
		        utilities variables $fmt \"$ChangeVarInfo($fmt)\"\
		        escape escape escape escape
	    }
	}

	set GidPriv(RealFormat,Default) $ChangeVarInfo(RealFormat,Default)

    }
    if { $ChangeVarInfo(type) == "POSTPROCESS" } {

	#set AreWeInPost [ .central.s info postprocess arewein ]
	#if { $AreWeInPost == "NO" } {
	#    tk_dialogRAM $w.err [_ "error"] \
	#             [_ "These options only work inside the PostProcess section!"] error 0 [_ "OK"]
	#}

    }
    if { $res != "" } {
	eval [concat .central.s process escape escape escape escape results $res \
		escape escape]
	ChangeWarnLineVar "Accepted new $ChangeVarInfo(type) preferences"
    }
}


proc DynamicPrecision { val } {
    .central.s process escape escape escape escape utilities variables \
	    CurvePrecision $val escape
#    .central.s makecurrent
#    .central.s process redraw
    .central.s render
}

proc ChangeToPrefType { w name } {
    global ChangeVarInfo
    $w conf -cursor watch
    update
    switch $name {
	General {
	    set ChangeVarInfo(type) GENERAL
	}
	Graphical { 
	    set ChangeVarInfo(type) GRAPH
	}
	Meshing { 
	    set ChangeVarInfo(type) MESHING
	}
	Import { 
	    set ChangeVarInfo(type) IMPORT
	}
	Fonts { 
	    set ChangeVarInfo(type) FONTS
	}
	Format {
	    set ChangeVarInfo(type) FORMAT

	    #get_all_post_limits            
	}
	Postprocess {
	    set ChangeVarInfo(type) POSTPROCESS

	    #get_all_post_limits            
	}
    }
    FillPrefValuesFromGid
    $w conf -cursor ""
}

proc get_all_post_limits { } {
    global ChangeVarInfo

    set postprefs [.central.s info postprocess get results_options]

    set postncols [ lindex $postprefs 0]
    set ChangeVarInfo(MinColors) [ lindex $postncols 1]
#    set ChangeVarInfo(MaxColors) [ lindex $postncols 2]

    set postconttype [ lindex $postprefs 3]
    set ChangeVarInfo(ContourAllTypes) "[ lindex $postconttype 1] [ lindex $postconttype 2]"

    set postvoff [ lindex $postprefs 4]
    set ChangeVarInfo(MinVectorOffset) [ lindex $postvoff 1]
    set ChangeVarInfo(MaxVectorOffset) [ lindex $postvoff 2]

    set postvcol [ lindex $postprefs 5]
    set ChangeVarInfo(VectorAllColors) "[ lindex $postvcol 1] [ lindex $postvcol 2]"

    set base $ChangeVarInfo(BasePostWin) 
    set existPost [ lindex [.central.s info Project] 7]
    
    set borderangles [ join [ .central.s info postprocess get criterio_bordes]]
    set ChangeVarInfo(BordeAngleMin) [ lindex $borderangles 1]                
    set ChangeVarInfo(BordeAngleMax) [ lindex $borderangles 2]                
    
    set postvcols [ lindex $postprefs 6]
    set ChangeVarInfo(VectMinColors) [ lindex $postvcols 1]
#    set ChangeVarInfo(VectMaxColors) [ lindex $postvcols 2]
    
    if { $existPost == "YESPOST"} {
##        $base.label#1 conf -text "Number of colors ($ChangeVarInfo(MinColors):$ChangeVarInfo(MaxColors)):"
#         $base.label#1 conf -text "Number of colors (up $ChangeVarInfo(MinColors)):"
#         #set cmd "tk_optionMenu $base.menubutton#1 ChangeVarInfo(ContourType) $ChangeVarInfo(ContourAllTypes)"
#         #eval $cmd
#         $base.menubutton#1.menu del 0 end
#         foreach i $ChangeVarInfo(ContourAllTypes) {
#             $base.menubutton#1.menu add command -label $i \
#                     -command [list set ChangeVarInfo(ContourType) $i]
#         }
#         $base.menubutton#1 conf -width 14
#         #set cmd "tk_optionMenu $base.menubutton#2 ChangeVarInfo(VectorColor) $ChangeVarInfo(VectorAllColors)"
#         #eval $cmd
#         $base.menubutton#2.menu del 0 end
#         foreach i $ChangeVarInfo(VectorAllColors) {
#             $base.menubutton#2.menu add command -label $i \
#                     -command [list set ChangeVarInfo(VectorColor) $i]
#         }
#         $base.menubutton#2 conf -width 14
#         $base.label#7 conf -text "Vector offset ($ChangeVarInfo(MinVectorOffset):$ChangeVarInfo(MaxVectorOffset)):"
#         
#         $base.label#9 conf -text "Border Angle ($ChangeVarInfo(BordeAngleMin):$ChangeVarInfo(BordeAngleMax)):"
#
##        $base.label#81 conf -text "N. of Vector colors ($ChangeVarInfo(VectMinColors):$ChangeVarInfo(VectMaxColors)):"
#         $base.label#81 conf -text "N. of Vector colors ( up $ChangeVarInfo(VectMinColors)):"
#
#         set AreWeInPost [ .central.s info postprocess arewein ]
#         if { $AreWeInPost == "NO" } {
#             $base.label#1 conf -fg grey60
#             $base.label#2 conf -fg grey60
#             $base.label#3 conf -fg grey60
#             $base.label#4 conf -fg grey60
#             $base.label#5 conf -fg grey60
#             $base.label#6 conf -fg grey60
#             $base.label#7 conf -fg grey60
#             $base.label#8 conf -fg grey60
#             $base.label#81 conf -fg grey60
#             $base.label#9 conf -fg grey60
#             $base.entry#1 conf -state disabled
#             $base.entry#1 conf -fg grey60
#             $base.entry#2 conf -state disabled
#             $base.entry#2 conf -fg grey60
#             $base.entry#3 conf -state disabled
#             $base.entry#3 conf -fg grey60
#             $base.entry#4 conf -state disabled
#             $base.entry#4 conf -fg grey60
#             $base.entry#5 conf -state disabled
#             $base.entry#5 conf -fg grey60
#             $base.entry#6 conf -state disabled
#             $base.entry#6 conf -fg grey60
#             $base.entry#81 conf -state disabled
#             $base.entry#81 conf -fg grey60
#             $base.menubutton#1 conf -state disabled
#             $base.menubutton#2 conf -state disabled
#         } else  {
#             $base.label#1 conf -fg black
#             $base.label#2 conf -fg black
#             $base.label#3 conf -fg black
#             $base.label#4 conf -fg black
#             $base.label#5 conf -fg black
#             $base.label#6 conf -fg black
#             $base.label#7 conf -fg black
#             $base.label#8 conf -fg black
#             $base.label#81 conf -fg black
#             $base.label#9 conf -fg black
#             $base.entry#1 conf -state normal
#             $base.entry#1 conf -fg black
#             $base.entry#2 conf -state normal
#             $base.entry#2 conf -fg black
#             $base.entry#3 conf -state normal
#             $base.entry#3 conf -fg black
#             $base.entry#4 conf -state normal
#             $base.entry#4 conf -fg black
#             $base.entry#5 conf -state normal
#             $base.entry#5 conf -fg black
#             $base.entry#6 conf -state normal
#             $base.entry#6 conf -fg black
#             $base.entry#81 conf -state normal
#             $base.entry#81 conf -fg black
#             $base.menubutton#1 conf -state normal
#             $base.menubutton#2 conf -state normal
#         }
    }

}

#
# ChangeVariables
#



source $GIDDEFAULTTCL/pref_general.ui.tcl
source $GIDDEFAULTTCL/pref_graphical.ui.tcl
source $GIDDEFAULTTCL/pref_format.ui.tcl
source $GIDDEFAULTTCL/pref_meshing.ui.tcl
source $GIDDEFAULTTCL/pref_import.ui.tcl
source $GIDDEFAULTTCL/pref_fonts_ui.tcl
#source $GIDDEFAULTTCL/pref_postprocess.ui.tcl

proc ChangeVariables { { w .gid.wvar } } {

    set existPost [ lindex [.central.s info Project] 7]

    InitWindow $w [_ "Preferences"] ChangeVarWindowGeom
	    
    wm withdraw $w

    frame $w.buts -bg [CCColorActivo [$w cget -background]]
    button $w.buts.ok -text [_ "Accept"] -command "AcceptValues $w" -under 0
    button $w.buts.rs -text [_ "Reset"] -command FillValuesDefault -under 0
    button $w.buts.cn -text [_ "Close"] -command "destroy $w" -under 0
    #pack $w.buts.ok $w.buts.rs $w.buts.cn -side left -padx 5 -ipadx 5 -pady 5
    grid $w.buts.ok -row 0 -column 0 -padx 5 -pady 5
    grid $w.buts.rs -row 0 -column 1 -padx 5 -pady 5
    grid $w.buts.cn -row 0 -column 2 -padx 5 -pady 5

    GidHelp $w.buts.ok [_ "With this button, changed values of the active tab are\
    accepted. If this button is not pressed, values changed by the user are not\
    computed."]
    GidHelp $w.buts.rs [_ "This button sets all the options of the active tab to its\
	    default values."]
    GidHelp $w.buts.cn [_ "This button closes the window without accepting any changed\
	    value. Press 'Accept' not to loose the changed options."]

    focus $w.buts.ok
    bind $w <Escape> "tkButtonInvoke $w.buts.cn"
    bind $w <Alt-a>  "tkButtonInvoke $w.buts.ok"
    bind $w <Alt-r>  "tkButtonInvoke $w.buts.rs"
    bind $w <Alt-c>  "tkButtonInvoke $w.buts.cn ; break"

    frame $w.f
    grid $w.buts -row 1 -column 0 -sticky nsew
    grid $w.f -row 0 -column 0 -sticky nsew
    grid rowconfigure $w.f 0 -weight 1
    grid columnconfigure $w.f 0 -weight 1
    grid rowconfigure $w 0 -weight 1
    grid columnconfigure $w 0 -weight 1

    set all_qq "pref_general_ui pref_graphical_ui pref_meshing_ui \
	    pref_import_ui pref_fonts_ui pref_format_ui "
    #set labels [list [_ "General"]  [_ "Graphical"]  [_ "Meshing"]  \
    #             [_ "Import"]  [_ "Fonts"] [_ "Format"] [_ "Post"]]
    set labels [list [_ "General"]  [_ "Graphical"]  [_ "Meshing"]  \
	    [_ "Import"]  [_ "Fonts"] [_ "Format"]]
    set func "optionbar $w.f.c"
    foreach qq $all_qq label $labels {
	regexp {pref_(.*)_ui} $qq dummy pref
	set pref [string tolower $pref]
	set holder [frame $w.f.$pref]
	$qq $holder
	set pref [string toupper \
		[string range $pref 0 0]][string range $pref 1 end]
	lappend func [list $label $holder [list ChangeToPrefType $w $pref]]
    }

    eval $func
    pack $w.f.c -fill both -expand yes

    update idletasks

    wm deicon $w
}




#----------------------ChangeVariables ALONE---------------------------
# if { $ChangeVariablesRunningAlone == 1 } {
#     ChangeVariables
# } 
#-----------------------------------------------------------------
