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


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

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


namespace eval PostGeom {
    variable _w_id
    # pagina_malla_principal
    variable _p_mp
    # pagina__malla_copia 
    variable _p_mc

    variable _MallaPrincipal
    variable _MallaCopia
}

proc FillPGInfo {} {
}


proc PostGeom::Close { } {
    variable _w_id

    destroy $_w_id
}

proc PostGeom::Apply { } {
    variable _w_id

    WarnWin "Aceptado" $_w_id
}

proc PostGeom::GetGiDResultsInfoPrincipal { w storevar estado} {
    upvar $storevar data

    # todos los analisis
    set all [.central.s info post get all_analisis]
    $data(analisis,wid).menu del 0 end
    foreach j $all {
	regsub -all {_} $j { } var
	$data(analisis,wid).menu add command -label $var -command [list set data(analisis) $var]
    }

    # analisis actual
    set cur [.central.s info post get cur_analisis]
    if { $cur != "" } {
	regsub -all {_} $cur { } cur
    } else { regsub -all {_} [ lindex $all 0] { } cur }

    set data(analisis) "$cur"

    # todos los pasos del analisis
    $data(paso,wid) listbox del 0 end
    if { $data(analisis) != "" } {
	regsub -all { } $data(analisis) {_} analisis
	set all_st [.central.s info post get all_steps $analisis]
	foreach j $all_st {
	    $data(paso,wid) add $j
	}
	set data(paso) [.central.s info post get cur_step $cur]
    }

    set all [ .central.s info post get main_geom_all_deform]
    $data(resultado,wid) del 0 end
    foreach j $all {
	regsub -all {_} $j { } var
	$data(resultado,wid) ins end "$var"
    }

    #if { "$estado" == "Deformed"} {
	set var [ .central.s info post get main_geom_cur_deform]
	if { "$var" == ""} { set var [ lindex $all 0] }
	regsub -all {_} $var { } var
	set data(resultado) $var
    #}

    if { ( "$data(analisis)" != "") && \
	    ( "$data(paso)" != "") && \
	    ( "$data(resultado)" != "") } {
	regsub -all { } $data(resultado) {_} def_vec
	regsub -all { } $data(analisis) {_} res_anal
	set data(factor) [.central.s info post \
		get main_geom_factor $res_anal $data(paso) $def_vec]
    } else {
	set data(factor) 1
    }

    #WarnWin "Current: $data(analisis) $data(paso) $data(resultado) $data(factor)"
}

proc PostGeom::GetGiDResultsInfoCopia { w storevar estado} {
    upvar $storevar data

    # todos los analisis
    set all [.central.s info post get all_analisis]
    $data(analisis,wid).menu del 0 end
    foreach j $all {
	regsub -all {_} $j { } var
	$data(analisis,wid).menu add command -label $var -command [list set data(analisis) $var]
    }

    # analisis actual
    set cur [.central.s info post get cur_analisis]
    if { $cur != "" } {
	regsub -all {_} $cur { } cur
    } else { regsub -all {_} [ lindex $all 0] { } cur }

    set data(analisis) "$cur"

    # todos los pasos del analisis
    $data(paso,wid) listbox del 0 end
    if { $data(analisis) != "" } {
	regsub -all { } $data(analisis) {_} analisis
	set all_st [.central.s info post get all_steps $analisis]
	foreach j $all_st {
	    $data(paso,wid) add $j
	}
	set data(paso) [.central.s info post get cur_step $cur]
    }

    regsub -all { } $data(analisis) {_} ana
    set all [ .central.s info post get show_geom_all_deform $ana $data(paso)]
    $data(resultado,wid) del 0 end
    foreach j $all {
	regsub -all {_} $j { } var
	$data(resultado,wid) ins end "$var"
    }

    #if { "$estado" == "Deformed"} {
	set var [ .central.s info post get show_geom_cur_deform]
	if { "$var" == ""} { set var [ lindex $all 0] }
	regsub -all {_} $var { } var
	set data(resultado) $var
    #}

    if { ( "$data(analisis)" != "") && \
	    ( "$data(paso)" != "") && \
	    ( "$data(resultado)" != "") } {
	regsub -all { } $data(resultado) {_} def_vec
	regsub -all { } $data(analisis) {_} res_anal
	set data(factor) [.central.s info post \
		get show_geom_factor $res_anal $data(paso) $def_vec]
    } else {
	set data(factor) 1
    }

    #WarnWin "Current: $data(analisis) $data(paso) $data(resultado) $data(factor)"
}

