#################################
# The Plot Module
#
# Part of the SharpeSound Editor
# by Paul D. Sharpe
# A 3rd Year Project at the University of Leeds;
# Department of Electronic and Electrical Engineering.
# Date: 27/2/95
#
# Revision:1 for Tk4.0
# Date:  
set PLOT(Startx) 0
set PLOT(Endx) 0

#################################
# The plot Module
#
# Part of the SharpeSound Editor
# by Paul D. Sharpe
# A 3rd Year Project at the University of Leeds;
# Department of Electronic and Electrical Engineering.
#
# Revision:0
# Date: 27/2/94

##########################
# plotbox - Is the procedure that ...
# There are 6 options.
# 1. init : this sets up   but doesn't display them.
# 2. show : this packs.
# 3. noshow this unpacks.
# 4. disabled : this disables all the buttons.
# 5. normal : re-enables all the buttons.
# 6. reset : redraws the window; useful if color settings have been changed
proc PlotBox { option } {

  #++++++++++++++++++++++
  # Get User Settings
  global ConfigSet
  global Menu_anc PLOT
  set PLOT(Height) 100
  set PLOT(Width) 400

  if {$option == "init" } {
if {![winfo exists .plotbox]} {
	frame .plotbox
	label .plotbox.title -text Plot
	frame .plotbox.cs
	canvas .plotbox.cs.canvas
	scrollbar .plotbox.cs.scroll -orient horizontal\
				-command ".plotbox.cs.canvas xview"
	.plotbox.cs.canvas configure -xscrollcommand {.plotbox.cs.scroll set}
	frame .plotbox.cs.marker
	frame .plotbox.cs.marker.start
	label .plotbox.cs.marker.start.label -text "Point 1"
	label .plotbox.cs.marker.start.data -textvariable PLOT(Start)	
	pack 	.plotbox.cs.marker.start.data\
		.plotbox.cs.marker.start.label\
		-side top -fill x -expand 1
	frame .plotbox.cs.marker.end
 	label .plotbox.cs.marker.end.label -text "Point 2"
	label .plotbox.cs.marker.end.data -textvariable PLOT(End)
	pack 	.plotbox.cs.marker.end.data\
		.plotbox.cs.marker.end.label\
		-side top -fill x -expand 1
	frame .plotbox.cs.marker.zoom
	label .plotbox.cs.marker.zoom.label -text "Zoom Level"
	scrollbar .plotbox.cs.marker.zoom.data\
			-command scrollcb\
			-orient horizontal
	# Callback to the scrollbar
	proc scrollcb {value} {
		if {$value==1} { Zoom up}\
		elseif {$value==-1} {Zoom down}
	}
	label .plotbox.cs.marker.zoom.data.label\
			-textvariable PLOT(ZoomCount) 
	pack .plotbox.cs.marker.zoom.data.label\
		-pady 1
		
	pack 	.plotbox.cs.marker.zoom.data -side top -fill x  
	pack	.plotbox.cs.marker.zoom.label\
		-side top -fill x -expand 1

	pack 	.plotbox.cs.marker.start\
		.plotbox.cs.marker.zoom\
		.plotbox.cs.marker.end\
		-padx 5 -side left -fill x -expand 1

	pack 	.plotbox.cs.canvas \
		.plotbox.cs.scroll \
		 -fill both -expand 1

	pack .plotbox.cs.marker -pady 2 -expand 1


	pack .plotbox.title -fill x -expand 1
	pack .plotbox.cs -padx 10


	# Bindings for Canvas
	# Button 1: start marking a block
	bind .plotbox.cs.canvas <Button-1> { 
		global PLOT
		set PLOT(Startx) [expr round([.plotbox.cs.canvas canvasx %x])]
		if {$PLOT(Startx)>$PLOT(Total)} { set PLOT(Startx) $PLOT(Total)}
		set PLOT(Endx) $PLOT(Startx)
		#reset marked area
		MarkPlot
		if {[info exists PLOT(MarkBox)] } {.plotbox.cs.canvas raise $PLOT(MarkBox) graph}
	set FLAG(Marked) 0
	}

	# Button 1 motion mark an area
	bind .plotbox.cs.canvas <B1-Motion> {
 
		# if mouse is dragged beyound the limits of the visual canvas
		# continue marking  into rest of canvas.

		# Right limit.
		if {%x > $PLOT(Width) } { .plotbox.cs.canvas scan mark %x %y
					.plotbox.cs.canvas scan dragto [expr %x-10] 0 } 
		# Left limit.
		if {%x < 0 } { .plotbox.cs.canvas scan mark %x %y
					.plotbox.cs.canvas scan dragto [expr %x+10] 0 }
		# Set the end marker
		set PLOT(Endx) [expr round([.plotbox.cs.canvas canvasx %x])]
		if {$PLOT(Endx)>$PLOT(Total)} { set PLOT(Endx) $PLOT(Total)}
		# Mark the area.	
		MarkPlot
		if {[info exists PLOT(MarkBox)] } {.plotbox.cs.canvas lower $PLOT(MarkBox) graph}
			set FLAG(Marked) 1
	}

	# Button 2 : adjust marked area up to pointer
	bind .plotbox.cs.canvas <Button-2> {
		# Set the end marker
 		set PLOT(Endx) [expr round([.plotbox.cs.canvas canvasx %x])]
		if {$PLOT(Endx)>$PLOT(Total)} { set PLOT(Endx) $PLOT(Total)}
		# Mark the area.
		MarkPlot
		if {[info exists PLOT(MarkBox)] } {.plotbox.cs.canvas lower $PLOT(MarkBox) graph}
		set FLAG(Marked) 1
	}

	# Button 3: Drag the canvas view.
	bind .plotbox.cs.canvas <Button-3> { .plotbox.cs.canvas scan mark %x %y }
	bind .plotbox.cs.canvas <B3-Motion> { .plotbox.cs.canvas scan dragto %x %y }
}

	# Configure Widgets
	.plotbox config -background $ConfigSet(cBG1)\
			-relief $ConfigSet(REL) -bd $ConfigSet(BDW)
	.plotbox.title config \
			-background $ConfigSet(cBG1) \
			-foreground $ConfigSet(cFG1) \
			-anchor w\
			-font $ConfigSet(FNT_S)	
	.plotbox.cs config	-background $ConfigSet(cBG1)
	.plotbox.cs.canvas config -background $ConfigSet(cBG3)\
				-width $PLOT(Width)\
				-height $PLOT(Height)\
				-relief sunken\
				-cursor crosshair
	.plotbox.cs.scroll config -background $ConfigSet(cBG1) \
				-activebackground $ConfigSet(cABG)
	.plotbox.cs.marker config -background $ConfigSet(cBG1) -bd 2
	.plotbox.cs.marker.start config -background $ConfigSet(cBG1) 
	.plotbox.cs.marker.start.label config\
			-background $ConfigSet(cBG1) \
			-foreground $ConfigSet(cFG1) \
			-anchor w\
			-font $ConfigSet(FNT_S)
			
	.plotbox.cs.marker.start.data config\
			-background $ConfigSet(cBG2) \
			-foreground $ConfigSet(cFG2) \
			-anchor c\
			-font $ConfigSet(FNT_L)\
			-width 15\
			-relief groove
	.plotbox.cs.marker.end config -background $ConfigSet(cBG1) 
 	.plotbox.cs.marker.end.label config\
			-background $ConfigSet(cBG1) \
			-foreground $ConfigSet(cFG1) \
			-anchor e\
			-font $ConfigSet(FNT_S)
			
	.plotbox.cs.marker.end.data config\
			-background $ConfigSet(cBG2) \
			-foreground $ConfigSet(cFG2) \
			-anchor c\
			-font $ConfigSet(FNT_L)\
			-width 15\
			-relief groove

	.plotbox.cs.marker.zoom config -background $ConfigSet(cBG1) 
	.plotbox.cs.marker.zoom.label config\
			-background $ConfigSet(cBG1) \
			-foreground $ConfigSet(cFG1) \
			-anchor w\
			-font $ConfigSet(FNT_S)
	.plotbox.cs.marker.zoom.data config\
			-background $ConfigSet(cBG2) \
			-activebackground $ConfigSet(cABG)\
			-bd 2\
			-relief groove
	.plotbox.cs.marker.zoom.data.label config\
			-background $ConfigSet(cBG2) \
			-foreground $ConfigSet(cFG2) \
			-anchor c\
			-font $ConfigSet(FNT_L)
  }\
elseif {[winfo exists .plotbox]} {
	if {$option == "show"} {
	global PACK
	if {$PACK(PlotBox) != ""} {	
	 pack .plotbox -fill x -expand 1 -after $PACK(PlotBox)
	} else {
	 pack .plotbox -fill x -expand 1 -after
	}
	}\
	elseif {$option == "disabled" || $option=="normal"} { }\
	elseif {$option == "noshow" } { pack forget .plotbox} \
  	elseif {$option == "init"} {# A try to use init again;Ignore This case} \
 	elseif {$option == "reset"} {
		set packflag [llength [pack info .infobar]]
		destroy .plotbox
		# DEBUG LINE
		source menu.tk
		PlotBox init
		if {$packflag} {PlotBox show}
	}
}
}

proc MarkPlot {} {
  global PLOT ConfigSet LOAD
  if {[info exists PLOT(MarkBox)] } {
	.plotbox.cs.canvas coord $PLOT(MarkBox) $PLOT(Startx) 0 $PLOT(Endx) $PLOT(Height)
	.plotbox.cs.canvas itemconfigure m1 -fill $ConfigSet(cFG3)
	.plotbox.cs.canvas dtag m1
	.plotbox.cs.canvas addtag m1 enclosed $PLOT(Startx) 0 $PLOT(Endx) $PLOT(Height)
	.plotbox.cs.canvas itemconfigure m1 -fill $ConfigSet(cFG2)
	set PLOT(Start) [expr (($PLOT(Startx)*$PLOT(Blocksize))+$PLOT(Offset))/$LOAD(rate).0]
	set PLOT(End) [expr ( ($PLOT(Endx)*$PLOT(Blocksize))+$PLOT(Offset))/$LOAD(rate).0]
	# And now formatted
	set PLOT(Start) [Sec2Time $PLOT(Start)]
	set PLOT(End) [Sec2Time $PLOT(End)]
  }

}

proc FillPlot {  {blocksize init} { opt default}} {
global PLOT ConfigSet SAMPLE FILE LOAD

if {$opt=="default"} {
	set file $FILE(TMP1)
} else { set file $opt }
set mask [expr (($LOAD(bit)>>4)<<1) + ($LOAD(chnl)>>1) ]

# Set an initial block size based on the number of samples 
if {$blocksize=="init"} {
	   set maxmin [exec $FILE(SRC)/maxmin 100 $mask $file] 
	   set length [join [lindex $maxmin 1]]
	   set Plot(Total) [expr $PLOT(Width) -5]
	   set PLOT(Blocksize) [expr int(round($length/$PLOT(Width).0))]
	   if {$opt=="default"} {set PLOT(MaxBlockSize) $PLOT(Blocksize)
				set PLOT(Offset) 0 
				set SAMPLE(Size) $length
			  	# Update InfoBar
				InfoFill	
				Zoom init}
} elseif {$blocksize=="delete"} {
	# Delete complete contents 
	.plotbox.cs.canvas delete all
	catch {unset PLOT(MarkBox)}
	set PLOT(Startx)		0
	set PLOT(Endx)			0
	set PLOT(Start) 	[Sec2Time 0]
	set PLOT(End) 		[Sec2Time 0]
	set PLOT(Total)		$PLOT(Width)
	set PLOT(Blocksize)		0
	set PLOT(Zoom) 			100
	return
} \
else {
	set PLOT(Blocksize) $blocksize
}

# Get points with new blocksize
if {$PLOT(Blocksize)>0} {
set maxmin [exec $FILE(SRC)/maxmin $PLOT(Blocksize) $mask $file]
set PLOT(Points) [lindex $maxmin 0]
} else {FillPlot delete; return}


set i 0
# Delete any previous plot 
.plotbox.cs.canvas delete graph

# Plot the points
set count 0
foreach point $PLOT(Points) {
incr count
# get from normalised values to Canvas coordinates
set min [expr round( [lindex $point 0] *($PLOT(Height)))]
set max [expr round( [lindex $point 1] *($PLOT(Height)))]

# Ensure that a dot is placed
if {$min==$max} { incr max}
.plotbox.cs.canvas create line $i $min $i $max -tags graph -fill $ConfigSet(cFG3)
		
incr i

}
# Set the total lines drawn
set PLOT(Total) $count

# Set the marking area
if {[info exists PLOT(MarkBox)]} { 
	MarkPlot
} else {
    # Configure marking area
    set PLOT(MarkBox) [.plotbox.cs.canvas create rectangle 0 0 0 $PLOT(Height) \
		-fill $ConfigSet(cFG3) -outline $ConfigSet(cFG2) ]
    set PLOT(Startx) 0
    set PLOT(Endx) 0
    MarkPlot
}

# Put Marking Box below the graph
.plotbox.cs.canvas lower $PLOT(MarkBox) graph 
# Set the canvas range
.plotbox.cs.canvas configure -scrollregion " 0 0 $PLOT(Total) $PLOT(Height) "

# Remove numerical data from memory
unset PLOT(Points)

}
