#!/bin/sh
#UndoCom  -*- TCL -*- Created: ramsan Feb-1997, Modified: ramsan Feb-1997


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

set UndoComRunningAlone 0
# catch {
#     if {$argv0 == [info script] } {
#         set UndoComRunningAlone 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 !!!
#-------------------------------------------------------------------
proc Changeundostate { text file } {
    global GidPriv
    if { $GidPriv(undostate) == 0 } {
	$text tag configure secondary1  -elide 0
#         $text tag configure secondary2 -elide 0
	$text tag configure secondary3 -elide 0
    } else {
	$text tag configure secondary1 -elide 1
#         $text tag configure secondary2 -elide 1
	$text tag configure secondary3 -elide 1
    }
    FillUndoWin $text $file
}

proc FillUndoWin { text file } {

    global GIDDEFAULT GidPriv
    #if { $file == "" || ![file exists $file] } return
    set fileid [open $file r]
    set Position 0
    set GidPriv(ReadFileName) ""
    $text configure -state normal
    $text delete 0.0 end
    while { ![eof $fileid] } {
	set aa [gets $fileid]
	if { ![string comp "*****SAVE MARK" [string range $aa 0 13]] } {
	    set Position [tell $fileid]
	    set GidPriv(ReadFileName) ""
	    regexp {SAVE MARK[ ]*(.*)$} $aa trash GidPriv(ReadFileName)
	}
    }
    seek $fileid $Position
    set inum 0
    set col [$text cget -bg]
    set flaggeo 0
    set numescapes -1
    while { ![eof $fileid] } {
	gets $fileid aa
	if { $aa == "*****SAVE STATE" } {
	    set pos [tell $fileid]
	    readdefaults $file $pos
	    while { ![eof $fileid] && ![string match "\*\*\*\*\*END*" $aa] } {
		gets  $fileid aa
	    }
	    gets  $fileid aa
	}
	set first [lindex $aa 0]
	set second [lindex $aa 1]
	if {  $first == "redraw"  ||  $first == "rotate" || $first == "zoom"} {
	    set tag secondary1
	} else {
	    set tag normal
	}

	if { $flaggeo == 1 } {
	     if { $first == "escape" } {
		incr numescapes
	    } else {
		set numescapes 0
	    }
	    if { $numescapes < 2 && $second != "escape" } {
		set tag secondary3
	    } else {
		set flaggeo 0
		set tag normal
	    }
	}
	foreach i $aa {
	    if { $i != "escape" } {
		if { $i == "geometry" || $i == "utilities" || $i == "Meshing" } {
		   set flaggeo 1
		}
		if { [llength $i] == 1 } {
		    $text ins end "$i " $tag
		} else { $text ins end "\"$i\" " }
	    } elseif { $i != "redraw" } {
		if { $GidPriv(undostate) == 0 } {
		    label $text.$inum -bitmap @$GIDDEFAULT/bitmaps/arrow-l -bg $col
		    regexp {([^.]*)} [$text index end] trash line
		    incr line -1
		    bind $text.$inum <1> "ChangeSelInUndoL $text $line"
		    $text window create end -padx 5 -window $text.$inum

		    #WarnWinText "FillUndoWin $GidPriv(undostate)"
		    #set img [$text image create end -image [ibitmapfrom arrow-l #903e9c]]
		    #WarnWinText "$img-[ibitmapfrom arrow-l #903e9c]"
		    #$text tag add secondary3 end end
		} else {
		    $text ins end "$i " secondary2
		    regexp {([^.]*)} [$text index end] trash line
		    incr line -1
		                    }
		incr inum
	    }
	}
	if { $GidPriv(undostate) == 1 && [info exists i] && $i == "escape" } {

	} else {
	    $text ins end \n $tag
	}
    }
    regexp {([^.]*)} [$text index end] trash line
    $text del [expr $line-2].0 end
    incr line -3
    $text tag add selected $line.0 end
    $text tag conf selected -background red
    $text dump -window -command "SetBackground red" selected.first selected.last
    $text yview end
    $text conf -state disabled

    $text tag configure normal -foreground black -elide 0
    $text tag configure secondary1 -foreground blue -elide $GidPriv(undostate)
    $text tag configure secondary2 -elide 1
    $text tag configure secondary3 -foreground grey -elide $GidPriv(undostate) -lmargin1 25

}

proc SetBackground { color key value index } {

    $value conf -bg $color

}

proc ChangeSelInUndo { text x y } {
    set y [expr $y-[winfo rooty $text]]
    regexp {([^.]*)} [$text index @$x,$y] trash line
    ChangeSelInUndoL $text $line
}
proc ChangeSelInUndoL { text line } {
    set col [$text cget -bg]
    $text dump -window -command "SetBackground $col" selected.first selected.last
    eval [concat $text tag remove selected [$text tag ranges selected]]
    $text tag add selected $line.0 end

    $text dump -window -command "SetBackground red" selected.first selected.last
}


proc tkTextAutoScanNoSel {w} {
    global tkPriv
    if {![winfo exists $w]} return
    if {$tkPriv(y) >= [winfo height $w]} {
	$w yview scroll 2 units
    } elseif {$tkPriv(y) < 0} {
	$w yview scroll -2 units
    } elseif {$tkPriv(x) >= [winfo width $w]} {
	$w xview scroll 2 units
    } elseif {$tkPriv(x) < 0} {
	$w xview scroll -2 units
    } else {
	return
    }
    set tkPriv(afterId) [after 50 tkTextAutoScanNoSel $w]
}

