#!/bin/sh
#PostPoint -*- TCL -*- Created: miguel Mar-2001, Modified: miguel ???-2001

#---------------------------------------------------------------
# Point Size Options
#---------------------------------------------------------------

proc FillPostPointOptionsWInfo { { primera_vez 0}} {
    global PPointPriv GidPriv

    if { ![ info exist PPointPriv(AvoidUpdate)]} {
	set PPointPriv(AvoidUpdate) 0
    }
    if { ![ info exist PPointPriv(DoingAccept)]} {
	set PPointPriv(DoingAccept) 0
    }
    
    if { $PPointPriv(AvoidUpdate) } { return }
    
    if { $PPointPriv(DoingAccept) } { return }
    
    if { ![ info exist GidPriv(PostPointOptionsWindow)] || ![winfo exist $GidPriv(PostPointOptionsWindow)]} {
	set w ""
    } else {
	set w $GidPriv(PostPointOptionsWindow)
    }

    set info [ .central.s info post get info_tamanno_punto]
    
    #$w.f1.s conf -state normal -foreground black
    #$w.f1.fe.e conf -state normal -foreground black
    if { "$w" != "" } {
	UpDownEntryConf $w.f1.fe -state normal -foreground black -entry-options "-background $PPointPriv(NormalEntryBackground)"
    }
#-foreground black

    set PPointPriv(Value) [ lindex $info 0]
    set opciones [ lindex $info 1]
    set PPointPriv(MinQuick) [ lindex $opciones 0]
    set PPointPriv(MaxQuick) [ lindex $opciones 1]
    set PPointPriv(StepQuick) [ lindex $opciones 2]
    set PPointPriv(DefaultValue) [ lindex $opciones 3]

    set opciones [ lindex $info 2]
    set PPointPriv(MinNice) [ lindex $opciones 0]
    set PPointPriv(MaxNice) [ lindex $opciones 1]
    set PPointPriv(StepNice) [ lindex $opciones 2]

    set opciones [ lindex $info 3]    
    set PPointPriv(Detail) [ lindex $opciones 0]
    set PPointPriv(DefaultDetail) [ lindex $opciones 1]

    set PPointPriv(NiceValue) [ lindex $info 4]
    set opciones [ lindex $info 5]
    set PPointPriv(MinDetail) [ lindex $opciones 0]
    set PPointPriv(MaxDetail) [ lindex $opciones 1]
    set PPointPriv(StepDetail) [ lindex $opciones 2]
    set PPointPriv(DefaultNiceValue) [ lindex $opciones 3]

    set PPointPriv(MinTexture) $PPointPriv(MinNice)
    set PPointPriv(MaxTexture) $PPointPriv(MaxNice)
    set PPointPriv(StepTexture) $PPointPriv(StepNice)
    
    if { $primera_vez} {
	set PPointPriv(OldValue) $PPointPriv(Value)
	set PPointPriv(OldDetail) $PPointPriv(Detail)
    }
    set PPointPriv(AccValue) $PPointPriv(Value)
    set PPointPriv(AccDetail) $PPointPriv(Detail)
    if { $primera_vez} {
	set PPointPriv(OldNiceValue) $PPointPriv(NiceValue)
    }
    set PPointPriv(AccNiceValue) $PPointPriv(NiceValue)

    set WhichValues [ lindex $info 6]

    set PPointPriv(WhichSize) [ lindex $WhichValues 0]
    if { "$PPointPriv(WhichSize)" == "Variable"} {
	set PPointPriv(SelectedResult) [ list [ lindex $WhichValues 1] [ lindex $WhichValues 2] [ lindex $WhichValues 3] [ lindex $WhichValues 4]]
	set PPointPriv(ResultFactor) [ lindex $WhichValues 5]
    } else {
	set PPointPriv(SelectedResult) ""
	set PPointPriv(ResultFactor) 1.0
    }
    
    if { $primera_vez} {
	set PPointPriv(OldWhichSize) $PPointPriv(WhichSize)
	set PPointPriv(OldSelectedResult) $PPointPriv(SelectedResult)
	set PPointPriv(OldResultFactor) $PPointPriv(ResultFactor)
    }
    set PPointPriv(AccWhichSize) $PPointPriv(WhichSize)
    set PPointPriv(AccSelectedResult) $PPointPriv(SelectedResult)
    set PPointPriv(AccResultFactor) $PPointPriv(ResultFactor)



    set TextureValues [ lindex $info 7]

    set PPointPriv(NumTextures) [ lindex $TextureValues 0]
    set PPointPriv(MinTextures) [ lindex $TextureValues 1]
    set PPointPriv(MaxTextures) [ lindex $TextureValues 2]
    set PPointPriv(TexturesLst) [ lindex $TextureValues 3]

    if { $primera_vez} {
	set PPointPriv(OldNumTextures) $PPointPriv(NumTextures)
	set PPointPriv(OldTexturesLst) $PPointPriv(TexturesLst)
    }
    set PPointPriv(AccNumTextures) $PPointPriv(NumTextures)
    set PPointPriv(AccTexturesLst) $PPointPriv(TexturesLst)

    #$w.f1.s conf -from $PPointPriv(Min$PPointPriv(Detail)) -to $PPointPriv(Max$PPointPriv(Detail)) -resolution $PPointPriv(Step$PPointPriv(Detail))
    #$w.f1.s set $PPointPriv(Value)

    #$w.f2.s conf -from $PPointPriv(MinDetail) -to $PPointPriv(MaxDetail) -resolution $PPointPriv(StepDetail)
    #$w.f2.s set $PPointPriv(NiceValue)
    # para (des)habilitar la barrita del 'nice level'

#     set PerspPriv(DoIt) 0
#     if { $perspmode == "PERSPECTIVE" } {
# 	  set PerspPriv(DoIt) 1
#     }
#     if { $perspmode == "NORMAL" } {
# 	  set PerspPriv(DoIt) 0
# 	  $w.f1.s conf -state disabled -foreground grey60
# 	  $w.f1.e conf -state disabled -foreground grey60
#     }
# 
    if { "$w" != "" } {
	EnableDisablePointDetailOptions
	EnableDisablePointSizeOptions
    }

}

