#!/bin/sh
#PostResults -*- TCL -*- Created: ramsan&miguel Jan-1997, Modified: ramsan Jan-1997


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

set PRRunningAlone 0
# if { ![info exists GIDDEFAULT] } {
#     set PRRunningAlone 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 FillPRInfo {} {
    global GidPriv

    if { ![info exists GidPriv(PostResultsWindow)] || \
	    ![winfo exists $GidPriv(PostResultsWindow)] } { return }


    set w $GidPriv(PostResultsWindow)

    #puts FillPRInfo----------------------------
    set all [.central.s info post get all_analisis]
    set curr [.central.s info post get cur_analisis]
    if { $all == ""} { 
	$w.m1.menu del 0 end
	$w.m2.menu del 0 end
	#$w.m3.menu del 0 end
	$w.m3 listbox del 0 end
	$w.m4.menu del 0 end
	return
    }
    $w.m1.menu del 0 end
    foreach i $all {
	regsub -all {_} $i { } var
	$w.m1.menu add command -label $var -command \
		[list set GidPriv(PostResultsAnal) $var]
    }
    if { $curr != "" } {
	regsub -all {_} $curr { } curr
    } else { regsub -all {_} [lindex $all 0] { } curr }
	set GidPriv(PostResultsAnal) $curr

    # Ahora los Steps:
    regsub -all { } $GidPriv(PostResultsAnal) {_} analisis
    $w.ll.list del 0 end
    if { $analisis != ""} {
	set current [.central.s info post get cur_step $analisis]
	set currentidx 0
	foreach i [.central.s info post get all_steps $analisis] {
	    $w.ll.list ins end $i
	    if { $i == $current } {
		$w.ll.list sel set end
		set currentidx [$w.ll.list index end]
	    }
	}
	$w.ll.list see $currentidx
	$w.e1 del 0 end
	$w.e1 ins end $current
	focus $w.e1
    } else {
	$w.e1 del 0 end
	$w.e1 conf -state disabled
    }
    set all [.central.s info post get all_steps $analisis]
    set curr [.central.s info post get cur_step]
    if { $curr != "" } {
	regsub -all {_} $curr { } curr
    } else { regsub -all {_} [lindex $all 0] { } curr }
    #WarnWinText "FillPRInfo: set GidPriv(PostResultsStep) -$curr-"
    set GidPriv(PostResultsStep) $curr


    # Ahora la vista de resultados
    $w.m2.menu del 0 end
    set all [.central.s info post get all_results_views]
    foreach j $all {
	regsub -all {_} $j { } var
	$w.m2.menu add command -label $var -command \
		[list set GidPriv(PostResultsView) $var]
    }
    set curr [.central.s info post get cur_results_view]
    if { $curr == "Iso_Surfaces"} { set curr "No_Result"}
    if { $curr == "" } {
    regsub -all {_} [ lindex $all 0] { } curr
    } else {
	regsub -all {_} $curr { } curr
    }
    set GidPriv(PostResultsView) $curr

    # Ahora los resultados
    #$w.m3.menu del 0 end
    $w.m3 listbox del 0 end
    regsub -all { }  $GidPriv(PostResultsView) {_} curr
    set all [.central.s info post get cur_results_list $curr ]
    if { $all != "" } {
	foreach j $all {
	    regsub -all {_} $j { } var
	    #$w.m3.menu add command -label $var -command \
		#    [list set GidPriv(PostResultsRes) $var]
	    $w.m3 add $var
	}

	set curr [.central.s info post get cur_result]
	if { $curr == "" } {
	    regsub -all {_} [ lindex $all 0] { } curr
	} else {
	    regsub -all {_} $curr { } curr
	}

	set GidPriv(PostResultsRes) $curr
    } else {
	set GidPriv(PostResultsRes) ""
    }

    # y sus compoenentes
    $w.m4.menu del 0 end
    if { $GidPriv(PostResultsRes) != "" } {
	regsub -all { } $GidPriv(PostResultsRes) {_} curr
	set all [.central.s info post get cur_components_list $curr ]
	if { $all != "" } {
	    foreach j $all {
		regsub -all {_} $j { } var
		$w.m4.menu add command -label $var -command \
			[list set GidPriv(PostResultsComp) $var]
	    }
	    
	    set curr [.central.s info post get cur_component]
	    if { $curr == "" } {
		regsub -all {_} [ lindex $all 0] { } curr
	    } else {
		regsub -all {_} $curr { } curr
	    }

	    set GidPriv(PostResultsComp) $curr
	}
    } else {
	set GidPriv(PostResultsComp) ""
    }

    $w.f.e2 del 0 end 
    $w.f.e2 conf -state disabled
}

proc FillPRSteps {name1 name2 op} {
    global GidPriv

    #puts FillPRSteps----------------------
    set w $GidPriv(PostResultsWindow)
    regsub -all { } $GidPriv(PostResultsAnal) {_} analisis
    $w.ll.list del 0 end
    if { $analisis != ""} {
	set all [.central.s info post get all_steps $analisis]
	set current [.central.s info post get cur_step $analisis]
	if { ( $current == "") || ( [ lsearch $all $current] == -1) } { 
	    regsub -all {_} [lindex $all 0] { } current
	}
	set currentidx 0
	foreach i $all {
	    $w.ll.list ins end $i
	    if { $i == $current } {
		$w.ll.list sel set end
		set currentidx [$w.ll.list index end]
	    }
	}
	$w.ll.list see $currentidx
	$w.e1 del 0 end
	$w.e1 ins end $current
	#WarnWinText "FillPRSteps: set GidPriv(PostResultsStep) -$current-"
	set GidPriv(PostResultsStep) $current
	focus $w.e1
    } else {
	$w.e1 del 0 end
	$w.e1 conf -state disabled
    }
}