proc PostGeom::ActualizaVariables { } {
    variable _w_id

    variable _MallaPrincipal
    variable _MallaCopia

    # averiguar estado malla principal
    
    set mainst [.central.s info post get main_geom_state]
    set _MallaPrincipal(estado) $mainst

    GetGiDResultsInfoPrincipal $_MallaPrincipal(wid_pagina) _MallaPrincipal $mainst

    if { "$mainst" == "Original"} {
	$_MallaPrincipal(wid_pagina).frd.rd1 invoke
	#DisableResultFactorWidget $_MallaPrincipal(wid_pagina).rs
    } else {
	$_MallaPrincipal(wid_pagina).frd.rd2 invoke
	#DisableResultFactorWidget $_MallaPrincipal(wid_pagina).rs
    }
    #WarnWin "$_MallaPrincipal(analisis) $_MallaPrincipal(paso) $_MallaPrincipal(resultado)"

    
    # ahora la copia
    set copiast [.central.s info post get show_geom_state]
    set _MallaCopia(estado) $copiast
    
    GetGiDResultsInfoCopia $_MallaCopia(wid_pagina) _MallaCopia $copiast

    if { "$copiast" == "Off"} {
	$_MallaCopia(wid_pagina).frd.rd1 invoke
    } elseif { "$copiast" == "Original"} {
	$_MallaCopia(wid_pagina).frd.rd2 invoke
    } else {
	$_MallaCopia(wid_pagina).frd.rd3 invoke
    }

}

proc DisableResultFactorWidget { w } {
    $w.fa.la conf -foreground grey60
    $w.fa.ma conf -state disabled
    $w.fs.ls conf -foreground grey60
    $w.fs.cb conf -foreground grey60
    $w.fs.cb conf -state disabled
    $w.fr.lr conf -foreground grey60
    $w.fr.lf.lb conf -foreground grey60
    #$w.fr.lf.lb conf -state disabled
    $w.ff.l conf -foreground grey60
    $w.ff.e conf -foreground grey60
    $w.ff.e conf -state disabled
}

proc EnableResultFactorWidget { w } {
    $w.fa.la conf -foreground black
    $w.fa.ma conf -state normal
    $w.fs.ls conf -foreground black
    $w.fs.cb conf -foreground black
    $w.fs.cb conf -state normal
    $w.fr.lr conf -foreground black
    $w.fr.lf.lb conf -foreground black
    #$w.fr.lf.lb conf -state normal
    $w.ff.l conf -foreground black
    $w.ff.e conf -foreground black
    $w.ff.e conf -state normal
}

proc PostGeom::CambiaMalla { cual que w_id} {
    variable $cual

    set PostGeom::${cual}(estado) $que
    if { "$que" == "Deformed"} {
	EnableResultFactorWidget $w_id.rs
    } else {
	DisableResultFactorWidget $w_id.rs
    }
}