proc PostPointAccept { } {
    global PPointPriv

    set PPointPriv(DoingAccept) 1

    set oldAutomaticRedraw [.central.s info variables AutomaticRedraw]
    .central.s process escape escape escape escape utilities variables \
	    AutomaticRedraw -1 escape escape 
	
    switch "$PPointPriv(Detail)" {
	"Quick" {
	    if { $PPointPriv(Detail) != $PPointPriv(AccDetail)} {
		.central.s process escape escape escape escape Result Options PointDetail $PPointPriv(Detail)
	    }
	}
	"Nice" {
	    ##if { $PPointPriv(NiceValue) != $PPointPriv(AccNiceValue) }
	    if { ( $PPointPriv(Detail) != $PPointPriv(AccDetail)) || ( $PPointPriv(NiceValue) != $PPointPriv(AccNiceValue))} {
		.central.s process escape escape escape escape Result Options PointDetail $PPointPriv(Detail) $PPointPriv(NiceValue)
		set PPointPriv(AccNiceValue) $PPointPriv(NiceValue)
	    }
	}
	"Texture" {
	    # if { ( $PPointPriv(Detail) != $PPointPriv(AccDetail)) || ( $PPointPriv(TextureValue) != $PPointPriv(AccTextureValue))} {
	    # 	  .central.s process escape escape escape escape Result Options PointDetail $PPointPriv(Detail) 1 $PPointPriv(TextureValue)
	    # }
	    # set PPointPriv(AccTextureValue) $PPointPriv(TextureValue)
	    if { ( $PPointPriv(AccNumTextures) != $PPointPriv(NumTextures)) || ( $PPointPriv(AccTexturesLst) != $PPointPriv(TexturesLst))} {
		if { $PPointPriv(NumTextures) && ( [ llength $PPointPriv(TexturesLst)] == $PPointPriv(NumTextures))} {
		    eval [ concat .central.s process escape escape escape escape Result \
			    Options PointDetail $PPointPriv(Detail) $PPointPriv(NumTextures) $PPointPriv(TexturesLst)]
		    set PPointPriv(AccNumTextures) $PPointPriv(NumTextures)
		    set PPointPriv(AccTexturesLst) $PPointPriv(TexturesLst)
		}
	    }
	}
    }

    set PPointPriv(AccDetail) $PPointPriv(Detail)

    switch "$PPointPriv(WhichSize)" {
	"Fixed" {
	    if { ( $PPointPriv(WhichSize) != $PPointPriv(AccWhichSize)) || ( $PPointPriv(Value) != $PPointPriv(AccValue))} {
		.central.s process escape escape escape escape Result Options PointSize fixed $PPointPriv(Value)
		set PPointPriv(AccValue) $PPointPriv(Value)
	    }
	}
	"Variable" {
	    if {  ( $PPointPriv(WhichSize) != $PPointPriv(AccWhichSize)) || \
		    ( $PPointPriv(SelectedResult) != $PPointPriv(AccSelectedResult)) || \
		    ( $PPointPriv(ResultFactor) != $PPointPriv(AccResultFactor)) } {
		if { [ llength $PPointPriv(SelectedResult)] == 4} {
		    .central.s process escape escape escape escape Result Options PointSize variable \
			    [ lindex $PPointPriv(SelectedResult) 0] [ lindex $PPointPriv(SelectedResult) 1] \
			    [ lindex $PPointPriv(SelectedResult) 2] [ lindex $PPointPriv(SelectedResult) 3] $PPointPriv(ResultFactor)
		    set PPointPriv(AccSelectedResult) $PPointPriv(SelectedResult)
		    set PPointPriv(AccResultFactor) $PPointPriv(ResultFactor)
		}
	    }
	}
    }

    set PPointPriv(AccWhichSize) $PPointPriv(WhichSize)

    .central.s process escape escape escape escape utilities variables \
	    AutomaticRedraw $oldAutomaticRedraw escape escape 

    set PPointPriv(DoingAccept) 0

    set old $PPointPriv(DynamicUpdate)
    set PPointPriv(DynamicUpdate) 0
    FillPostPointOptionsWInfo
    set PPointPriv(DynamicUpdate) $old

    .gid.central.s render
}

proc PostPointOriginal { } {
    global PPointPriv
    
    set oldAutomaticRedraw [.central.s info variables AutomaticRedraw]
    .central.s process escape escape escape escape utilities variables \
	    AutomaticRedraw -1 escape escape 

    .central.s process escape escape escape escape Result Options PointDetail $PPointPriv(DefaultDetail) $PPointPriv(DefaultNiceValue)
    .central.s process escape escape escape escape Result Options PointSize fixed $PPointPriv(DefaultValue)

    .central.s process escape escape escape escape utilities variables \
	    AutomaticRedraw $oldAutomaticRedraw escape escape 

    .gid.central.s render    

    set old $PPointPriv(DynamicUpdate)
    set PPointPriv(DynamicUpdate) 0
    FillPostPointOptionsWInfo
    set PPointPriv(DynamicUpdate) $old
}

