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


#---------------------------------------------------------------
# Line Size Options
#---------------------------------------------------------------

proc FillPostLineOptionsWInfo { } {
    global PLinePriv GidPriv

    if { $PLinePriv(AvoidUpdate) } { return }

    set w $GidPriv(PostLineOptionsWindow)
    set info [ .central.s info post get $PLinePriv(LineInfoCMD)]

    $w.f1.s conf -state normal -foreground black
    $w.f1.e conf -state normal -foreground black

    set mi_valor [ lindex $info 0]
    set opciones [ lindex $info 1]
    set PLinePriv(MinNice) [ lindex $opciones 0]
    set PLinePriv(MaxNice) [ lindex $opciones 1]
    set PLinePriv(StepNice) [ lindex $opciones 2]
    set PLinePriv(DefaultValue) [ lindex $opciones 3]
    set PLinePriv(MinQuick) -1.00000001
    set PLinePriv(MaxQuick) -0.99999999
    set PLinePriv(StepQuick) 1.0

    set PLinePriv(DetailValue) [ lindex $info 2]
    set opciones [ lindex $info 3]
    set PLinePriv(DefaultDetailValue) [ lindex $opciones 3]

    if { $mi_valor < 0.0} { 
	set PLinePriv(Detail) Quick
	set PLinePriv(Value) -1.0
    } else {
	set PLinePriv(Detail) Nice
	set PLinePriv(Value) $mi_valor
    }

    set PLinePriv(OldDetail) $PLinePriv(Detail)
    set PLinePriv(OldValue) $PLinePriv(Value)
    set PLinePriv(AccValue) $PLinePriv(Value)
    set PLinePriv(OldDetailValue) $PLinePriv(DetailValue)
    set PLinePriv(AccDetailValue) $PLinePriv(DetailValue)
    
    $w.f1.s conf -from $PLinePriv(Min$PLinePriv(Detail)) -to $PLinePriv(Max$PLinePriv(Detail)) -resolution $PLinePriv(Step$PLinePriv(Detail))
    $w.f1.s set $PLinePriv(Value)


    # (des)habilitamos el detalle
    if { $PLinePriv(Value) <= 0.0} {
	$w.f2.rd1 conf -state disabled
	$w.f2.rd2 conf -state disabled
	$w.f2.l conf -foreground grey60
	if { $PLinePriv(Value) < 0.0} {
	    # deshabilitamos la escala
	    $w.f1.l conf -fg grey60
	    $w.f1.e conf -state disabled -fg grey60
	    $w.f1.s conf -state disabled -fg grey60
	}
    } else {
	$w.f2.rd1 conf -state normal
	$w.f2.rd2 conf -state normal
	$w.f2.l conf -foreground black
	# habilitamos la escala
	$w.f1.l conf -fg black
	$w.f1.e conf -state normal -fg black
	$w.f1.s conf -state normal -fg black
    }
}

proc PostLineAccept { } {
    global PLinePriv

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

    if { $PLinePriv(DetailValue) != $PLinePriv(AccDetailValue)} {
	.central.s process escape escape escape escape Result Options $PLinePriv(LineDetailCMD) $PLinePriv(DetailValue)
	set PLinePriv(AccValue) $PLinePriv(DetailValue)
    }
    
    if { $PLinePriv(Value) != $PLinePriv(AccValue)} {
	.central.s process escape escape escape escape Result Options $PLinePriv(LineSizeCMD) $PLinePriv(Value)
	set PLinePriv(AccValue) $PLinePriv(Value)
    }
    .central.s process escape escape escape escape utilities variables \
	    AutomaticRedraw $oldAutomaticRedraw escape escape 

    set old $PLinePriv(DynamicUpdate)
    set PLinePriv(DynamicUpdate) 0
    FillPostLineOptionsWInfo
    set PLinePriv(DynamicUpdate) $old
	
    .gid.central.s render
}

proc PostLineOriginal { } {
    global PLinePriv
    
    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 $PLinePriv(LineDetailCMD) $PLinePriv(DefaultDetailValue)

    .central.s process escape escape escape escape Result Options $PLinePriv(LineSizeCMD) $PLinePriv(DefaultValue)

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

    .gid.central.s render    

    set old $PLinePriv(DynamicUpdate)
    set PLinePriv(DynamicUpdate) 0
    FillPostLineOptionsWInfo
    set PLinePriv(DynamicUpdate) $old
}