proc PRChangedEntry {name1 name2 op} {
    global GidPriv
    set w $GidPriv(PostResultsWindow)

    #WarnWinText PRChangedEntry----------------------------
    if {  $GidPriv(PostResultsStep) == ""} { return }
    set elms [$w.ll.list get 0 end] 
    $w.ll.list sel clear 0 end

    set first [lsearch $elms $GidPriv(PostResultsStep)]
    #WarnWinText "first = $first = lsearch -$elms- -$GidPriv(PostResultsStep)-"
    if { $first != -1 } {
	$w.ll.list see $first
	if { [lsearch [lrange $elms [expr $first+1] end] $GidPriv(PostResultsStep)] == -1 } {
	    $w.ll.list sel set $first
	    $w.ll.list activate $first
    #WarnWinText "PRChangedEntry set GidPriv(PostResultsStep) -[lindex $elms $first]-"
	    set GidPriv(PostResultsStep) [lindex $elms $first]
	    FillPROtherMenusLazy $name1 $name2 $op
	    return
	}
    } else {
	bell
	set GidPriv(PostResultsStep) [string range $GidPriv(PostResultsStep) 0 [$w.e1 index insert]]
	set GidPriv(PostResultsStep) [string range $GidPriv(PostResultsStep) 0 \
		[expr [string length $GidPriv(PostResultsStep)] -2]  ]
    }
    set GidPriv(PostResultsView) ""
    $w.ll.list sel clear 0 end
}