proc ChangePointSizeScale { name1 name2 op} {
    global PPointPriv GidPriv

    if { $PPointPriv(Value) == $PPointPriv(OldValue)} { return}
    if { "$PPointPriv(Value)" == ""} { return}

    set w $GidPriv(PostPointOptionsWindow)

    set newval $PPointPriv(Value)

    if { ![IsWord real $newval $w " In point size field"] } {
	$w.f1.fe.e sel from 0
	$w.f1.fe.e sel to end
	set PPointPriv(Value) $PPointPriv(OldValue) 
	return error
    }

    if { ( $newval < $PPointPriv(Min$PPointPriv(Detail))) || ( $newval > $PPointPriv(Max$PPointPriv(Detail))) } {
	tk_dialogRAM $w.tempwin [_ "Error window"] \
	    [_ "Value out of limits ( must be between %1\$s and %2\$s in steps of %3\$s)" \
		 $PPointPriv(Min$PPointPriv(Detail)) \
		 $PPointPriv(Max$PPointPriv(Detail)) $PPointPriv(Step$PPointPriv(Detail))]
	error 0 [_ "OK"]
	$w.f1.fe.e sel from 0
	$w.f1.fe.e sel to end
	set PPointPriv(Value) $PPointPriv(OldValue) 
	return error
    }

    set pp [ expr ($newval - $PPointPriv(Min$PPointPriv(Detail))) / $PPointPriv(Step$PPointPriv(Detail))]
    set ppi [ expr int($pp + 0.5)]
    if { [ expr abs( $ppi - $pp)] > 1e-5 } {
	tk_dialogRAM $w.tempwin [_ "Error window"] \
	    [_ "Value not %1\$s %2\$s correct ( must be between %3\$s and %4\$s in steps of %5\$s)" \
		 $pp $ppi $PPointPriv(Min$PPointPriv(Detail)) \
		 $PPointPriv(Max$PPointPriv(Detail)) $PPointPriv(Step$PPointPriv(Detail))]
	error 0 [_ "OK"]
	$w.f1.fe.e sel from 0
	$w.f1.fe.e sel to end

	set PPointPriv(Value) $PPointPriv(OldValue) 
	return error
    }

    set PPointPriv(OldValue) $PPointPriv(Value) 

    #set PPointPriv(Value) $newval

    # if { $PPointPriv(Value) != [$w.f1.s get ] } {
    # 	  $w.f1.s set $PPointPriv(Value)
    # }
    if { $PPointPriv(DynamicUpdate) } { PostPointAccept}
}

proc EnableDisablePointDetailOptions { } {
    global GidPriv PPointPriv

    set w $GidPriv(PostPointOptionsWindow)

    if { $PPointPriv(Detail) == "Nice"} {
	$w.f2.l conf -foreground black
	$w.f1.rd2 conf -state normal
	#$w.f2.s conf -state normal -foreground black
	UpDownEntryConf $w.f2.s -state normal -foreground black -entry-options "-background $PPointPriv(NormalEntryBackground)"
	set PPointPriv(WhichSize) $PPointPriv(OldWhichSize)
    } else {
	$w.f2.l conf -foreground grey60
	$w.f1.rd2 conf -state disabled
	UpDownEntryConf $w.f2.s -state disabled -foreground grey60 -entry-options "-background $PPointPriv(DisabledEntryBackground)"
	#$w.f2.s conf -state disabled -foreground grey60
	set PPointPriv(OldWhichSize) $PPointPriv(WhichSize)
	set PPointPriv(WhichSize) Fixed
    }

    if { $PPointPriv(Detail) == "Texture"} {
	#$w.f3.t1.e conf -state normal -foreground black -background $PPointPriv(NormalEntryBackground)
	#$w.f3.t1.b conf -state normal
	$w.f3.l conf -foreground black
	UpDownEntryConf $w.f3.e -state normal -foreground black -entry-options "-background $PPointPriv(NormalEntryBackground)"
	$w.f3.seltex conf -state normal
    } else {
	#$w.f3.t1.e conf -state disabled -foreground grey60 -background $PPointPriv(DisabledEntryBackground)
	#$w.f3.t1.b conf -state disabled
	$w.f3.l conf -foreground grey60
	UpDownEntryConf $w.f3.e -state disabled -foreground grey60 -entry-options "-background $PPointPriv(DisabledEntryBackground)"
	$w.f3.seltex conf -state disabled
    }
}

proc ChangePointDetail { n1 n2 op} {
    global GidPriv PPointPriv

    if { $PPointPriv(Detail) == $PPointPriv(OldDetail)} { return}

    set PPointPriv(OldDetail) $PPointPriv(Detail) 

    if { $PPointPriv(Value) < $PPointPriv(Min$PPointPriv(Detail))} {
	set PPointPriv(Value) $PPointPriv(Min$PPointPriv(Detail))
    }

    if { $PPointPriv(Value) > $PPointPriv(Max$PPointPriv(Detail))} {
	set PPointPriv(Value) $PPointPriv(Max$PPointPriv(Detail))
    }

    set w $GidPriv(PostPointOptionsWindow)

    #$w.f1.s conf -from $PPointPriv(Min$PPointPriv(Detail)) -to $PPointPriv(Max$PPointPriv(Detail)) -resolution $PPointPriv(Step$PPointPriv(Detail))
    #$w.f1.s set $PPointPriv(Value)

    if { "$PPointPriv(Detail)" == "Texture"} {
	if { $PPointPriv(NumTextures) < $PPointPriv(MinTextures)} {
	    set PPointPriv(NumTextures) $PPointPriv(MinTextures)
	}
	if { $PPointPriv(NumTextures) > $PPointPriv(MaxTextures)} {
	    set PPointPriv(NumTextures) $PPointPriv(MaxTextures)
	}
    }

    if { ( "$PPointPriv(Detail)" != "Texture") || \
	    ( $PPointPriv(NumTextures) && ( [ llength $PPointPriv(TexturesLst)] == $PPointPriv(NumTextures)))} {
	if { $PPointPriv(DynamicUpdate)} { PostPointAccept}
    }

    EnableDisablePointDetailOptions
}

proc EnableDisablePointSizeOptions { } {
    global GidPriv PPointPriv

    set w $GidPriv(PostPointOptionsWindow)

    if { $PPointPriv(WhichSize) == "Fixed"} {
	UpDownEntryConf $w.f1.fe -state normal -foreground black \
		-entry-options "-background $PPointPriv(NormalEntryBackground)"
	$w.f1.t conf -foreground grey60
	$w.f1.selres conf -state disabled
	$w.f1.ff.l conf -foreground grey60
	$w.f1.ff.factor conf -foreground grey60 -background $PPointPriv(DisabledEntryBackground)
    } else { # is "Variable"
	UpDownEntryConf $w.f1.fe -state disabled -foreground grey60 \
		-entry-options "-background $PPointPriv(DisabledEntryBackground)"
	$w.f1.t conf -foreground black
	$w.f1.selres conf -state normal
	$w.f1.ff.l conf -foreground black
	$w.f1.ff.factor conf -foreground black -background $PPointPriv(NormalEntryBackground)
    }
}

