#!/bin/sh
#Comments  -*- TCL -*- Created: ramsan Apr-1999, Modified: ramsan Apr-1999


#---------------------- Comments ALONE---------------------------
# the next line restarts using wish \
	exec wish8.' "$0" "$@"

set CommentsRunningAlone 0
catch {
    if {$argv0 == [info script] } {
	set CommentsRunningAlone 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 CommentsFillFromGiD { w } {
    global CommentsPriv

    set CommentsPriv(initializing) 1
    set aa [.central.s info post get comments]
    for { set i 1 } { $i<=3 } { incr i } {
	set CommentsPriv(comm$i) [lindex $aa [ expr $i + 1]]
	if { $CommentsPriv(comm$i) == "" } {
	    set CommentsPriv(active$i) 0
	} else { set CommentsPriv(active$i) 1 }
    }
    if { [lindex $aa 0] == "yes" } {
	set CommentsPriv(active) 1
    } else {
	set CommentsPriv(active) 0
    }
    if { [lindex $aa 1] == "yes" } {
	set CommentsPriv(automatic) 1
    } else {
	set CommentsPriv(automatic) 0
    }

    if { "$CommentsPriv(donde)" == "PRE" } {
	set CommentsPriv(automatic) 0
    }    
#     WarnWinText aa=--$aa--

    set CommentsPriv(initializing) 0
}

proc AcceptComments { w } {
    global CommentsPriv

    if { $CommentsPriv(active) == 0 } {
	.central.s process escape escape utilities comments comments no escape
    } else {
	.central.s process escape escape utilities comments comments yes
	for { set i 1 } { $i<=3 } { incr i } {
	    if { $CommentsPriv(active$i) } {
		if { "$CommentsPriv(comm$i)" != "" } {
		    regsub -all {"} $CommentsPriv(comm$i) {\"} com
		    # {"}
		    .central.s process Line$i \"$com\"
		} else {
		    .central.s process Line$i \"\"
		}
	    } else { .central.s process Line$i \"\" }
	}
	if { $CommentsPriv(automatic) == 1 } {    
	    .central.s process escape escape utilities comments automatic yes
	} else {
	    .central.s process escape escape utilities comments automatic no
	}
	.central.s process escape
    }
}

proc CommentsActiveChange { w what} {
    global CommentsPriv
    switch $what {
	main {
	    if { $CommentsPriv(active) == 0 } {
		$w.ch2 conf -state disabled
		for { set i 1 } { $i<=3 } { incr i } {
		    set CommentsPriv(active$i) 0
		    $w.fr1.ch$i conf -state disabled
		    $w.fr1.l$i conf -state disabled -fg grey60
		}
	    } else {
		$w.ch2 conf -state normal
		CommentsAutomaticChange $w $what
		for { set i 1 } { $i<=3 } { incr i } {
		    $w.fr1.ch$i conf -state normal
		    if { [$w.fr1.l$i get] != "" } {
			  set CommentsPriv(active$i) 1
			  $w.fr1.l$i conf -state normal -fg black
		    }
		}
	    }
	}
	default {
	    if { $CommentsPriv(active$what) == 0 } {
		$w.fr1.l$what conf -state disabled -fg grey60
	    } else {
		$w.fr1.l$what conf -state normal -fg black
	    }
	}
    }
}

proc CommentsAutomaticChange { w what} {
    global CommentsPriv
    switch $what {
	main {
	    if { $CommentsPriv(automatic) == 0 } {
		for { set i 1 } { $i<=3 } { incr i } {
		    $w.fr1.ch$i conf -state normal
		    if { [$w.fr1.l$i get] != "" } {
			 set CommentsPriv(active$i) 1
			 $w.fr1.l$i conf -state normal -fg black
		    }
		}
		TipWin::Close "Comments fields"
	    } else {
		for { set i 1 } { $i<=3 } { incr i } {
		    set CommentsPriv(active$i) 0
		    $w.fr1.ch$i conf -state disabled
		    $w.fr1.l$i conf -state disabled -fg grey60
		}
		#if { !$CommentsPriv(initializing)} {
		#    TipWin::Create $w "Comments fields" "With automatic comments (in Post)\n"\
		#	      "you can use following fields:\n"\
		#	      "%an - analysis name\n"                  \
		#	      "%sv - step value\n"		     \
		#	      "%vt - result visualization type\n"	     \
		#	      "%vf - vector factor\n"		     \
		#	      "%rn - result name\n"		     \
		#	      "%cn - component name\n"		     \
		#	      "%da - deformation used analysis\n"	     \
		#	      "%ds - deformation used step value\n"    \
		#	      "%dr - deformation used result\n"	     \
		#	      "%df - deformation used factor" 
		#}
	    }
	}
	default {
	    if { $CommentsPriv(active$what) == 0 } {
		$w.fr1.l$what conf -state disabled -fg grey60
	    } else {
		$w.fr1.l$what conf -state normal -fg black
	    }
	}
    }
}


proc CommentsW { { w .wCommentsWW } } {
    global CommentsPriv
    InitWindow $w [_ "Comments"] CommentsWindowGeom CommentsW
    
    set def_back [ $w cget -background]
    set realzado [ CCColorActivo $def_back]

    if { [catch { set whatuse [lindex [.central.s info Project] 4] }] } {
	set whatuse GEOMETRYUSE
    }
    if { ( $whatuse == "GEOMETRYUSE") || ( $whatuse == "MESHUSE") } {
	set CommentsPriv(donde) PRE
    } else {
	set CommentsPriv(donde) POST
    }
    checkbutton $w.ch1 -text [_ "Active"] -variable CommentsPriv(active)
    checkbutton $w.ch2 -text [_ "Automatic"] -variable CommentsPriv(automatic)
    TipWin::CreateHelpButton $w.bh "Automatic fields" "When using automatic comments (in Post)\n"\
	    "you can use following fields:\n"\
	    "%an - analysis name\n"                  \
	    "%sv - step value\n"		     \
	    "%vt - result visualization type\n"	     \
	    "%vf - vector factor\n"		     \
	    "%rn - result name\n"		     \
	    "%cn - component name\n"		     \
	    "%da - deformation used analysis\n"	     \
	    "%ds - deformation used step value\n"    \
	    "%dr - deformation used result\n"	     \
	    "%df - deformation used factor" 

    frame $w.fr1 -relief ridge -bd 2
    frame $w.but -bd 0 -background $realzado

    grid $w.ch1 -sticky w -column 1 -row 1
    if { "$CommentsPriv(donde)" == "POST" } {
	grid $w.ch2 -sticky w -column 2 -row 1
	grid $w.bh -sticky e -column 3 -row 1
    }
    grid $w.fr1 -sticky nsew -padx 3 -pady 3 -column 1 -row 2 -columnspan 3
    grid $w.but -sticky nsew -column 1 -row 3 -columnspan 3
    grid columnconf $w 1 -weight 1
    grid rowconf $w 2 -weight 1


    for { set i 1 } { $i<=3 } { incr i } {
	checkbutton $w.fr1.ch$i -text "line $i" -variable CommentsPriv(active$i)
	entry $w.fr1.l$i -relief sunken -textvar CommentsPriv(comm$i) -bd 1
	
	grid $w.fr1.ch$i -column 1 -row $i
	grid $w.fr1.l$i -column 2 -row $i -sticky we
	
	grid columnconf $w.fr1 2 -weight 1
	grid rowconf $w.fr1 $i -weight 1
	trace variable CommentsPriv(active$i) w "CommentsActiveChange $w $i ;#"
    }

    # marco de los botones
    button $w.but.apply -text [_ "Apply"] -command "AcceptComments $w" -takefocus 0 -bd 1
    button $w.but.close -text [_ "Close"] -command "destroy $w" -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
    
    focus $w.fr1.l1


    trace variable CommentsPriv(active) w "CommentsActiveChange $w main ;#"
    if { "$CommentsPriv(donde)" == "POST" } {
	trace variable CommentsPriv(automatic) w "CommentsAutomaticChange $w main ;#"
    }

    CommentsFillFromGiD $w
}



#----------------------CopyMove ALONE---------------------------
if { $CommentsRunningAlone == 1 } {
    CommentsW
} 
#-----------------------------------------------------------------