proc FillPROtherMenus {name1 name2 op} {
    global GidPriv
    set w $GidPriv(PostResultsWindow)

    #puts FillPROtherMenus-----------------------------
    set vn { PostResultsStep PostResultsView PostResultsRes PostResultsComp}
    set allmenus "$w.m2.menu $w.m3 $w.m4.menu"
    set i [lsearch $vn $name2]
    switch $i {
	0 {
	    set all [.central.s info post get all_results_views]
	    set curr [.central.s info post get cur_results_view]
	    if { $curr == "Iso_Surfaces"} { set curr "No_Result"}
	}
	1 {
	    if { $GidPriv(PostResultsView) == "" } {
		set all ""
		set curr ""
	    } else {
		regsub -all { } $GidPriv(PostResultsView) {_} res_view
		regsub -all { } $GidPriv(PostResultsAnal) {_} res_anal
		if { ( $res_view == "" ) || ( $res_anal == "") } {
		    set all ""
		    set curr ""
		} else {
		    
		    set all [.central.s info post get results_list \
			    $res_view  $res_anal \
			    $GidPriv(PostResultsStep)]
		    #WarnWinText "$res_view -> $all"
		    
		    set curr [.central.s info post get cur_result]
		    if { $curr == "" || [ lsearch -exact $all $curr] == -1 } {
			set curr [lindex $all 0]
		    }
		    regsub -all { } $GidPriv(PostResultsRes) {_} res_res
		    regsub -all { } $GidPriv(PostResultsComp) {_} res_comp
		    if { [ lsearch -exact $all $res_res] == -1} {
			set res_res [lindex $all 0]
		    }
		    if { $GidPriv(PostResultsView) == "Display Vectors" } {
			$w.f.e2 conf -state normal
			$w.f.e2 del 0 end
			if { ( $res_res != "") && ( $res_anal != "") && ( $res_view != "") && \
				( $GidPriv(PostResultsStep) != "") } {
			    #WarnWin "1 - -$res_view-$res_res-$res_anal-$GidPriv(PostResultsStep)-"
			    set kk  [.central.s info post get cur_vector_factor \
				    $res_view $res_res $res_anal $GidPriv(PostResultsStep) ]
			    if { $kk != 0 } { $w.f.e2 ins end $kk }
			    focus $w.f.e2
			} else { 
			    $w.f.e2 ins end "1.0"
			    focus $w.f.e2
			}
			$w.m4 conf -state normal
		    } elseif { ( $GidPriv(PostResultsView) == "Scalar Line Diagram") || \
			    ( $GidPriv(PostResultsView) == "Vector Line Diagram") } {
			$w.f.e2 conf -state normal
			$w.f.e2 del 0 end
			if { ( $res_res != "") && ( $res_anal != "") && ( $res_view != "") && \
				( $GidPriv(PostResultsStep) != "") } {
			    if { ( $GidPriv(PostResultsView) == "Scalar Line Diagram") && ( $res_comp != "")} {
				set kk  [.central.s info post get cur_diagram_factor $res_res $res_comp $res_anal $GidPriv(PostResultsStep)]
			    } else {
				set kk  [.central.s info post get cur_diagram_factor $res_res $res_anal $GidPriv(PostResultsStep)]
			    }
			    if { $kk != 0 } { $w.f.e2 ins end $kk }
			    focus $w.f.e2
			} else { 
			    $w.f.e2 ins end "1.0"
			    focus $w.f.e2
			}
			if { $GidPriv(PostResultsView) == "Vector Line Diagram" } {
			    $w.m4 conf -state disabled
			} else {
			    $w.m4 conf -state normal
			}
		    } elseif { $GidPriv(PostResultsView) == "Result Surface" } {
			$w.f.e2 conf -state normal
			$w.f.e2 del 0 end
			if { ( $res_res != "") && ( $res_anal != "") && ( $res_view != "") && \
				( $GidPriv(PostResultsStep) != "") } {
			    #WarnWin "1 - -$res_view-$res_res-$res_anal-$GidPriv(PostResultsStep)-"
			    set kk  [.central.s info post get cur_result_surface_factor \
				    $res_view $res_res $res_comp $res_anal $GidPriv(PostResultsStep) ]
			    if { $kk != 0 } { $w.f.e2 ins end $kk }
			    focus $w.f.e2
			} else { 
			    $w.f.e2 ins end "1.0"
			    focus $w.f.e2
			}
			$w.m4 conf -state normal
		    } else { 
			#$w.f.e2 del 0 end 
			$w.f.e2 conf -state disabled
			$w.m4 conf -state normal
		    }
		}
	    }
	}
	2 {
	    if { $GidPriv(PostResultsView) == "" } {
		set all ""
		set curr ""
	    } else {
		regsub -all { } $GidPriv(PostResultsView) {_} res_view
		regsub -all { } $GidPriv(PostResultsRes) {_} res_res
		regsub -all { } $GidPriv(PostResultsAnal) {_} res_anal
		set all [.central.s info post get components_list \
			$res_view $res_res $res_anal \
			$GidPriv(PostResultsStep)]
		set curr [.central.s info post get cur_component]
		if { $curr == "" || [ lsearch -exact $all $curr] == -1 } {
		    set curr [lindex $all 0]
		}
		if { $GidPriv(PostResultsView) == "Display Vectors" } {
		    if { ( $res_res != "") && ( $res_anal != "") && ( $res_view != "") && \
			    ( $GidPriv(PostResultsStep) != "") } {
			#WarnWin "2 --$res_view-$res_res-$curr-$res_anal-$GidPriv(PostResultsStep)-"
			set kk  [.central.s info post get cur_vector_factor \
				$res_view $res_res $curr $res_anal $GidPriv(PostResultsStep) ]
			if { $kk != 0 } {
			    $w.f.e2 del 0 end  
			    $w.f.e2 ins end $kk 
			} else {
			    $w.f.e2 ins end "1.0"
			}
		    }
		}
		if { ( $GidPriv(PostResultsView) == "Scalar Line Diagram") || \
			( $GidPriv(PostResultsView) == "Vector Line Diagram") } {
		    if { ( $res_res != "") && ( $res_anal != "") && ( $res_view != "") && \
			    ( $GidPriv(PostResultsStep) != "") } {
			if { ( $GidPriv(PostResultsView) == "Scalar Line Diagram") && ( $curr != "")} {
			    set kk  [.central.s info post get cur_diagram_factor $res_res $curr $res_anal $GidPriv(PostResultsStep)]
			} else {
			    set kk  [.central.s info post get cur_diagram_factor $res_res $res_anal $GidPriv(PostResultsStep)]
			}
			#WarnWin "'$res_res' '$curr' '$res_anal' '$GidPriv(PostResultsStep)' -> '$kk'"
			if { $kk != 0 } { 
			    $w.f.e2 del 0 end  
			    $w.f.e2 ins end $kk 
			}
		    } else { 
			$w.f.e2 ins end "1.0"
		    }
		}
		if { $GidPriv(PostResultsView) == "Result Surface" } {
		    if { ( $res_res != "") && ( $res_anal != "") && ( $res_view != "") && \
			    ( $GidPriv(PostResultsStep) != "") } {
			#WarnWin "2 --$res_view-$res_res-$curr-$res_anal-$GidPriv(PostResultsStep)-"
			set kk  [.central.s info post get cur_result_surface_factor \
				$res_view $res_res $curr $res_anal $GidPriv(PostResultsStep) ]
			if { $kk != 0 } {
			    $w.f.e2 del 0 end  
			    $w.f.e2 ins end $kk 
			} else {
			    $w.f.e2 ins end "1.0"
			}
		    }
		}
	    }
	}
	3 {
	    if { $GidPriv(PostResultsView) == "" } {
		set all ""
		set curr ""
	    } else {
		regsub -all { } $GidPriv(PostResultsView) {_} res_view
		regsub -all { } $GidPriv(PostResultsRes) {_} res_res
		regsub -all { } $GidPriv(PostResultsComp) {_} res_comp
		regsub -all { } $GidPriv(PostResultsAnal) {_} res_anal
		set all [.central.s info post get components_list \
			$res_view $res_res $res_anal \
			$GidPriv(PostResultsStep)]
		set curr [.central.s info post get cur_component]
		if { $curr == "" || [ lsearch -exact $all $curr] == -1 } {
		    set curr [lindex $all 0]
		}
		if { $GidPriv(PostResultsView) == "Display Vectors" } {
		    if { ( $res_res != "") && ( $res_anal != "") && ( $res_view != "") && \
			    ( $GidPriv(PostResultsStep) != "") } {
			#WarnWin "3 -$res_view-$res_res-$res_comp-$res_anal-$GidPriv(PostResultsStep)-"
			set kk  [.central.s info post get cur_vector_factor \
				$res_view $res_res $res_comp $res_anal $GidPriv(PostResultsStep) ]
			if { $kk != 0 } {
			    $w.f.e2 del 0 end  
			    $w.f.e2 ins end $kk 
			} else {
			    $w.f.e2 ins end "1.0"
			}
		    }
		}
		if { ( $GidPriv(PostResultsView) == "Scalar Line Diagram") ||
		( $GidPriv(PostResultsView) == "Vector Line Diagram") } {
		    if { ( $res_res != "") && ( $res_anal != "") && ( $res_view != "") && \
			    ( $GidPriv(PostResultsStep) != "") } {
			if { ( $GidPriv(PostResultsView) == "Scalar Line Diagram") && ( $res_comp != "")} {
			    set kk  [.central.s info post get cur_diagram_factor $res_res $res_comp $res_anal $GidPriv(PostResultsStep)]
			} else {
			    set kk  [.central.s info post get cur_diagram_factor $res_res $res_anal $GidPriv(PostResultsStep)]
			}
			#WarnWin "'$res_res' '$res_comp' '$res_anal' '$GidPriv(PostResultsStep)' -> '$kk'"
			if { $kk != 0 } { 
			    $w.f.e2 del 0 end  
			    $w.f.e2 ins end $kk 
			}
		    } else { 
			$w.f.e2 ins end "1.0"
		    }
		}
		if { $GidPriv(PostResultsView) == "Result Surface" } {
		    if { ( $res_res != "") && ( $res_anal != "") && ( $res_view != "") && \
			    ( $GidPriv(PostResultsStep) != "") } {
			#WarnWin "3 -$res_view-$res_res-$res_comp-$res_anal-$GidPriv(PostResultsStep)-"
			set kk  [.central.s info post get cur_result_surface_factor \
				$res_view $res_res $res_comp $res_anal $GidPriv(PostResultsStep) ]
			if { $kk != 0 } {
			    $w.f.e2 del 0 end  
			    $w.f.e2 ins end $kk 
			} else {
			    $w.f.e2 ins end "1.0"
			}
		    }
		}
	    }
	}
    }

    if { "[lindex $vn [expr $i+1]]" != ""} {
	#WarnWinText "[lindex $vn [expr $i+1]] - $curr - $GidPriv([lindex $vn [expr $i+1]])"
	#if { $GidPriv([lindex $vn [expr $i+1]]) != $curr } {
	    set m [lindex $allmenus $i]
	    if { "$m" != "$w.m3"} {
		#WarnWinText "Contruyendo $m -> $all"
		$m del 0 end
		foreach j $all {
		    regsub -all {_} $j { } var
		    $m add command -label $var -command \
			    [list set GidPriv([lindex $vn [expr $i+1]]) $var]
		}
	    } else {
		$m listbox del 0 end
		foreach j $all {
		    regsub -all {_} $j { } var
		    $m add $var
		}
	    }
	    regsub -all {_} $curr { } curr
	    set GidPriv([lindex $vn [expr $i+1]]) $curr
	#}
    }
}