proc ChangePointWhichSize { n1 n2 op} {
    global GidPriv PPointPriv

    if { $PPointPriv(WhichSize) == $PPointPriv(OldWhichSize)} { return}

    if {  "$PPointPriv(SelectedResult)" == ""} {
	set PPointPriv(SelectedResult) $PPointPriv(OldSelectedResult)
	set PPointPriv(OldSelectedResult) ""
	ChangeSelectedResult PPointPriv OldSelectedResult w
    }

    set PPointPriv(OldWhichSize) $PPointPriv(WhichSize)

    set w $GidPriv(PostPointOptionsWindow)

    if { $PPointPriv(DynamicUpdate) && ( "$PPointPriv(SelectedResult)" != "")} { PostPointAccept}

    EnableDisablePointSizeOptions
}

proc ChangePointDetailScale { name1 name2 op} {
    global PPointPriv GidPriv

    if { $PPointPriv(NiceValue) == $PPointPriv(OldNiceValue)} { return}
    if { "$PPointPriv(NiceValue)" == ""} { return}

    set w $GidPriv(PostPointOptionsWindow)

    set newval $PPointPriv(NiceValue)

    if { ( $newval < $PPointPriv(MinDetail)) || ( $newval > $PPointPriv(MaxDetail)) } {
	tk_dialogRAM $w.tempwin [_ "Error window"] \
	    [_ "Value out of limits ( must be between %1\$s and %2\$s in steps of %3\$s)" \
		 $PPointPriv(MinDetail) $PPointPriv(MaxDetail) $PPointPriv(StepDetail)]
	error 0 [_ "OK"]
	$w.f1.fe.e sel from 0
	$w.f1.fe.e sel to end
	set PPointPriv(Value) $PPointPriv(OldValue) 
	return error
    }

    set PPointPriv(OldNiceValue) $PPointPriv(NiceValue) 
    if { $PPointPriv(DynamicUpdate) } { PostPointAccept}    
}

proc PostPointOptionsTextureFile { w varname} {
    upvar $varname destination
    set destination [Browser-ramR file read $w [_ "Point texture file"]]
}

proc PostPointOptionsIncrementaTamanno { paso} {
    global PPointPriv

    set tmp [ expr $PPointPriv(Value) + $paso * $PPointPriv(Step$PPointPriv(Detail))]
    set pp [ expr ($tmp - $PPointPriv(Min$PPointPriv(Detail))) / $PPointPriv(Step$PPointPriv(Detail))]
    set ppi [ expr int($pp + 0.5)]
    if { [ expr abs( $ppi - $pp)] > 1e-5 } {
	set tmp [ expr double( $ppi) * $PPointPriv(Step$PPointPriv(Detail)) + $PPointPriv(Min$PPointPriv(Detail))]
    }

    if { $tmp > $PPointPriv(Max$PPointPriv(Detail))} {
	set tmp $PPointPriv(Max$PPointPriv(Detail))
	bell
    }

    set PPointPriv(Value) [ format %g $tmp]
}

proc PostPointOptionsDecrementaTamanno { paso} {
    global PPointPriv

    set tmp [ expr $PPointPriv(Value) - $paso * $PPointPriv(Step$PPointPriv(Detail))]
    set pp [ expr ($tmp - $PPointPriv(Min$PPointPriv(Detail))) / $PPointPriv(Step$PPointPriv(Detail))]
    set ppi [ expr int($pp + 0.5)]
    if { [ expr abs( $ppi - $pp)] > 1e-5 } {
	set tmp [ expr double( $ppi) * $PPointPriv(Step$PPointPriv(Detail)) + $PPointPriv(Min$PPointPriv(Detail))]
    }

    if { $tmp < $PPointPriv(Min$PPointPriv(Detail))} {
	set tmp $PPointPriv(Min$PPointPriv(Detail))
	bell
    }

    set PPointPriv(Value) [ format %g $tmp]
}

proc PostPointOptionsIncrementaBonito { } {
    global PPointPriv

    set tmp [ expr $PPointPriv(NiceValue) + $PPointPriv(StepDetail)]
    if { $tmp > $PPointPriv(MaxDetail)} {
	set tmp $PPointPriv(MaxDetail)
	bell
    }

    set PPointPriv(NiceValue) [ format %g $tmp]
}

proc PostPointOptionsDecrementaBonito { } {
    global PPointPriv

    set tmp [ expr $PPointPriv(NiceValue) - $PPointPriv(StepDetail)]
    if { $tmp < $PPointPriv(MinDetail)} {
	set tmp $PPointPriv(MinDetail)
	bell
    }

    set PPointPriv(NiceValue) [ format %g $tmp]
}

proc PostPointOptionsIncrementaNumTex { } {
    global PPointPriv

    set tmp [ expr $PPointPriv(NumTextures) + 1]
    if { $tmp > $PPointPriv(MaxTextures)} {
	set tmp $PPointPriv(MaxTextures)
	bell
    }

    set PPointPriv(NumTextures) [ format %g $tmp]
}

proc PostPointOptionsDecrementaNumTex { } {
    global PPointPriv

    set tmp [ expr $PPointPriv(NumTextures) - 1]
    if { $tmp < $PPointPriv(MinTextures)} {
	set tmp $PPointPriv(MinTextures)
	bell
    }

    set PPointPriv(NumTextures) [ format %g $tmp]
}

proc PostPointQuickOptions { w} {
    global PPointPriv
WarnWin hola
    
}

proc PostFillSelecResultComponent { } {
    global GidPriv PostSelecResultComponent

    set w $GidPriv(PostSelecResultComponentWindow)
    $w.r.a.cb listbox del 0 end

    set all [.central.s info post get all_analisis]
    if { "$all" == ""} { 
	return
    }

    foreach j $all {
	regsub -all {_} $j { } analisis
	$w.r.a.cb add $analisis
    }

    set cur [ lindex $PostSelecResultComponent(Current) 0]
    if { "$cur" == "" } {
	regsub -all {_} [ lindex $all 0] { } cur2
    } else {
	regsub -all {_} $cur { } cur2
    }
    set PostSelecResultComponent(Analysis) $cur2
}