proc ChangeLineSizeScale { name1 name2 op} {
    global PLinePriv GidPriv

    if { $PLinePriv(Value) == $PLinePriv(OldValue)} { return}

    set w $GidPriv(PostLineOptionsWindow)

    set newval $PLinePriv(Value)

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

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

    set pp [ expr ($newval - $PLinePriv(Min$PLinePriv(Detail))) / $PLinePriv(Step$PLinePriv(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 $PLinePriv(Min$PLinePriv(Detail)) \
		 $PLinePriv(Max$PLinePriv(Detail)) $PLinePriv(Step$PLinePriv(Detail))]
	error 0 [_ "OK"]
	$w.f1.e sel from 0
	$w.f1.e sel to end

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

    set PLinePriv(OldValue) $PLinePriv(Value) 
 
    if { $PLinePriv(OldValue) <= 0.0} {
	$w.f2.rd1 conf -state disabled
	$w.f2.rd2 conf -state disabled
	$w.f2.l conf -foreground grey60
    } else {
	$w.f2.rd1 conf -state normal
	$w.f2.rd2 conf -state normal
	$w.f2.l conf -foreground black
    }

    #set PLinePriv(Value) $newval

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

proc ChangeLineDetailValue { name1 name2 op} {
    global PLinePriv GidPriv

    if { $PLinePriv(DetailValue) == $PLinePriv(OldDetailValue)} { return}

    set PLinePriv(OldValue) $PLinePriv(Value) 
    
    if { $PLinePriv(DynamicUpdate) } { PostLineAccept}
}


proc ChangeLineDetail { n1 n2 op} {
    global PLinePriv GidPriv

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

    set PLinePriv(OldDetail) $PLinePriv(Detail) 

    if { "$PLinePriv(Detail)" == "Quick" } {
	if { $PLinePriv(Value) > 0.0} {
	    set PLinePriv(LastNiceValue) $PLinePriv(Value)
	} else {
	    set PLinePriv(LastNiceValue) $PLinePriv(DefaultValue)
	}
    }

    set w $GidPriv(PostLineOptionsWindow)

    $w.f1.s conf -from $PLinePriv(Min$PLinePriv(Detail)) -to $PLinePriv(Max$PLinePriv(Detail)) -resolution $PLinePriv(Step$PLinePriv(Detail))

    if { "$PLinePriv(Detail)" == "Nice" } {
	$w.f1.l conf -fg black
	$w.f1.e conf -state normal -fg black
	$w.f1.s conf -state normal -fg black
	set PLinePriv(Value) $PLinePriv(LastNiceValue)
    } elseif { "$PLinePriv(Detail)" == "Quick" } {
	$w.f1.l conf -fg grey60
	$w.f1.e conf -state disabled -fg grey60
	$w.f1.s conf -state disabled -fg grey60
	set PLinePriv(Value) -1.0
    }
    $w.f1.s set $PLinePriv(Value)
    #ChangeLineDetailValue PlinePriv Value w
}

proc PostLineOptions { line_size_cmd line_detail_cmd line_info_cmd title { w .gid.wPostLineOptions } } {
    global PLinePriv

    InitWindow $w $title PostLineOptionsWindowGeom PostLineOptions

    set PLinePriv(DynamicUpdate) 0

    set PLinePriv(LineSizeCMD) $line_size_cmd
    set PLinePriv(LineDetailCMD) $line_detail_cmd
    set PLinePriv(LineInfoCMD) $line_info_cmd
    catch {
	trace vdelete PLinePriv(Value) w ChangeLineSizeScale
	trace vdelete PLinePriv(DetailValue) w ChangeLineDetailValue
	trace vdelete PLinePriv(Detail) w ChangeLineDetail
    }

    frame $w.f1 -relief ridge -bd 2
    label $w.f1.l -text [_ "Line Size"]:
    entry $w.f1.e -relief sunken -textvariable PLinePriv(Value) -width 5
    #bind $w.f1.e <Return> { ChangePerspective $PerspPriv(Distance) }
    scale $w.f1.s -highlightbackground black -highlightthickness 0 -orient h -bd 1 -variable PLinePriv(Value)

    grid $w.f1.l -sticky w -row 0 -column 0
    grid $w.f1.e -sticky ew -row 0 -column 1
    grid $w.f1.s -sticky ew -row 1 -column 0 -columnspan 2
    grid columnconfigure $w.f1 1 -weight 1

    frame $w.f2 -relief ridge -bd 2
    label $w.f2.l -text [_ "Line's prismas of"]
    radiobutton $w.f2.rd1 -text [_ "4 sides"] -value 4 -variable PLinePriv(DetailValue)
    radiobutton $w.f2.rd2 -text [_ "8 sides"] -value 8 -variable PLinePriv(DetailValue)
    grid $w.f2.l -sticky w -padx 2
    grid $w.f2.rd1 -sticky we -padx 2
    grid $w.f2.rd2 -sticky we -padx 2
    grid columnconfigure $w.f2 0 -weight 1

    frame $w.f3 -bd 0
    radiobutton $w.f3.rd1 -text [_ "Quick"] -value Quick -variable PLinePriv(Detail)
    radiobutton $w.f3.rd2 -text [_ "Nice"] -value Nice -variable PLinePriv(Detail)

    checkbutton $w.f3.din -text [_ "Dynamic update"] \
	    -variable PLinePriv(DynamicUpdate)

    grid $w.f3.rd1 -sticky es -padx 0 -pady 3
    grid $w.f3.rd2 -sticky ws -padx 0 -pady 3 -row 0 -column 1
    grid $w.f3.din -sticky ws -padx 3 -pady 3 -row 0 -column 2
    #grid $w.f3.rd1 $w.f3.rd2 $w.f3.din -sticky ws -padx 3 -pady 3 -columnspan 2

    frame $w.but
    button $w.but.accept -text [_ "Apply"] -bd 1 -command PostLineAccept
    button $w.but.original -text [_ "Original"] -bd 1 -command PostLineOriginal
    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.close -padx 4 -pady 2 -sticky ew

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

    set PLinePriv(AvoidUpdate) 0

    FillPostLineOptionsWInfo
    if { $PLinePriv(Value) < 0.0} {
	set PLinePriv(LastNiceValue) $PLinePriv(DefaultValue)
    } else {
	set PLinePriv(LastNiceValue) $PLinePriv(Value)
    }

    trace variable PLinePriv(Value) w ChangeLineSizeScale
    trace variable PLinePriv(DetailValue) w ChangeLineDetailValue
    trace variable PLinePriv(Detail) w ChangeLineDetail
}