proc FillPROtherMenusLazy { name1 name2 op} {
    global GidPriv
    set w $GidPriv(PostResultsWindow)

    #puts FillPROtherMenusLazy---------------------------------
    set allmenus "$w.m2.menu $w.m3 $w.m4.menu"
    regsub -all { } $GidPriv(PostResultsView) {_} res_view
    regsub -all { } $GidPriv(PostResultsAnal) {_} res_anal
    if { $res_view == "" } {
	FillPROtherMenus $name1 $name2 $op 
	return
    }

    set all [.central.s info post get results_list $res_view $res_anal \
	    $GidPriv(PostResultsStep) ]
    regsub -all { } $GidPriv(PostResultsRes) {_} res_res

    if { $res_res == "" } {
	FillPROtherMenus $name1 $name2 $op 
	return
    }
    if { [ lsearch $all $res_res] == -1 } { 
	FillPROtherMenus $name1 $name2 $op 
	return
    } else {
	set all [.central.s info post get components_list $res_view \
		$res_res $res_anal $GidPriv(PostResultsStep)]
	regsub -all { } $GidPriv(PostResultsComp) {_} res_comp
	if { [ lsearch $all $res_comp] == -1 } { 
	    FillPROtherMenus $name1 $name2 $op 
	    return
	} 
    }
}