proc PostFillChangedAnalysis { name1 name2 op} {
    global GidPriv PostSelecResultComponent

    set w $GidPriv(PostSelecResultComponentWindow)

    $w.r.s.cb listbox del 0 end

    regsub -all { } $PostSelecResultComponent(Analysis) {_} analisis
    set all [.central.s info post get all_steps $analisis]
    if { "$all" == ""} { 
	return
    }
    foreach j $all {
	$w.r.s.cb add $j
    }

    set cur [ lindex $PostSelecResultComponent(Current) 1]
    if { "$cur" == "" } {
    	  set cur [ lindex $all 0]
    }
    set PostSelecResultComponent(Step) $cur
}

proc PostFillChangedStep { name1 name2 op} {
    global GidPriv PostSelecResultComponent

    set w $GidPriv(PostSelecResultComponentWindow)

    $w.r.r.cb listbox del 0 end

    regsub -all { } $PostSelecResultComponent(Analysis) {_} analisis
    set all [.central.s info post get results_list CONTOUR_FILL $analisis $PostSelecResultComponent(Step)]
    if { "$all" == ""} { 
	return
    }
    foreach j $all {
	regsub -all {_} $j { } resultado
	$w.r.r.cb add $resultado
    }

    set cur [ lindex $PostSelecResultComponent(Current) 2]
    if { "$cur" == "" } {
	regsub -all {_} [ lindex $all 0] { } cur2
    } else {
	regsub -all {_} $cur { } cur2
    }
    set PostSelecResultComponent(Result) $cur2
}

proc PostFillChangedResult { name1 name2 op} {
    global GidPriv PostSelecResultComponent

    set w $GidPriv(PostSelecResultComponentWindow)

    $w.r.c.cb listbox del 0 end

    regsub -all { } $PostSelecResultComponent(Analysis) {_} analisis
    regsub -all { } $PostSelecResultComponent(Result) {_} resultado
    set all [ .central.s info post get components_list CONTOUR_FILL $resultado $analisis $PostSelecResultComponent(Step)]
    if { "$all" == ""} { 
	return
    }
    foreach j $all {
	regsub -all {_} $j { } componente
	$w.r.c.cb add $componente
    }

    set cur [ lindex $PostSelecResultComponent(Current) 3]
    if { "$cur" == "" } {
	regsub -all {_} [ lindex $all 0] { } cur2
    } else {
	regsub -all {_} $cur { } cur2
    }
    set PostSelecResultComponent(Component) $cur2
}

proc PostSelecResultComponentClose { w } {
    global PostSelecResultComponent

    catch {
	trace vdelete PostSelecResultComponent(Analysis) w PostFillChangedAnalysis
	trace vdelete PostSelecResultComponent(Result) w PostFillChangedResult
    }

    destroy $w
}

proc PostSelecResultComponentAccept { w varname} {
    global PostSelecResultComponent
    upvar $varname destination

    regsub -all { } $PostSelecResultComponent(Analysis) {_} analisis
    regsub -all { } $PostSelecResultComponent(Result) {_} resultado
    regsub -all { } $PostSelecResultComponent(Component) {_} componente
    set destination [ list $analisis \
	    $PostSelecResultComponent(Step) $resultado $componente]

    PostSelecResultComponentClose $w
}

proc PostSelecResultComponent { w varname} {
    global GidPriv PostSelecResultComponent
    upvar $varname destination

    if { [ winfo exist $w] } { 
	raise $w
	return
    }
    toplevel $w
    set parent_w [winfo toplevel [winfo parent $w]]
    wm transient $w $parent_w

    set GidPriv(PostSelecResultComponentWindow) $w

    catch {
	trace vdelete PostSelecResultComponent(Analysis) w PostFillChangedAnalysis
	trace vdelete PostSelecResultComponent(Step) w PostFillChangedStep
	trace vdelete PostSelecResultComponent(Result) w PostFillChangedResult
    }

    frame $w.r -relief ridge -bd 2

    frame $w.r.a -bd 0
    label $w.r.a.l -text [_ "Analysis"] 
    combobox $w.r.a.cb -textvariable PostSelecResultComponent(Analysis) -editable 0 -width 15
    grid $w.r.a.l -sticky e -pady 2 -padx 2
    grid $w.r.a.cb -row 0 -column 1 -sticky w -pady 2 -padx 2

    frame $w.r.s -bd 0
    label $w.r.s.l -text [_ "Step"]
    combobox $w.r.s.cb -textvariable PostSelecResultComponent(Step) -editable 0 -width 5
    grid $w.r.s.l -sticky e -pady 2 -padx 2
    grid $w.r.s.cb -row 0 -column 1 -sticky w -pady 2 -padx 2

    frame $w.r.r -bd 0
    label $w.r.r.l -text [_ "Result"]
    combobox $w.r.r.cb -textvariable PostSelecResultComponent(Result) -editable 0
    grid $w.r.r.l -sticky e -pady 2 -padx 2
    grid $w.r.r.cb -row 0 -column 1 -sticky w -pady 2 -padx 2

    frame $w.r.c -bd 0
    label $w.r.c.l -text [_ "Component"]
    combobox $w.r.c.cb -textvariable PostSelecResultComponent(Component) -editable 0
    grid $w.r.c.l -sticky e -pady 2 -padx 2
    grid $w.r.c.cb -row 0 -column 1 -sticky w -pady 2 -padx 2

    grid $w.r.a -sticky ew
    grid $w.r.s -sticky ew
    grid $w.r.r -sticky ew
    grid $w.r.c -sticky ew

    set def_back [ $w cget -background]
    frame $w.but -bd 0 -background [ CCColorActivo $def_back]
    button $w.but.accept -text [_ "Apply"] -bd 1 -command "PostSelecResultComponentAccept $w $varname"
    button $w.but.close -text [_ "Close"] -bd 1 -command "PostSelecResultComponentClose $w"
    grid $w.but.accept -padx 4 -pady 10 -sticky ew
    grid $w.but.close -row 0 -column 1 -padx 4 -pady 10 -sticky ew

    grid $w.r -sticky news
    grid $w.but -sticky wes

    trace variable PostSelecResultComponent(Analysis) w PostFillChangedAnalysis
    trace variable PostSelecResultComponent(Step) w PostFillChangedStep
    trace variable PostSelecResultComponent(Result) w PostFillChangedResult

    set PostSelecResultComponent(Current) $destination

    PostFillSelecResultComponent

    wm withdraw $w
    update idletasks

    # # centrada en la pantalla
    # set x [expr {[winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
    # 	      - [winfo vrootx [winfo parent $w]]}]
    # set y [expr {[winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
    # 	      - [winfo vrooty [winfo parent $w]]}]
    set w2 [ expr [ winfo reqwidth $w] / 2]
    set h2 [ expr [ winfo reqheight $w] / 2]
    set x [ expr [ winfo pointerx $parent_w] - $w2]
    set y [ expr [ winfo pointery $parent_w] - $h2]

    # miramos si sale la X
    set off_scr_x [ expr ( $x + $w2 + $w2) - [ winfo screenwidth $w]]
    if { $off_scr_x >= 0} {
	set x [ expr $x - $off_scr_x - 10]
    }
    if { $x < 0} { set x 0}
    # miramos si sale la Y
    set off_scr_y [ expr ( $y + $h2 + $h2) - [ winfo screenheight $w]]
    if { $off_scr_y >= 0} {
	set y [ expr $y - $off_scr_y - 20]
    }
    if { $y < 0} { set y 0}
    wm geom $w +$x+$y
    wm deiconify $w

    bind $w <Escape> "PostSelecResultComponentClose $w"

}

