#!/bin/sh
#PerspectiveWindow  -*- TCL -*- Created: miguel Nov-1997, Modified: miguel Nov-1997


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

set PerspectiveWRunningAlone 0
catch {
    if {$argv0 == [info script] } {
	set PerspectiveWRunningAlone 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"
	}
	wm withdraw .
    }
}

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

proc PerspectiveWOriginal {} {
    global PerspPriv

#    if { $PerspPriv(DoIt) } {
	set PerspPriv(AvoidUpdate) 1
#    .central.s process clip original
    set PerspPriv(Distance) [ expr log10( 4.0)]
    set PerspPriv(DoIt) 0
    if { $PerspPriv(DynamicUpdate) } { SendPerspectiveInfo no }
    after idle set PerspPriv(AvoidUpdate) 0 ; FillPerspectiveWInfo
#    }
}

proc FillPerspectiveWInfo {} {
    global GidPriv PerspPriv
    
    if { $PerspPriv(AvoidUpdate) } { return }
    
    set w $GidPriv(PerspectiveWWindow)

    set perspmode [ .central.s info perspectivemode ]
    set perspdist [ .central.s info perspectivefactor ]

#set perspmode "NORMAL"
#set perspdist 4.0

#     set ortholimits [.central.s info ortholimits]
#     set graphcenter [.central.s info graphcenter]
#     set graphcenterOrig [.central.s info graphcenter -original]

#     set PerspPriv(Xcenter) [lindex $graphcenter 0]
#     set PerspPriv(Ycenter) [lindex $graphcenter 1]
#     set PerspPriv(Zcenter) [lindex $graphcenter 2]

#     set PerspPriv(XcenterOrig) [lindex $graphcenterOrig 0]
#     set PerspPriv(YcenterOrig) [lindex $graphcenterOrig 1]
#     set PerspPriv(ZcenterOrig) [lindex $graphcenterOrig 2]


#     set PerspPriv(Near) [expr -1.0*[lindex $ortholimits 4]-$PerspPriv(Zcenter)]
#     set PerspPriv(Far) [expr $PerspPriv(Zcenter)+[lindex $ortholimits 5]]

#     set PerspPriv(ExtOrtho) [expr [lindex $ortholimits 6]*$PerspPriv(Boundariesfactor)]
#     set PerspPriv(ExtOrthoReal) [expr $PerspPriv(ExtOrtho)+sqrt( \
# 	    pow($PerspPriv(Xcenter)-$PerspPriv(XcenterOrig),2)+ \
# 	    pow($PerspPriv(Ycenter)-$PerspPriv(YcenterOrig),2)+ \
# 	    pow($PerspPriv(Zcenter)-$PerspPriv(ZcenterOrig),2))]

    
#     set resolution [expr $PerspPriv(ExtOrthoReal)/100.0]
#     set from [expr $PerspPriv(ExtOrthoReal)-$resolution]
#     set to [expr $resolution-$PerspPriv(ExtOrthoReal)]

#     $w.f1.s conf -from $from -to $to -resolution $resolution

  
    
    # escala logaritmica:
#    $w.f1.s conf -from 0.01 -to 100.0 -resolution 0.01
#    $w.f1.s conf -from 0.0001 -to 100.0 -resolution 0.01
    #$w.f1.s conf -from [ expr log10( 0.01)] -to [ expr log10( 100.0)] -resolution 0.1
    $w.f1.s conf -from [ expr log10( 0.0001)] -to [ expr log10( 100.0)] -resolution 0.1
    
    set PerspPriv(Distance) [ expr log10( $perspdist) ]

    $w.f1.s set $PerspPriv(Distance)
    set PerspPriv(DoIt) 0
    if { $perspmode == "NORMAL" } {
	set PerspPriv(DoIt) 0
	$w.f1.s conf -state disabled -foreground grey60
	$w.f1.e conf -state disabled -foreground grey60
    } else {
	set PerspPriv(DoIt) 1
	$w.f1.s conf -state normal -foreground black
	$w.f1.e conf -state normal -foreground black
    }
    #set PerspPriv(DynamicUpdate) 0
    

#     $w.f2.s conf -from $from -to $to -resolution $resolution

#     $w.f2.s set $PerspPriv(Near)

#    ActualizePerspectiveWCanvas

}

proc ChangePerspective { newval } {
    global GidPriv PerspPriv

    set w $GidPriv(PerspectiveWWindow)

    if { ![IsWord real $newval $w " In Distance field"] } {
	$w.f1.e sel from 0
	$w.f1.e sel to end
	return error
    }

    #if { $newval < [ expr log10( 0.0099999)] || }

    if { $newval < [ expr log10( 0.000099999)] || \
	    $newval > [ expr log10( 100.0)] } {
	tk_dialogRAM $w.tempwin [_ "Error window"] [_ "Value out of limits"] \
	    error 0 [_ "OK"]
	$w.f1.e sel from 0
	$w.f1.e sel to end
	return error
    }

    set PerspPriv(Distance) $newval

    if { $PerspPriv(Distance) != [$w.f1.s get ] } {
	$w.f1.s set $PerspPriv(Distance)
    }

    if { $PerspPriv(DynamicUpdate) } { SendPerspectiveInfo no }
} 