proc ExecUndo { w text } {
    global GidPriv

    $text conf -cursor watch
    ::GidUtils::DisableGraphics
    ::GidUtils::DisableWarnLine
    ::GidUtils::WaitState $w


    set changes [lindex [.central.s info Project] 2]
    set comm "escape escape escape escape files new"
    DWInitUserDataOptions
    if { $changes == 1 } { lappend comm no }

    .central.s process $comm
    if { $GidPriv(ReadFileName) != "" } {
	if { [llength $GidPriv(ReadFileName)] == 1 } {
	    .central.s process escape escape files read $GidPriv(ReadFileName)
	} else {
	    .central.s process escape escape files read \"$GidPriv(ReadFileName)\"
	}
    }

    foreach {key value index} [$text dump -text -window 1.0 selected.first] {
	switch $key {
	    text {
		set value [string trimright $value \n]
		set len [llength $value]
		set ini 0
		set end 99
		while 1 {
		    if { $end >= $len } {
		        set end [expr $len-1]
		    }
		set newval [lrange $value $ini $end]
		if { [llength $newval] == 1 && [llength [lindex $newval 0]] > 1 } {
		      set newval \"[lindex $newval 0]\"
		}
		    .central.s process $newval
		    if { $end == [expr $len-1] } { break }
		    set ini [expr $end+1]
		    incr end 100
		}
	    }
	    window {
		.central.s process escape
	    }
	}
    }

    after idle "::GidUtils::EnableGraphics ; \
	    ::GidUtils::SetWarnLine [list  [_ "Done undo"]] ; \
	    destroy $w ; \
	    .central.s process redraw"
}

proc UndoCom { { w .wundocom } {file ""} } {
    global GidPriv
    if { [info exists GidPriv(undostate)] == 0 } {
	set GidPriv(undostate) 0
    }
    if { $file == "" } {
	set file [.central.s flushBatchToWrite]
	if { $file == "" } {
	    tk_dialogRAM $w.dd [_ "error"] \
		[_ "To enable Undo set on 'Write Batch file' in preferences window"] \
		error 0 [_ "OK"]
	    return
	}
    }
    InitWindow $w [_ "Undo"] UndoComWindowGeom

    frame $w.fr1
    label $w.fr1.l -text [_ "Pick start of undo region"] \
	    -relief groove -bd 2 -font BoldFont
    checkbutton $w.fr1.choose -text [_ "Short list"] -variable GidPriv(undostate) \
	    -command [list Changeundostate $w.t $file]
    text $w.t -xscrollcommand [list $w.hscr set] -yscrollcommand [list $w.vscr set] \
	    -wrap none
    scrollbar $w.vscr -orient vertical -command [list $w.t yview]
    scrollbar $w.hscr -orient horizontal -command [list $w.t xview]

    set def_back [$w cget -background]
    frame $w.frmButtons -bg [CCColorActivo $def_back]
    button $w.frmButtons.btnundo -text [_ "Undo"] \
	    -command [list ExecUndo $w $w.t] \
	    -underline 0 -width 6
    button $w.frmButtons.btnclose -text [_ "Close"] \
	    -command [list destroy $w] \
	    -underline 0 -width 6 

    grid $w.fr1.l $w.fr1.choose -padx 5 -pady 5
    grid $w.fr1
    grid $w.t $w.vscr -sticky nsew
    grid configure $w.vscr -sticky ns -rowspan 2
    grid $w.hscr -sticky ew

    grid $w.frmButtons -sticky ews -columnspan 7
    grid $w.frmButtons.btnundo $w.frmButtons.btnclose -padx 5 -pady 6
    grid columnconf $w 0 -weight 1
    grid rowconfigure $w 1 -weight 1
    
    focus $w.frmButtons.btnclose
    
    bind $w <Alt-c> "tkButtonInvoke $w.frmButtons.btnclose"
    bind $w <Escape> "tkButtonInvoke $w.frmButtons.btnclose"


    FillUndoWin $w.t $file
    bind $w.t <1> "ChangeSelInUndo $w.t %X %Y ; break"
    bind $w.t <B1-Motion> "ChangeSelInUndo $w.t %X %Y ; break"
    bind $w.t <B1-Leave> {
	set tkPriv(x) %x
	set tkPriv(y) %y
	tkTextAutoScanNoSel %W
	break
    }

    bind $w.t <Configure> [list ConfigureTextScroll $w.t $w.hscr $w.vscr]
    #text must be a text or cavas widget and sx,sy scrollbar widgets
    proc ConfigureTextScroll { text sx sy } {
	foreach i "x y" {
	    if { ![info exists s${i}] || ![winfo exists [set s${i}]] } { continue }
	    foreach "${i}1 ${i}2" [$text ${i}view] break
	    
	    if { [set ${i}1] == 0 && [set ${i}2] == 1 } {
		after idle grid remove [set s${i}]
	    } else {
		after idle grid [set s${i}]
	    }
	}
    }

    grab $w
}


#----------------------CopyMove ALONE---------------------------
# if { $UndoComRunningAlone == 1 } {
#     UndoCom .ww /tmp/tmp.batch
# }
#-----------------------------------------------------------------