proc PostGeom::CreateResultFactorSelectionWidget { w storevar} {

    frame $w -bd 0

    frame $w.fa -bd 0
    label $w.fa.la -text "Analysis:"
    set menu [ tk_optionMenu $w.fa.ma PostGeom::${storevar}(analisis) {Load analysis} {Time analysis}]
    $w.fa.ma conf -width 20 -bd 1 -highlightthickness 1 
    $menu conf -bd 1 -activeborderwidth 1
    grid $w.fa.la $w.fa.ma -sticky w
    grid conf $w.fa.ma -sticky we
    grid rowconfigure $w.fa 0 -weight 1
    grid columnconfigure $w.fa 1 -weight 1
    set PostGeom::${storevar}(analisis,wid) $w.fa.ma

    frame $w.fs -bd 0
    label $w.fs.ls -text "Step:"
    combobox $w.fs.cb -textvariable PostGeom::${storevar}(paso) -editable 0 -width 6
    grid $w.fs.ls $w.fs.cb -sticky w
    grid rowconfigure $w.fs 0 -weight 1
    grid columnconfigure $w.fs 1 -weight 1
    set PostGeom::${storevar}(paso,wid) $w.fs.cb

    frame $w.fr -bd 0
    label $w.fr.lr -text "Result:"
    frame $w.fr.lf -relief sunken -bd 1
    listbox $w.fr.lf.lb -height 5 -width 0 -borderwidth 0 -relief flat \
	    -selectmode extended -yscrollcommand "$w.fr.lf.ys set" \
	    -highlightthickness 0 -xscrollcommand "$w.fr.lf.xs set"
    
    scrollbar $w.fr.lf.ys -orient v -command "$w.fr.lf.lb yview" -bd 1 \
	    -elementborderwidth 1
    scrollbar $w.fr.lf.xs -orient h -command "$w.fr.lf.lb xview" -bd 1 \
	    -elementborderwidth 1
    grid $w.fr.lf.lb -row 0 -column 0 -sticky nesw
    grid $w.fr.lf.ys -row 0 -column 1 -sticky ns
    grid $w.fr.lf.xs -row 1 -column 0 -sticky ew
    grid columnconfigure $w.fr.lf 0 -weight 1
    grid rowconfigure $w.fr.lf 0 -weight 1
    #set menu [ tk_optionMenu $w.fr.mr PostGeom::${storevar}(resultado) {Displacements} {Deformation}]
    #$w.fr.mr conf -width 20 -bd 1 -highlightthickness 1 
    #$menu conf -bd 1 -activeborderwidth 1
    #grid $w.fr.lr $w.fr.mr -sticky w
    #grid conf $w.fr.mr -sticky we
    grid $w.fr.lr $w.fr.lf -sticky nw
    grid conf $w.fr.lf -sticky wens
    grid rowconfigure $w.fr 0 -weight 1
    grid columnconfigure $w.fr 1 -weight 1
    set PostGeom::${storevar}(resultado,wid) $w.fr.lf.lb

    frame $w.ff -bd 0
    label $w.ff.l -text factor:
    entry $w.ff.e -width 10 -textvariable PostGeom::${storevar}(factor) -bd 1 -relief sunken
    grid $w.ff.l $w.ff.e -sticky w
    set PostGeom::${storevar}(factor,wid) $w.ff.l

    grid $w.fa -sticky we -padx 2 -pady 0
    grid $w.fs -sticky nesw -padx 2 -pady 0
    grid $w.fr -sticky we -padx 2 -pady 0
    grid $w.ff -sticky w -padx 2 -pady 0
    grid columnconfigure $w 0 -weight 1
    grid rowconfigure $w 0 -weight 1
    grid rowconfigure $w 1 -weight 1
    grid rowconfigure $w 2 -weight 1

}