proc ChangeSelectedResult { name1 name2 op } {
    global PPointPriv

    if { $PPointPriv(SelectedResult) == $PPointPriv(OldSelectedResult)} { return}

    if { "$PPointPriv(SelectedResult)" != ""} {
	set PPointPriv(OldSelectedResult) $PPointPriv(SelectedResult)
    }

    set wtext $PPointPriv(PostPointOptionsResultTextW)

    if { "$wtext" != ""} {
	$wtext conf -state normal
	$wtext del 0.0 end
	if { [llength $PPointPriv(SelectedResult)] == 4} {
	    regsub -all {_} [ lindex $PPointPriv(SelectedResult) 0] { } analisis
	    set paso [ lindex $PPointPriv(SelectedResult) 1]
	    regsub -all {_} [ lindex $PPointPriv(SelectedResult) 2] { } resultado
	    regsub -all {_} [ lindex $PPointPriv(SelectedResult) 3] { } componente
	    $wtext ins end "$analisis\n$paso\n$resultado\n$componente"
	}
	$wtext conf -state disabled
    }
}

proc PostPointOptionsSelectResult { w { wtext ""}} {
    global PPointPriv

    set PPointPriv(PostPointOptionsResultTextW) $wtext
#    set PPointPriv(SelectedResult) ""
    PostSelecResultComponent $w.sr PPointPriv(SelectedResult)
    tkwait window $w.sr

    # sugiere un factor
    if { [llength $PPointPriv(SelectedResult)] == 4} {
	set analisis [ lindex $PPointPriv(SelectedResult) 0]
	set paso [ lindex $PPointPriv(SelectedResult) 1]
	set resultado [ lindex $PPointPriv(SelectedResult) 2]
	set componente [ lindex $PPointPriv(SelectedResult) 3]
	set PPointPriv(ResultFactor) [ .central.s info post get info_factor_tamanno_punto $analisis $paso $resultado $componente]

	if { $PPointPriv(DynamicUpdate) } { PostPointAccept}
    }
}


proc PostSelecTexturesClose { w } {
    global PostSelecTextures

    #catch {
    #	 trace vdelete PostSelecResultComponent(Analysis) w PostFillChangedAnalysis
    #	 trace vdelete PostSelecResultComponent(Result) w PostFillChangedResult
    #}

    destroy $w
}

proc PostSelecTexturesAccept { w varname} {
    global PostSelecTextures
    upvar $varname destination

    #regsub -all { } $PostSelecResultComponent(Analysis) {_} analisis
    #regsub -all { } $PostSelecResultComponent(Result) {_} resultado
    #regsub -all { } $PostSelecResultComponent(Component) {_} componente
    #set $PostSelecResultComponent(ReturnVariable) [ list $analisis \
    #	     $PostSelecResultComponent(Step) $resultado $componente]
    
    set vacios 0
    for { set it 0 } { $it < $PostSelecTextures(Num)} { incr it} {
	if { "$PostSelecTextures($it)" == ""} {
	    incr vacios
	}
    }
    if { $vacios != 0} {
	if { $vacios == 1} {
	    set msg [_ "Sorry, but there is one entry with no image file."]
	} else {
	    set msg [_ "Sorry, but there is %s entries with no image files." $vacios]
	}
	WarnWin $msg $w
	return
    }

    set destination ""
    for { set it 0 } { $it < $PostSelecTextures(Num)} { incr it} {
	lappend destination $PostSelecTextures($it)
    }

    PostSelecTexturesClose $w
}

