# File recorder.tcl
# 
# Prozeduren, die das Playback-Fenster erzeugen und den Ablauf steuern

proc recorder { name } {

    global chart edge token map draw

    my_trace "Entering recorder"

    set chart($name,recorder_count) 0

    for { set i 0 } { $i <= $chart($name,max_edges) } { incr i } {
        set edge($name,$i,record) 0
    }

    # current edge to display
    set chart($name,current_edge) ""
    
    # delete all items
    .chart$name.canvas.draw delete all
    
    # set ruleheight to -1
    set chart($name,ruleheight) -1

    # draw all of the tokens
    for { set i 0 } { $i <= $chart($name,max_token) } { incr i } {
	draw_token $name $i
    }
    
    if { [winfo exists .chart$name.recorder] == 1 } {
	destroy .chart$name.recorder
    }
    
    toplevel .chart$name.recorder
    wm title .chart$name.recorder "Chart-Playback: $chart($name,headline)"
    
    focus .chart$name.recorder
#    grab .chart$name.recorder
    
    set b [frame .chart$name.recorder.buttonframe]
    
    button $b.back -text "Back" -command "backward $name"
    button $b.play -text "Play" -command "play $name"
    button $b.stop -text "Stop" -command "set chart($name,recorder_flag) 1"
#    button $b.for -text "Forw." -command "set id $map($name,$count);physical_draw_edge $name $id $edge($name,$id,start) $edge($name,$id,end) $edge($name,$id,label) $edge($name,$id,status) $edge($name,$id,extension) $edge($name,$id,children);incr count"

    button $b.for -text "Forw." -command "forward $name"
    
    pack $b.back $b.play $b.stop $b.for -side left
    
    pack $b

    set e [frame .chart$name.recorder.edgeframe -relief raised]
    
    label $e.label -textvariable "chart($name,current_edge)"

    pack $e.label
    
    pack $e
           
    
    set s [frame .chart$name.recorder.statusframe -relief sunken]

    label $s.label0 -text "Total edges / Current edge:"
    label $s.label1 -textvariable "chart($name,max_edges)"
    label $s.label2 -text "/"
    label $s.label3 -textvariable chart($name,recorder_count)

    pack $s.label0 $s.label1 $s.label2 $s.label3 -side left
    
    pack $s
    
    set q [frame .chart$name.recorder.quitframe]

    button $q.quit -text "Quit" -command "destroy .chart$name.recorder"
    
    pack $q.quit

    pack $q
    


}

proc forward { name } {

    global map chart edge

    
    if { $chart($name,recorder_count) == $chart($name,max_edges) } {
        return
    }
    
    set id $map($name,$chart($name,recorder_count),id)

    set chart($name,current_edge) $edge($name,$id,label)

    if { $edge($name,$id,record) != 1 } {
        physical_draw_edge $name $id $edge($name,$id,start) $edge($name,$id,end) $edge($name,$id,label) $edge($name,$id,status) $edge($name,$id,extension) $edge($name,$id,children)
        set edge($name,$id,record) 1
    } else {
        .chart$name.canvas.draw create text $edge($name,$id,xtext) $edge($name,$id,ytext) -text $edge($name,$id,label) -justify center -tags "remove" -font $chart(defaultfont) -fill red
	update
	after 20
	.chart$name.canvas.draw delete remove
	update
	after 20
    }
    incr chart($name,recorder_count)

}

proc backward { name } {

    global map chart edge token
    if { $chart($name,recorder_count) == 0 } {
        set chart($name,current_edge) ""
        return
    }

    if { $chart($name,recorder_count) != 1 } {
        incr chart($name,recorder_count) -2
        set id $map($name,$chart($name,recorder_count),id)
        set chart($name,current_edge) $edge($name,$id,label)
        incr chart($name,recorder_count)
        set id $map($name,$chart($name,recorder_count),id)
    } else {
        set chart($name,current_edge) ""
        incr chart($name,recorder_count) -1
        set id $map($name,$chart($name,recorder_count),id)
    }

    
    if { $map($name,$chart($name,recorder_count),original) == 1 } {
        .chart$name.canvas.draw delete item$id
        if { $edge($name,$id,start) == -1 } {
            # Kante ist eine Regel
            incr chart($name,ruleheight)
        } else {
            if { $edge($name,$id,start) == $edge($name,$id,end) } {
                # Kante hat 0-Span
                incr token($name,$edge($name,$id,start),downspace) -1
            } else {
                # normale Kante
                # setze tokenhoehe auf alten wert
                for { set i $edge($name,$id,start) } { $i < $edge($name,$id,end) } { incr i } {
                    set token($name,$i,height) [lindex $edge($name,$id,tokenheights) [expr $i - $edge($name,$id,start)]]
                }
            }
        }
        set edge($name,$id,record) 0
    } else {
        .chart$name.canvas.draw create text $edge($name,$id,xtext) $edge($name,$id,ytext) -text $edge($name,$id,label) -justify center -tags "remove" -font $chart(defaultfont) -fill red
	update
	after 20
	.chart$name.canvas.draw delete remove
	update
	after 20
    }

    
}

proc play { name } {

    global token chart edge
    
    set chart($name,recorder_flag) 0

    
    while { $chart($name,recorder_count) < $chart($name,max_edges) } {
        if { $chart($name,recorder_flag) != 1 } {
            forward $name
            after 1000
        } else {
            return
        }
    }
}
