##########################################################################
#
# summ.tcl --Graph summary information
# 
# Author: Dan Gunter
# Date  : 30 July 1998
#
##########################################################################

##################################################
#
# SummInit --
#
#    Initialize summary data lines, etc.
#

proc SummInit { w } {
    global Elts param

#
# Initialize coordinates and
# other persistent attributes.
#

    set lw $param(SL,lw)

    set Elts(Width)   2
    set Elts(Display) [expr $param(SL,inner) / $Elts(Width)]
    set Elts(MinY)    $param(SL,y1)
    set Elts(MaxY)    [expr $param(SL,mid) + $lw]
    set Elts(YRange)  [expr $Elts(MinY)-$Elts(MaxY)]
    set Elts(MinX)    [expr $param(SL,x0) + $lw]
    set Elts(MaxX)    [expr $param(SL,x1) + $lw]
    set Elts(YScale)  1

    #
    # Create a lot of blank elements.
    #
    
    for {set i 0} {$i < $Elts(Display)} {incr i} {
	set Elts($i) [$w create line 0 0 0 0 -tag Elts -width 1]
    }

    return $w
}

##################################################
#
# SummRedraw --
#
#     Redraw all elements from t1 to t2.
#

proc SummRedraw { t1 t2 } {
    global Elts widget
    
    set n [SLSize $t2]
    if { $n == 0 } { return }

    set skip [expr ($n*1.0) / ($Elts(Display)*1.0)]
    set tmskip [expr int(($t2-$t1) * $skip / $n + 1)  ]
    set h [expr ($Elts(YRange)+0.0) / ([SLHighest]+0.00001) ]
    if { $h == 0 } { return }
    # init scan
    catch {
	SLScanStart $t1
    } scan_result
    if { $scan_result != "" } {
	return 
    }
    # main loop
    for { set t $t1; set i 0 } { $t < $t2 } { incr t $tmskip; incr i } {
	set a [SLScanNext $skip]
	set height [expr ($a+0.0) * ($h+0.0)]
	if { $height < 0 } { set height 0 }
	set y [expr int($Elts(MinY) - $height)]
	set x [expr int([SLTimeToPos $t])]
	# draw line
	$widget(Summ) coords $Elts($i) $x $Elts(MinY) $x $y
    }
    # finish scan
    SLScanDone
    # clear unused lines
    for { } { $i < $Elts(Display) } { incr i } {
	$widget(Summ) coords $Elts($i) 0 0 0 0
    }
    # refit
    SLUpdateScale $widget(Summ) $t1 $t2
}

#######################################################################
##        MAIN                                                       ##
#######################################################################

#
# The array of vertical lines
#

global Elts()