proc PRAccept {} {
    global GidPriv

    #puts PRAccept---------------------------
    set oldAutomaticRedraw [.central.s info variables AutomaticRedraw]
    .central.s process escape escape escape escape escape escape utilities variables \
	    AutomaticRedraw -1 escape escape escape escape 

    set w $GidPriv(PostResultsWindow)
    regsub -all { } $GidPriv(PostResultsAnal) {_} res_anal
    regsub -all { } $GidPriv(PostResultsStep) {_} res_step
    set postcmd "escape escape escape escape escape escape results analysissel $res_anal $res_step"
    regsub -all { } $GidPriv(PostResultsView) {} res_view
    if { $res_view == "NoResult"} {
	set postcmd "$postcmd geometry $res_view"
    } else {
	regsub -all { } $GidPriv(PostResultsRes) {_} res_res
	regsub -all { } $GidPriv(PostResultsComp) {_} res_comp
	if { ( $GidPriv(PostResultsView) != "Scalar Line Diagram") &&
	( $GidPriv(PostResultsView) != "Vector Line Diagram") } {
	    set postcmd "$postcmd $res_view"
	    set postcmd "$postcmd $res_res"
	    set postcmd "$postcmd $res_comp"
	    if { $res_view == "DisplayVectors" } {
		if { $GidPriv(PostResultsVfact) != ""} {
		    set postcmd "$postcmd $GidPriv(PostResultsVfact)"
		}
	    }
	    if { $res_view == "ResultSurface" } {
		if { $GidPriv(PostResultsVfact) != ""} {
		    set postcmd "$postcmd $GidPriv(PostResultsVfact)"
		}
	    }
	} else {
	    if { $GidPriv(PostResultsView) == "Scalar Line Diagram"} {
		set postcmd "$postcmd linediagram scalardiagram $res_res $res_comp $GidPriv(PostResultsVfact)"
	    } else {
		set postcmd "$postcmd linediagram vectordiagram $res_res $GidPriv(PostResultsVfact)"
	    }
	}
    }
    #set postcmd "$postcmd escape escape escape escape utilities variables"
    #set postcmd "$postcmd AutomaticRedraw $oldAutomaticRedraw escape escape"
    .central.s process $postcmd

    # lo separamos por si lo de arriba da un error
    set postcmd "escape escape escape escape utilities variables"
    set postcmd "$postcmd AutomaticRedraw $oldAutomaticRedraw escape escape"
    .central.s process $postcmd

#    .central.s makecurrent
#    .central.s process redraw"
    .central.s render

#     regsub -all { } $GidPriv(PostResultsDS) {} var
#     if { $var != [.central.s info post get cur_Results_style] } {
# 	.central.s process escape escape escape escape ResultsStyle $var
#     }
#     .central.s process escape escape escape escape utilities variables \
# 	    AutomaticRedraw $oldAutomaticRedraw escape escape 
#     .central.s process redraw
}

proc PRListboxToEntry {} {
    #puts PRListboxToEntry----------------------------
    global GidPriv
    set w $GidPriv(PostResultsWindow)
    if { $GidPriv(PostResultsAnal) != "" } {
    #WarnWinText "PRListboxToEntry set GidPriv(PostResultsStep) -[$w.ll.list get [$w.ll.list curselection]]-"
	set GidPriv(PostResultsStep) [$w.ll.list get [$w.ll.list curselection]] 
	focus $w.e1
    }
}

proc PostResults { { w .wPostResults } } {
    global GidPriv

    #puts PostResults----------------------------
    InitWindow $w [_ "View Results"] PostResultsWindowGeom PostResults

    catch {
	trace vdelete GidPriv(PostResultsAnal) w FillPRSteps
	trace vdelete GidPriv(PostResultsStep) w PRChangedEntry
	trace vdelete GidPriv(PostResultsView) w FillPROtherMenus
	trace vdelete GidPriv(PostResultsRes) w FillPROtherMenus
	trace vdelete GidPriv(PostResultsComp) w FillPROtherMenus
    }
    label $w.l1 -text [_ "Analysis:"]
    set menu [ tk_optionMenu $w.m1 GidPriv(PostResultsAnal) ""]
    $w.m1 conf -width 20 -bd 1 -highlightthickness 1
    $menu conf -bd 1 -activeborderwidth 1

    frame $w.ll
    scrollbar $w.ll.xscroll -relief sunken \
	    -orient horizontal -command "$w.ll.list xview" -bd 1 \
	    -elementborderwidth 1
#    listbox $w.ll.list -width 27 -height 4 -xscroll \
#
    listbox $w.ll.list -height 4 -xscroll \
	    "$w.ll.xscroll set" -relief sunken \
	    -selectmode browse -bd 1
    bind $w.ll.list <ButtonRelease-1> PRListboxToEntry
    bind $w.ll.list <Double-1> PRAccept 
    scrollbar $w.ll.yscroll -relief sunken -command \
	    "$w.ll.list yview" -bd 1 \
	    -elementborderwidth 1
    $w.ll.list conf -yscroll "$w.ll.yscroll set"
    pack $w.ll.yscroll -side right -fill y
    pack $w.ll.xscroll -side bottom -fill x
    pack $w.ll.list -fill both -side top -expand yes

    label $w.l2 -text [_ "Steps:"]
    entry $w.e1 -relief sunken -width 10 -textvariable GidPriv(PostResultsStep) -bd 1

    label $w.l3 -text [_ "View:"]
    set menu [ tk_optionMenu $w.m2 GidPriv(PostResultsView) ""]
    $w.m2 conf -width 16 -bd 1 -highlightthickness 1
    $menu conf -bd 1 -activeborderwidth 1

    frame $w.f
    label $w.f.l6 -text [_ "factor:"]
    entry $w.f.e2 -relief sunken -width 12 -textvariable GidPriv(PostResultsVfact) -bd 1
    pack $w.f.l6 $w.f.e2 -side left
    
    label $w.l4 -text [_ "Results:"]
    #set menu [ tk_optionMenu $w.m3 GidPriv(PostResultsRes) ""]
    #$w.m3 conf -width 16 -bd 1 -highlightthickness 1
    #$menu conf -bd 1 -activeborderwidth 1
    combobox $w.m3 -textvariable GidPriv(PostResultsRes) -editable 0

    label $w.l5 -text [_ "Component:"]
    set menu [ tk_optionMenu $w.m4 GidPriv(PostResultsComp) ""]
    $w.m4 conf -width 20 -bd 1 -highlightthickness 1
    $menu conf -bd 1 -activeborderwidth 1
   

    frame $w.but
    button $w.but.accept -text [_ "Apply"] -command PRAccept -takefocus 0 -bd 1
    button $w.but.close -text [_ "Close"] -command "destroy $w" -takefocus 0 -bd 1
    grid $w.but.accept -sticky e -padx 5
    grid $w.but.close -sticky w -padx 5 -row 0 -column 1


    grid $w.l1 -sticky e
    grid $w.m1 -sticky w -row 0 -column 1
#    grid $w.ll - -sticky ewns -padx 5
    grid $w.l2 -sticky en
    grid $w.ll -sticky ewns -row 1 -column 1
    grid $w.e1 -sticky w -row 2 -column 1
    grid $w.l3 -sticky e
    grid $w.m2 -sticky w -row 3 -column 1
#    grid $w.l6 -sticky e
#    grid $w.e2 -sticky w -row 4 -column 1
    grid $w.f -sticky w -row 4 -column 1
    grid $w.l4 -sticky e
    grid $w.m3 -sticky w -row 5 -column 1
    grid $w.l5 -sticky e
    grid $w.m4 -sticky w -row 6 -column 1
    grid $w.but - -pady 5
    grid columnconfigure $w 0 -weight 1
    grid columnconfigure $w 1 -weight 1
    grid rowconfigure $w 1 -weight 1

    FillPRInfo

    trace variable GidPriv(PostResultsAnal) w FillPRSteps
    trace variable GidPriv(PostResultsStep) w PRChangedEntry
    trace variable GidPriv(PostResultsView) w FillPROtherMenus
    trace variable GidPriv(PostResultsRes) w FillPROtherMenus
    trace variable GidPriv(PostResultsComp) w FillPROtherMenus

    bind $w <Return> PRAccept
    bind $w <Escape> "destroy $w"

}