proc PostSelecTextures { w num_entries varname } {
    global GidPriv PostSelecTextures
    upvar $varname text_lst

    if { [ winfo exist $w] } { 
	raise $w
	return
    }
    set PA_OpenFolderImage [ image create bitmap -data {
#define folder_open_width 20
#define folder_open_height 16
	static unsigned char folder_open_bits[] = {
	    0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x04, 0x00,
	    0xf8, 0x83, 0x00, 0xf8, 0xff, 0x00, 0xf8, 0xff, 0x00, 0x38, 0x00, 0x08,
	    0x38, 0x00, 0x04, 0x18, 0x00, 0x04, 0x18, 0x00, 0x02, 0x08, 0x00, 0x02,
	    0x08, 0x00, 0x01, 0x00, 0x00, 0x01, 0xf8, 0xff, 0x00, 0x00, 0x00, 0x00
	};
    } -maskdata {
	#define folder_open_msk_width 20
	#define folder_open_msk_height 16
	static unsigned char folder_open_msk_bits[] = {
	    0x00, 0x00, 0x00, 0xf0, 0x01, 0x00, 0xf8, 0x03, 0x00, 0xfc, 0x07, 0x00,
	    0x04, 0xbc, 0x00, 0x04, 0x80, 0x00, 0x04, 0x80, 0x00, 0xc4, 0xff, 0x0f,
	    0xc4, 0xff, 0x07, 0x64, 0x77, 0x07, 0xe4, 0xdd, 0x03, 0x74, 0x77, 0x03,
	    0xd4, 0xdd, 0x01, 0x7c, 0x77, 0x01, 0xfc, 0xff, 0x00, 0x00, 0x00, 0x00
	};
    } -background yellow -foreground black ]
	
    toplevel $w
    set parent_w [winfo toplevel [winfo parent $w]]
    wm transient $w $parent_w
    wm withdraw $w

    set GidPriv(PostSelecTexturesWindow) $w
    #set PostSelecTextures(ReturnVariable) $varname
    
    set c [ CreateScrolledCanvas $w.c]
    $c conf -width 300 -height 200

    set min [ llength $text_lst]
    if { $num_entries < $min} {
	set min $num_entries
    }

    for { set it 0 } { $it < $min} { incr it} {
	set PostSelecTextures($it) [ lindex $text_lst $it]
    }
    for { set it $min} { $it < $num_entries} { incr it} {
	set PostSelecTextures($it) ""
    }

    set PostSelecTextures(Num) $num_entries
    for { set it 0 } { $it < $num_entries} { incr it} {
	frame $c.t$it -bd 0
	label $c.t$it.l -text "Tex [ format %2d [ expr $it + 1]]:"

	button $c.t$it.b -bd 1 -image $PA_OpenFolderImage -command "PostPointOptionsTextureFile $w PostSelecTextures($it)"
	entry $c.t$it.e -relief sunken -textvariable PostSelecTextures($it) -width 50
    
	grid $c.t$it.l $c.t$it.b $c.t$it.e -sticky we -padx 0
	grid rowconfigure $c.t$it 0 -weight 1
	grid columnconfigure $c.t$it 0 -weight 1
	
	AddToScrolledCanvas $w.c $c.t$it
    }

    set def_back [ $w cget -background]
    frame $w.but -bd 0 -background [ CCColorActivo $def_back]
    button $w.but.accept -text [_ "Apply"] -bd 1 -command "PostSelecTexturesAccept $w $varname"
    button $w.but.close -text [_ "Close"] -bd 1 -command "PostSelecTexturesClose $w"
    grid $w.but.accept -padx 4 -pady 10 -sticky ew
    grid $w.but.close -row 0 -column 1 -padx 4 -pady 10 -sticky ew

    grid $w.c -sticky news
    grid $w.but -sticky wes

    # trace variable PostSelecResultComponent(Analysis) w PostFillChangedAnalysis
    # trace variable PostSelecResultComponent(Step) w PostFillChangedStep
    # trace variable PostSelecResultComponent(Result) w PostFillChangedResult
    # 
    # PostFillSelecResultComponent

    #wm withdraw $w
    update idletasks
    
    set w2 [ expr [ winfo reqwidth $w] / 2]
    set h2 [ expr [ winfo reqheight $w] / 2]
    set x [ expr [ winfo pointerx $parent_w] - $w2]
    set y [ expr [ winfo pointery $parent_w] - $h2]

    # miramos si sale la X
    set off_scr_x [ expr ( $x + $w2 + $w2) - [ winfo screenwidth $w]]
    if { $off_scr_x >= 0} {
	set x [ expr $x - $off_scr_x - 10]
    }
    if { $x < 0} { set x 0}
    # miramos si sale la Y
    set off_scr_y [ expr ( $y + $h2 + $h2) - [ winfo screenheight $w]]
    if { $off_scr_y >= 0} {
	set y [ expr $y - $off_scr_y - 20]
    }
    if { $y < 0} { set y 0}

    wm geom $w +$x+$y
    wm deiconify $w

    bind $w <Escape> "PostSelecTexturesClose $w"
}

proc PostPointOptionsSelectTextures { w} {
    global PPointPriv

    ##set PPointPriv(SelectedResult) ""
    PostSelecTextures $w.st $PPointPriv(NumTextures) PPointPriv(TexturesLst)
    #PPointPriv(SelectedResult)
    tkwait window $w.st

    if { $PPointPriv(DynamicUpdate) } { PostPointAccept}
}

proc PostPointOptionsClose { w} {
    global PPointPriv

    catch {
	trace vdelete PPointPriv(Value) w ChangePointSizeScale
	trace vdelete PPointPriv(Detail) w ChangePointDetail
	trace vdelete PPointPriv(NiceValue) w ChangePointDetailScale
	trace vdelete PPointPriv(WhichSize) w ChangePointWhichSize
	trace vdelete PPointPriv(SelectedResult) w ChangeSelectedResult
    }

    destroy $w
}

