#! @TCL@ -f
#
# file: dec_plot_3.tcl
#

# procedure to draw a graph depicting statistics of trace generation
# in a Viterbi trace-passing decoder
#
proc draw_stats_proc {} {
    
    # import globals
    #
    global p

    global black_color
    global blue_color
    global red_color
    global green_color

    global font_0
    global font_1
    global font_2
    global font_3

    global current_frame
    global num_frames

    global traces_new
    global traces_del
    global traces_tot
    
    global trace_off
    global max_traces
    global canvas_width

    global dec_factor

    global x_off
    global y_off

    # remove everything if there is already some trace statistic displayed
    #
    catch {$p.stats delete all}

    # set the limit value
    #
    if {$current_frame < $num_frames} {
	set max_val [expr $dec_factor * $max_traces]
    } else {
	set max_val $traces_new
    }

    # do everything only for valid times
    #
    if {$current_frame > -1} {

	# create labels for the chart
	#
	$p.stats create text 10 30 -text "Active" \
		-font $font_2 -anchor sw -fill $black_color -tags label
	$p.stats create text 10 60 -text "New" \
		-font $font_2 -anchor sw -fill $blue_color -tags label
	$p.stats create text 10 90 -text "Deleted" \
		-font $font_2 -anchor sw -fill $red_color -tags label
	
	# draw rectangles displaying the number of new and deleted traces
	# show the corresponding numbers as well
	#
	set xx_new [expr $trace_off + $traces_new * \
		($canvas_width - $trace_off) / $max_val]
	set xx_del [expr $trace_off + $traces_del * \
		($canvas_width - $trace_off) / $max_val] 
	
	$p.stats create rectangle $trace_off 40 $xx_new 60 -fill $blue_color
	$p.stats create rectangle $trace_off 70 $xx_del 90 -fill $red_color

	$p.stats create text [expr $xx_new + $x_off] 60 \
		-text $traces_new -font $font_2 -anchor sw \
		-fill $blue_color -tags num
	$p.stats create text [expr $xx_del + $x_off] 90\
		-text $traces_del -font $font_2 -anchor sw \
		-fill $red_color -tags num

	# draw rectangles displaying the number of active traces
	# show the corresponding numbers as well
	#
	if {$current_frame < $num_frames} {
	    set num_old [expr $traces_tot - $traces_new + $traces_del]
	    set xx_old [expr $trace_off + $num_old * \
		    ($canvas_width - $trace_off) / $max_val] 
	    set xx_tot [expr $trace_off + $traces_tot * \
		    ($canvas_width - $trace_off) / $max_val] 
	    $p.stats create rectangle $trace_off 10 $xx_old 30 \
		    -fill $black_color
	    $p.stats create rectangle $xx_old 10 $xx_tot 30 -fill $green_color
	    $p.stats create text [expr $xx_tot + $x_off] 30 \
		    -text $traces_tot -font $font_2 -anchor sw \
		    -fill $black_color -tags num
	} else {
	    set num_old [expr $traces_new - $traces_del]
	    set xx_old [expr $trace_off + $num_old * \
		    ($canvas_width - $trace_off) / $max_val] 
	    $p.stats create rectangle $trace_off 10 $xx_old 30 \
		    -fill $black_color
	    $p.stats create text [expr $xx_old + $x_off] 30 \
		    -text $traces_tot -font $font_2 -anchor sw \
		    -fill $black_color -tags num
	}
    }

    # update scroll region
    #
    set size  [$p.stats bbox all]
    set x0 [expr [lindex $size 0] - $y_off]
    set y0 [expr [lindex $size 1] - 5]
    set x1 [expr [lindex $size 2] + $y_off]
    set y1 [expr [lindex $size 3] + 2]
    
    set size {}
    set size [linsert $size 0 $x0 $y0 $x1 $y1]
    
    $p.stats configure -scrollregion $size
}

# end of file
#