proc PostGeom::Create { { w .wPostGeom2 }} {
    variable _w_id
    variable _p_mp
    variable _p_mc
    variable _MallaPrincipal
    variable _MallaCopia

    set _w_id $w
    toplevel $w
    #puts PostGeom-----------------------------
    #InitWindow $w "Mesh Deformation" PostGeomWindowGeom PostGeom

    set def_back [ $w cget -background]
    set realzado [ CCColorActivo $def_back]

    # marco de las cosas:
    frame $w.f -bd 0

    NoteBook $w.f.nb -internalborderwidth 0 -activebackground $realzado

    # pagina de la geometria principal
    set _p_mp [ $w.f.nb insert end MainMesh -text "Main Mesh"]

    text $_p_mp.t -bd 4 -relief flat -width 20 -height 2 -wrap word -background $realzado
    $_p_mp.t ins end "Results will be drawn on this model."
    $_p_mp.t conf -state disabled

    frame $_p_mp.frd -bd 0
    radiobutton $_p_mp.frd.rd1 -text Original -value Original -variable _MallaPrincipal(estado) \
	    -command "PostGeom::CambiaMalla _MallaPrincipal Original $_p_mp"
    radiobutton $_p_mp.frd.rd2 -text Deformed -value Deformed -variable _MallaPrincipal(estado) \
	    -command "PostGeom::CambiaMalla _MallaPrincipal Deformed $_p_mp"
    grid $_p_mp.frd.rd1 $_p_mp.frd.rd2 -sticky wns -padx 4 -pady 0

    set _MallaPrincipal(wid_pagina) $_p_mp
    PostGeom::CreateResultFactorSelectionWidget $_p_mp.rs _MallaPrincipal


    grid $_p_mp.frd -sticky wns -padx 4 -pady 0
    grid $_p_mp.rs -sticky enws -padx 4 -pady 0
    grid $_p_mp.t -sticky nwes -padx 4 -pady 8
    grid columnconfigure $_p_mp 0 -weight 1
    grid rowconfigure $_p_mp 0 -weight 1
    grid rowconfigure $_p_mp 1 -weight 1
    grid rowconfigure $_p_mp 2 -weight 1


    ##############################
    # pagina de la geometria de referencia ( copia del modelo)
    set _p_mc [ $w.f.nb insert end CopyMesh -text "Copy mesh"]

    text $_p_mc.t -bd 4 -relief flat -width 20 -height 2 -wrap word -background $realzado
    $_p_mc.t ins end "This is a snapshot of the model. Results will NOT be drawn over this copy."
    $_p_mc.t conf -state disabled

    frame $_p_mc.frd -bd 0
    radiobutton $_p_mc.frd.rd1 -text Off -value Off -variable _MallaCopia(estado) \
	    -command "PostGeom::CambiaMalla _MallaCopia Off $_p_mc"
    radiobutton $_p_mc.frd.rd2 -text Original -value Original -variable _MallaCopia(estado) \
	    -command "PostGeom::CambiaMalla _MallaCopia Original $_p_mc"
    radiobutton $_p_mc.frd.rd3 -text Deformed -value Deformed -variable _MallaCopia(estado) \
	    -command "PostGeom::CambiaMalla _MallaCopia Deformed $_p_mc"
    grid $_p_mc.frd.rd1 $_p_mc.frd.rd2 $_p_mc.frd.rd3 -sticky wns -padx 4 -pady 0

    frame $_p_mc.fcol -bd 0
    label $_p_mc.fcol.l -text "Use color: "
    radiobutton $_p_mc.fcol.rd1 -text "of mesh " -value MeshColor -variable _MallaCopia(ColorCopia)
    radiobutton $_p_mc.fcol.rd2 -value UserColor -variable _MallaCopia(ColorCopia)
    button $_p_mc.fcol.b -width 3 -background $realzado
    #-command "CambiaColorCopia $w.showg.f.opt.bcol"
    grid $_p_mc.fcol.l $_p_mc.fcol.rd1 $_p_mc.fcol.rd2 $_p_mc.fcol.b -sticky wns -padx 0 -pady 0 -ipadx 0

    set _MallaCopia(wid_pagina) $_p_mc
    PostGeom::CreateResultFactorSelectionWidget $_p_mc.rs _MallaCopia


    grid $_p_mc.frd -sticky wns -padx 4 -pady 0
    grid $_p_mc.fcol -sticky wns -padx 4 -pady 0
    grid $_p_mc.rs -sticky enws -padx 4 -pady 0
    grid $_p_mc.t -sticky nwes -padx 4 -pady 8
    grid columnconfigure $_p_mc 0 -weight 1
    grid rowconfigure $_p_mc 0 -weight 1
    grid rowconfigure $_p_mc 1 -weight 1
    grid rowconfigure $_p_mc 2 -weight 1
    grid rowconfigure $_p_mc 3 -weight 1
    


    $w.f.nb compute_size
    $w.f.nb raise [ $w.f.nb page 0]
    grid $w.f.nb -sticky news -padx 4 -pady 2
    grid columnconfigure $w.f 0 -weight 1
    grid rowconfigure $w.f 0 -weight 1



    # marco de los botones
    frame $w.but -bd 0 -background $realzado

    button $w.but.apply -text Apply -command PostGeom::Apply -takefocus 0 -bd 1
    button $w.but.close -text Close -command PostGeom::Close -takefocus 0 -bd 1
    grid $w.but.apply -sticky ews -padx 4 -pady 10
    grid $w.but.close -sticky ews -padx 4 -pady 10 -row 0 -column 1



    #enpaquetamos marcos
    grid $w.f -sticky news -padx 0 -pady 0
    grid $w.but -sticky wes

    grid columnconfigure $w 0 -weight 1
    grid rowconfigure $w 0 -weight 1
    bind $w <Return> "focus $w.but.apply; \
	    $w.but.apply conf -relief sunken; \
	    $w.but.apply invoke; \
	    $w.but.apply conf -relief raised"
    bind $w <Escape> "focus $w.but.close; \
	    $w.but.close conf -relief sunken; \
	    $w.but.close invoke"

    update idletasks
    ActualizaVariables

}


#----------------------SetCoord ALONE---------------------------
# if { $PGRunningAlone == 1 } {
#     PostGeom .wPostPG
# } 
#-----------------------------------------------------------------