#----------------------SetCoord ALONE---------------------------
# if { $PRRunningAlone == 1 } {
#     PostResults .wPostPR
# } 
#-----------------------------------------------------------------

proc FillPostLegendInfo { } {
    global GIDDEFAULT PLegendPriv GidPriv

    if { ![ info exist GidPriv(PostLegendWindow)] || ![ winfo exist $GidPriv(PostLegendWindow)]} {
	return
    }
    set w $GidPriv(PostLegendWindow)
    #$PLegendPriv(w)

    set lst_colores [ .central.s info post get current_color_scale]
        
    set lst_info [ .central.s info post get info_leyenda]

    if { ( "$lst_colores" == "") || ( "$lst_info" == "")} {
	$PLegendPriv(CanvasLabel) conf -text ""
	$PLegendPriv(Canvas) del all
	return
    }

    #WarnWin $lst_info

    set cv $PLegendPriv(Canvas)

    $PLegendPriv(CanvasLabel) conf -text ""
    $cv del all

    set que_res [ lindex $lst_info 0]

    if { ( "$que_res" == "") || ( "$que_res" == "NOTHING")} {
	$PLegendPriv(CanvasLabel) conf -text ""
	$PLegendPriv(Canvas) del all
	return
    }

    if { ( "$que_res" == "CONTOUR_FILL") || ( "$que_res" == "SMOOTH_CONTOUR_FILL") || \
	    ( "$que_res" == "CONTOUR_LINES") || ( "$que_res" == "CONTOUR_RANGES") || \
	    ( "$que_res" == "VECTORS") || ( "$que_res" == "ISO_SURFACES") } {
	#set label_txt [ lindex $lst_info 1]
	regsub -all {_} [ lindex $lst_info 1] { } label_txt
	$PLegendPriv(CanvasLabel) conf -text $label_txt
	
	if { ( "$que_res" == "CONTOUR_FILL") || ( "$que_res" == "SMOOTH_CONTOUR_FILL")} {
	    set n_info [ expr [ lindex $lst_info 2] + 1]
	} else {
	    set n_info [ lindex $lst_info 2]
	}
	set entradas [ lindex $lst_info 3]
	
	set n_col [ lindex $lst_colores 0]
	if {  ( $n_col > 0) && ( $n_info > 0)} {
	    # ajustamos parametros escala de color
	    set margen_h 12.0
	    set margen_w 8.0 
	    set max_v [ expr $PLegendPriv(CanvasHeight) - 2.0 * $margen_h]
	    set incr_col 1.0
	    
	    if { ( "$que_res" == "CONTOUR_FILL") || ( "$que_res" == "SMOOTH_CONTOUR_FILL") || \
		    ( "$que_res" == "ISO_SURFACES") || ( "$que_res" == "CONTOUR_RANGES")} {
		set npix [ expr $max_v / $n_col]
	    } else {
		set npix [ expr $max_v / [ expr $n_col - 1]]
	    }
	    if { $npix < 1.0} {
		set npix 1.0
		if { ( "$que_res" == "CONTOUR_FILL") || ( "$que_res" == "SMOOTH_CONTOUR_FILL") || \
			( "$que_res" == "ISO_SURFACES") || ( "$que_res" == "CONTOUR_RANGES")} {
		    set incr_col [ expr $n_col / $max_v]
		} else {
		    set incr_col [ expr [ expr $n_col - 1] / $max_v]
		}
	    }

	    set xi $margen_w
	    set xo [ expr $xi + 16.0]
	    set yi $margen_h
	    if { ( "$que_res" == "CONTOUR_FILL") || ( "$que_res" == "SMOOTH_CONTOUR_FILL") || \
		    ( "$que_res" == "ISO_SURFACES") || ( "$que_res" == "CONTOUR_RANGES")} {
		set yo [ expr $yi + $npix]
	    } else {
		set yo $yi
	    }

	    if { ( "$que_res" != "CONTOUR_RANGES")} {
		set alto_linea [ font metrics FixedFont -linespace]
		set num_lin [ expr round( $max_v / $alto_linea)]
	    } else {
		set num_lin $n_info
	    }
	    if { $num_lin < 1.0} { set num_lin 1.0}
	    #ajustamos parametros entradas de texto
	    set incr_idx_txt 1.0
	    if { $n_info > [ expr $num_lin + 1]} {
		if { $num_lin < 2.0} { set num_lin 2.0}
		set incr_idx_txt [ expr ( $n_info - 1.0) / ( $num_lin - 1.0)]
		set n_txt [ expr $num_lin - 1]
		set tmp ""
		lappend tmp [ format %.5g [ lindex $entradas 0]]
		set div [ expr ( [ lindex $entradas [ expr $n_info - 1]] - [ lindex $entradas 0]) / ( $num_lin - 1.0)]
		for { set i 1} { $i < $n_txt} { incr i} {
		    lappend tmp [ format %.5g [ expr [ lindex $tmp [ expr $i - 1]] + $div]]
		}
		lappend tmp [ format %.5g [ lindex $entradas [ expr $n_info - 1]]]
		set entradas $tmp
		#WarnWin "[ llength $entradas] - $entradas"
	    } else {
		#WarnWin "$n_info - $entradas"
		set n_txt [ expr $n_info - 1]
		if { ( "$que_res" != "CONTOUR_RANGES")} {
		    set tmp ""
		    for { set i 0} { $i <= $n_txt} { incr i} {
			lappend tmp [ format %.5g [ lindex $entradas $i]]
		    }
		    set entradas $tmp
		}
	    }

	    # chapuza para alinear for la izquierda
	    set ll 0
	    set px 0.0
	    foreach i $entradas {
		set il [ string length $i]
		if { $il > $ll} { set ll $il}
		set ix [ font measure FixedFont $i]
		if { $ix > $px} { set px $ix}
	    }
	    set tmp ""
	    foreach i $entradas {
		lappend tmp [ format %-${ll}s $i]
	    }
	    set entradas $tmp

	    if { ( "$que_res" == "CONTOUR_FILL") || ( "$que_res" == "SMOOTH_CONTOUR_FILL") || \
		    ( "$que_res" == "CONTOUR_LINES") || ( "$que_res" == "VECTORS")} {
		set ntpix [ expr $max_v / $n_txt]
	    } else {
		set ntpix [ expr $max_v / ( $n_txt + 1.0)]
	    }
	    set txi_linea $xo
	    set txo_linea [ expr $txi_linea + 4.0]
	    # fallo del canvas, el texto lo alinea centrado, ponga lo que se ponga
	    #set txi_texto [ expr $txo_linea + 4.0]
	    #set txi_texto [ expr ( $PLegendPriv(CanvasWidth) - $txo_linea) / 3.0 + $txo_linea]
	    set txi_texto [ expr $txo_linea + 2.0 + $px / 2.0]
	    set tyi_linea $yi
	    set tyo_linea $yi
	    # como es centered, no hace falta esto:
	    #set tyi_texto [ expr $yi + 3.0]
	    if { ( "$que_res" == "CONTOUR_FILL") || ( "$que_res" == "SMOOTH_CONTOUR_FILL") || \
		    ( "$que_res" == "CONTOUR_LINES") || ( "$que_res" == "VECTORS") } {
		set tyi_texto $yi
	    } else {
		set tyi_texto [ expr $yi + $ntpix * 0.5] 
	    }


	    set x_sep [ expr $PLegendPriv(CanvasWidth) / 3.0]
	    $cv create line [ expr round( $x_sep)] 1 [ expr round( $x_sep + $x_sep)] 1 -fill black
	    if { ( "$que_res" == "CONTOUR_FILL") || ( "$que_res" == "SMOOTH_CONTOUR_FILL") || \
		    ( "$que_res" == "CONTOUR_RANGES") || ( "$que_res" == "ISO_SURFACES")} {
		for { set i 1.0} { [ expr round( $i)] <= $n_col} { set i [ expr $i + $incr_col]} {
		    $cv create rectangle [ expr round( $xi)] [ expr $PLegendPriv(CanvasHeight) - round( $yi)] \
			    [ expr round( $xo)] [ expr $PLegendPriv(CanvasHeight) - round( $yo)] \
			    -fill [ lindex $lst_colores [ expr round( $i)]] -outline ""
		    set yi [ expr $yi + $npix]
		    set yo [ expr $yo + $npix]
		}
	    } else {
		for { set i 1.0} { [ expr round( $i)] <= $n_col} { set i [ expr $i + $incr_col]} {
		    $cv create line [ expr round( $xi)] [ expr $PLegendPriv(CanvasHeight) - round( $yi)] \
			    [ expr round( $xo)] [ expr $PLegendPriv(CanvasHeight) - round( $yo)] \
			    -fill [ lindex $lst_colores [ expr round( $i)]]
		    set yi [ expr $yi + $npix]
		    set yo [ expr $yo + $npix]
		}
	    }

	    #set itexto 0.0
	    for { set i 0} { $i <= $n_txt} { incr i} {
		#WarnWin "$i - $itexto - $n_txt"
		if { ( "$que_res" == "CONTOUR_FILL") || ( "$que_res" == "SMOOTH_CONTOUR_FILL") } {
		    $cv create line [ expr round( $txi_linea)] \
			    [ expr $PLegendPriv(CanvasHeight) - round( $tyi_linea)] \
			    [ expr round( $txo_linea)] \
			    [ expr $PLegendPriv(CanvasHeight) - round( $tyo_linea)] -fill black
		}
		$cv create text [ expr round( $txi_texto)] \
			[ expr $PLegendPriv(CanvasHeight) - round( $tyi_texto)] \
			-fill black -text [ lindex $entradas $i] -font FixedFont
		#[ lindex $entradas [ expr round( $itexto)]]
		set tyi_linea [ expr $tyi_linea + $ntpix]
		set tyo_linea [ expr $tyo_linea + $ntpix]
		set tyi_texto [ expr $tyi_texto + $ntpix]
		#set itexto [ expr $itexto + $incr_idx_txt]
	    }
	}
    } else {
	# no es contour_fill ni contour_lines ni iso_surfaces
	if { "$que_res" == "LOCAL_AXES"} {
	    set label_txt [ lindex $lst_info 1]
	    $PLegendPriv(CanvasLabel) conf -text $label_txt

	    if { ![ info exists PLegendPriv(ImagenEjesLocales)]} {
		set PLegendPriv(ImagenEjesLocales) [ image create photo -file $GIDDEFAULT/bitmaps/localaxes.gif]
	    }

	    $cv create image [ expr $PLegendPriv(CanvasWidth) / 2] [ expr $PLegendPriv(CanvasHeight) / 2]\
		    -anchor center -image $PLegendPriv(ImagenEjesLocales)
	}
    }
    
}