proc SetPerspective { name1 name2 ops } {
    global GidPriv PerspPriv

    set w $GidPriv(PerspectiveWWindow)

    if { $PerspPriv(DoIt) } {
	$w.f1.e conf -state normal -foreground black
	$w.f1.s conf -state normal -foreground black
    } else {
	$w.f1.e conf -state disabled -foreground grey60
	$w.f1.s conf -state disabled -foreground grey60
    }

}

proc SendPerspectiveInfo { { DoTest yes } { Fastredraw yes } } {
    global GidPriv PerspPriv

    set w $GidPriv(PerspectiveWWindow)

    set PerspPriv(AvoidUpdate) 1
    if { $DoTest == "yes" } {
	if { [ChangePerspective $PerspPriv(Distance)] == "error" } { return }
    }

    set old_perspmode [ .central.s info perspectivemode ]
    set old_perspdist [ expr log10( [ .central.s info perspectivefactor ])]

    set perspcmd projection

    if { $Fastredraw == "yes" } {
	set perspcmd "$perspcmd fastdraw"
    }

    if { $PerspPriv(DoIt) } {
	if { $old_perspmode == "NORMAL" } {
	    set perspcmd "$perspcmd Perspective"
	}
    } else { 
	if { $old_perspmode == "PERSPECTIVE" } {
	    set perspcmd "$perspcmd Normal"
	}
    }

    if { $PerspPriv(DoIt) &&  \
	    [expr abs( $old_perspdist - $PerspPriv(Distance) ) ] > 1.0e-7 } {
	set perspcmd "$perspcmd distance [ expr pow( 10, $PerspPriv(Distance))]"
	if { $Fastredraw == "yes" } {
	    set perspcmd "$perspcmd nofastdraw"
	}
    }
    .central.s process $perspcmd escape escape escape escape

    after idle set PerspPriv(AvoidUpdate) 0 ; FillPerspectiveWInfo

}


proc PerspectiveW { { w .gid.wPerspectiveW } } {
    global GIDDEFAULT PerspPriv

    InitWindow $w [_ "Perspective"] PerspectiveWWindowGeom PerspectiveW

    set PerspPriv(DynamicUpdate) 0

#     label $w.dib -bitmap @$GIDDEFAULT/bitmaps/clipplane.xbm \
# 	    -relief groove -bd 2

    catch {
	trace vdelete PerspPriv(DoIt) w SetPerspective
    }

    frame $w.f0
    checkbutton $w.f0.c -text [_ "Perspective projection"] \
	    -variable PerspPriv(DoIt)
    label $w.f0.l -text [_ "Log10 of Distance between \nObserver and Object Center:"]

    frame $w.f1 -relief ridge -bd 2
    label $w.f1.l -text [_ "log10(Distance):"]
    entry $w.f1.e -relief sunken -textvariable PerspPriv(Distance)
    bind $w.f1.e <Return> { ChangePerspective $PerspPriv(Distance) }
    scale $w.f1.s -highlightbackground black -orient h \
	    -command "ChangePerspective"

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

    set def_back [$w cget -background]
    frame $w.but -bg [CCColorActivo $def_back]
    button $w.but.accept -text [_ "Apply"] -command "SendPerspectiveInfo yes no"  -underline 0 -width 6
    button $w.but.original -text [_ "Default"] -command "PerspectiveWOriginal" -underline 0 -width 6
    button $w.but.close -text [_ "Close"] -command "destroy $w" -underline 0 -width 6
    

    grid $w.f0.c -sticky w
    grid $w.f0.l -sticky ew

    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

    grid $w.but.accept -row 1 -column 1 -padx 5 -pady 6
    grid $w.but.original -row 1 -column 2 -padx 5 -pady 6
    grid $w.but.close -row 1 -column 3 -padx 5 -pady 6
    grid $w.but -row 4 -column 0  -sticky ews -columnspan 7

    grid $w.f0 -row 0 -padx 3 -pady 3 -sticky ew 
    grid $w.f1 -row 1 -padx 3 -pady 3 -sticky ew
    grid $w.din -row 2 -padx 3 -pady 3 -sticky w
    grid $w.but -row 3 -sticky ews -columnspan 7

    grid columnconfigure $w 0 -weight 1
    grid rowconfigure $w 3 -weight 1
    
    # Resize behavior management    
    

    wm minsize $w 180 200

    set PerspPriv(AvoidUpdate) 0

    FillPerspectiveWInfo

    trace variable PerspPriv(DoIt) w SetPerspective
}


#----------------------CopyMove ALONE---------------------------
if { $PerspectiveWRunningAlone == 1 } {
    PerspectiveW
} 
#-----------------------------------------------------------------