proc PostPointOptions { { w .gid.wPostPointOptions } } {
    global PPointPriv

    InitWindow $w [_ "Point Options"] PostPointOptionsWindowGeom PostPointOptions

    set PPointPriv(DynamicUpdate) 1

    catch {
	trace vdelete PPointPriv(Value) w ChangePointSizeScale
	trace vdelete PPointPriv(Detail) w ChangePointDetail
	trace vdelete PPointPriv(NiceValue) w ChangePointDetailScale
	trace vdelete PPointPriv(WhichSize) w ChangePointWhichSize
	trace vdelete PPointPriv(SelectedResult) w ChangeSelectedResult
    }

    frame $w.f0
    radiobutton $w.f0.rd1 -text [_ "Quick"] -value Quick -variable PPointPriv(Detail)
    radiobutton $w.f0.rd2 -text [_ "Nice"] -value Nice -variable PPointPriv(Detail)
    radiobutton $w.f0.rd3 -text [_ "Texture"] -value Texture -variable PPointPriv(Detail)
    checkbutton $w.f0.din -text [_ "Dynamic update"]  -variable PPointPriv(DynamicUpdate)
    grid $w.f0.rd1 $w.f0.rd2 $w.f0.rd3 -sticky w -padx 1
    grid $w.f0.din -row 0 -column 3 -sticky e -padx 3
    grid rowconfigure $w.f0 0 -weight 1

    frame $w.f1 -relief ridge -bd 2
    label $w.f1.l -text [_ "Point size:"]
    radiobutton $w.f1.rd1 -text [_ "Fixed size:"] -value Fixed -variable PPointPriv(WhichSize)
    radiobutton $w.f1.rd2 -text [_ "Variable size:"] -value Variable -variable PPointPriv(WhichSize)

    UpDownEntry $w.f1.fe -variable PPointPriv(Value) \
	    -up-command "PostPointOptionsIncrementaTamanno 1" \
	    -down-command "PostPointOptionsDecrementaTamanno 1" \
	    -entry-options "-relief sunken -width 5" \
	    -button-options "-bd 1" \
	    -show-double-buttons  \
	    -up-up-command "PostPointOptionsIncrementaTamanno 5" \
	    -down-down-command "PostPointOptionsDecrementaTamanno 5" \
	    -up-color red -down-color blue\
	    -up-up-color red -down-down-color blue \

    text $w.f1.t -relief sunken -bd 1 -width 15 -height 4 -state disabled
    button $w.f1.selres -text [_ "Select result ->"] -bd 1 \
	    -command "PostPointOptionsSelectResult $w $w.f1.t"
    set PPointPriv(PostPointOptionsResultTextW) $w.f1.t
    frame $w.f1.ff -bd 0
    label $w.f1.ff.l -text [_ "factor:"]
    entry $w.f1.ff.factor -relief sunken -textvariable PPointPriv(ResultFactor) -width 5
    set PPointPriv(NormalEntryBackground) [ $w.f1.ff.factor cget -background]
    set PPointPriv(DisabledEntryBackground) [ $w cget -background]
    grid $w.f1.ff.l -sticky es
    grid $w.f1.ff.factor -sticky wes -row 0 -column 1
    grid rowconfigure $w.f1.ff 0 -weight 1
    grid columnconfigure $w.f1.ff 1 -weight 1

    grid $w.f1.l -sticky w -row 0 -column 0
    grid $w.f1.rd1 -sticky w
    grid $w.f1.fe -sticky w -row 1 -column 1
    grid $w.f1.rd2 -sticky wn
    grid $w.f1.t -sticky nsew -row 2 -column 1 -rowspan 3 -padx 4 -pady 2
    grid $w.f1.selres -sticky en -row 3 -column 0 -padx 4
    grid $w.f1.ff -sticky wes -row 4 -column 0 -padx 4 -pady 2
    grid rowconfigure $w.f1 2 -weight 1
    grid columnconfigure $w.f1 1 -weight 1
    #grid rowconfigure $w.f1 4 -weight 1
    #grid columnconfigure $w.f1 0 -weight 1

    frame $w.f2 -relief ridge -bd 2
    label $w.f2.l -text [_ "Nice Detail:"]

    UpDownEntry $w.f2.s -variable PPointPriv(NiceValue) \
	    -up-command PostPointOptionsIncrementaBonito \
	    -down-command PostPointOptionsDecrementaBonito \
	    -entry-options "-relief sunken -width 5" \
	    -button-options "-bd 1" \
	    -up-color red -down-color blue\

    grid $w.f2.l -sticky w -padx 2
    grid $w.f2.s -row 0 -column 1 -sticky we -padx 4
    grid rowconfigure $w.f2 0 -weight 1
    grid columnconfigure $w.f2 1 -weight 1

    frame $w.f3 -relief ridge -bd 2

    label $w.f3.l -text [_ "Number of textures:"]

    UpDownEntry $w.f3.e -variable PPointPriv(NumTextures) \
	    -up-command PostPointOptionsIncrementaNumTex \
	    -down-command PostPointOptionsDecrementaNumTex \
	    -entry-options "-relief sunken -width 5" \
	    -button-options "-bd 1" \
	    -up-color red -down-color blue\
    
    button $w.f3.seltex -text [_ "Pick images >>>"] -bd 1 \
	    -command "PostPointOptionsSelectTextures $w"

    grid $w.f3.l -sticky w -padx 2
    grid $w.f3.e -sticky e -padx 4
    grid $w.f3.seltex -sticky se -padx 4 -pady 2
    grid rowconfigure $w.f3 1 -weight 1
    grid rowconfigure $w.f3 2 -weight 1
    grid columnconfigure $w.f3 0 -weight 1


    set def_back [ $w cget -background]
    frame $w.but -bd 0 -background [ CCColorActivo $def_back]
    button $w.but.accept -text [_ "Apply"] -bd 1 -command PostPointAccept
    button $w.but.original -text [_ "Original"] -bd 1 -command PostPointOriginal
    button $w.but.close -text [_ "Close"] -bd 1 -command "PostPointOptionsClose $w"

    grid $w.but.accept -row 0 -column 0 -padx 4 -pady 10 -sticky ew
    grid $w.but.original -row 0 -column 1 -padx 4 -pady 10 -sticky ew
    grid $w.but.close -row 0 -column 2 -padx 4 -pady 10 -sticky ew

    grid $w.f0 -sticky new -columnspan 2 -padx 3 -pady 3
    grid $w.f1 -sticky nsew -rowspan 2 -padx 3 -pady 3
    grid $w.f2 -sticky nsew -row 1 -column 1 -padx 3 -pady 3
    grid $w.f3 -sticky wes -row 2 -column 1 -padx 3 -pady 3 
    grid $w.but -sticky nsew -row 3 -column 0 -columnspan 2
    grid columnconfigure $w 0 -weight 1
    grid columnconfigure $w 1 -weight 1
    grid rowconfigure $w 1 -weight 1
    grid rowconfigure $w 2 -weight 1

    set PPointPriv(AvoidUpdate) 0

    set PPointPriv(DoingAccept) 0
    FillPostPointOptionsWInfo 1

    trace variable PPointPriv(Value) w ChangePointSizeScale
    trace variable PPointPriv(Detail) w ChangePointDetail
    trace variable PPointPriv(NiceValue) w ChangePointDetailScale
    trace variable PPointPriv(WhichSize) w ChangePointWhichSize
    trace variable PPointPriv(SelectedResult) w ChangeSelectedResult

    set pp $PPointPriv(SelectedResult)
    set PPointPriv(SelectedResult) ""
    set PPointPriv(SelectedResult) $pp

}