proc PostLegendResizeCanvas { width height } {
    global GIDDEFAULT PLegendPriv GidPriv

    set PLegendPriv(CanvasWidth) [ expr $width - 10]
    set PLegendPriv(CanvasHeight) [ expr $height - 10 - [ winfo reqheight $PLegendPriv(CanvasLabel)] - 4]
    $PLegendPriv(Canvas) conf -width $PLegendPriv(CanvasWidth) -height $PLegendPriv(CanvasHeight)

    FillPostLegendInfo
}

proc ExistPostLegend { } {
    global PLegendPriv
    set ret 0
    if { [ info exists PLegendPriv(window)] && [ winfo exists $PLegendPriv(window)]} {
	set ret 1
    } else {
	set ret 0
    }
    return $ret
}

proc PostLegendInside { } {
    global PLegendPriv

    .central.s process escape escape escape escape Hardcopy Options ShowLegends yes
    destroy $PLegendPriv(window)
}

proc PostLegend { { w .gid.wPostLegend } } {
    global GIDDEFAULT PLegendPriv

    #toplevel $w

    #set PLegendPriv(w) .gid.wPostLegend

    set PLegendPriv(window) $w

    InitWindow $w [_ "Legend"] PostLegendWindowGeom 
#PostLegend

    wm minsize $w 160 110

    frame $w.f1 -relief ridge -bd 2
    label $w.f1.l 
#-text "|DESPLACEMENTS|"
    canvas $w.f1.c -width 128 -height 200
    set PLegendPriv(CanvasWidth) 128
    set PLegendPriv(CanvasHeight) 200
    grid $w.f1.l -sticky ew -padx 4 -pady 2
    grid $w.f1.c -sticky news -padx 2 -pady 2
    set PLegendPriv(Canvas) $w.f1.c
    set PLegendPriv(CanvasLabel) $w.f1.l

    frame $w.but
    # button $w.but.accept -text [_ "Accept"] -bd 1 -command PostPointAccept
    # button $w.but.original -text [_ "Original"] -bd 1 -command PostPointOriginal
    button $w.but.inside -text [_ "Inside"] -bd 1 -command PostLegendInside
    button $w.but.close -text [_ "Close"] -bd 1 -command "destroy $w"

    # grid $w.but.accept -padx 4 -pady 2 -sticky ew
    # grid $w.but.original -padx 4 -pady 2 -sticky ew
    grid $w.but.inside $w.but.close -padx 4 -pady 2 -sticky ew

    grid $w.f1 -sticky nsew -padx 3 -pady 3
    grid $w.but -sticky nsew -padx 3 -pady 3
# -column 2 -row 0 
    grid columnconfigure $w 0 -weight 1
    grid rowconfigure $w 0 -weight 1

    update
    set width [ winfo reqwidth $w.f1]
    set height [ winfo reqheight $w.f1]
    set PLegendPriv(CanvasWidth) [ expr $width - 10]
    set PLegendPriv(CanvasHeight) [ expr $height - 20 - [ winfo reqheight $PLegendPriv(CanvasLabel)] - 4]
    $PLegendPriv(Canvas) conf -width $PLegendPriv(CanvasWidth) -height $PLegendPriv(CanvasHeight)
    update idletasks

    bind $w.f1 <Configure> "PostLegendResizeCanvas %w %h"
    FillPostLegendInfo

    .central.s process escape escape escape escape Hardcopy Options ShowLegends no

}








