#!../blt_wish -f

if [file exists ../library] {
    set blt_library ../library
}
blt_bitmap define pattern1 { {4 4} {01 02 04 08} }
option add *Blt_graph.font *New*Century*Bold*R*14* 
option add *Blt_htext.Font *Times*Bold-R*14*
option add *Blt_graph.textFont *new*century*140*

set visual [winfo screenvisual .]
if { $visual != "staticgray" && $visual != "grayscale" } {
    option add *Button.Background red
    option add *Blt_graph.foreground navyblue
    option add *Blt_graph.BorderWidth 2
    option add *Blt_graph.relief sunken 
    option add *Blt_graph.textTagForeground black
    option add *Blt_graph.textTagBackground yellow
    option add *Blt_graph.lineTagForeground black
    option add *Blt_graph.lineTagBackground yellow
    option add *Blt_graph.polyTagBackground dodgerblue
    option add *Blt_graph.polyTagForeground white
}

option add *Blt_graph.BorderWidth 	2
option add *Blt_graph.relief 		sunken 
option add *Blt_graph.title 		"Another XY Graph"
option add *Blt_graph.xTitle 		"X Axis Label"
option add *Blt_graph.yTitle 		"Y Axis Label"

blt_htext .msg -text {\
In this example, you can sweep out a box to zoom in by clicking with 
the left button and dragging the pointer.  To restore the original view,
simply click on the middle button.  
Hit the %%
button $blt_htext(widget).quit -text {Quit} -command {exit} 
$blt_htext(widget) append $blt_htext(widget).quit
%% button when you've seen enough. %%
label $blt_htext(widget).logo -bitmap BLT 
$blt_htext(widget) append $blt_htext(widget).logo
%%}

blt_graph .graph 

.graph xaxis configure -step 90 -command FormatXLabels -subticks 0
.graph yaxis configure -rotate 90

proc FormatXLabels {graph x} {
     return "[expr int($x)]\260"
}

.graph element create line1 \
    -fg navyblue \
    -label sin(x) \
    -linewidth 2 

.graph element create line2 \
    -symbol circle \
    -fg red \
    -bg pink \
    -label cos(x) \
    -linewidth 4 

set x [expr 3.14159265358979323846/180.0]
puts stderr "Generating data..."
for { set i -360 } { $i <= 360 } { incr i 5 } {
    set radians [expr $i*$x]
    .graph element append line1 { $i sin($radians) }
    .graph element append line2 { $i cos($radians) }
}
puts stderr "done."

set sine [lindex [.graph element configure line1 -data] 4]
set fill [concat -360 -Inf $sine 360 -Inf ]
.graph tag create polygon $fill -stipple pattern1

pack append .  \
	.graph { fill expand } \
	.msg  { padx 20 pady 10 } 
wm min . 0 0

proc get.coords { w sx sy xVar yVar } {
    # w   widget
    # sx  screen x position
    # sy  screen y position
    
    scan [$w locate $sx $sy] "%s %s" x y 
    scan [$w xaxis limits] "%s %s" xmin xmax
    scan [$w yaxis limits] "%s %s" ymin ymax
    if { $x > $xmax } { set x $xmax }
    if { $x < $xmin } { set x $xmin }
    if { $y > $ymax } { set y $ymax }
    if { $y < $ymin } { set y $ymin }
    global $xVar $yVar
    set $xVar $x
    set $yVar $y
}

proc swap { var1 var2 } {
    global $var1 $var2
    set hold [set $var1]
    set $var1 [set $var2]
    set $var2 $hold
}

proc get.anchor { w sx sy } {
    # w   widget
    # sx  screen x position
    # sy  screen y position
    
    global x1 y1 x2 y2
    set x2 "" ; set y2 ""
    get.coords $w $sx $sy x1 y1
    bind $w <B1-Motion> { scan.xy %W %x %y }
    bind $w <ButtonRelease-1> { zoom.xy %W %x %y }
}

set tag1 {}
set tag2 {}
set outline {}

proc box { w x1 y1 x2 y2 } {
    global tag1 tag2 outline

    set text [format "%.4g,%.4g" $x1 $y1] 
    if { $tag1 == "" } then {
	set tag1 [$w tag create text {$x1 $y1} -text $text ] 
    } else {
	$w tag configure $tag1 -text $text 
	$w tag coords $tag1 "$x1 $y1"
    }
    set text [format "%.4g,%.4g" $x2 $y2] 
    if { $tag2 == "" } then {
	set tag2 [$w tag create text {$x2 $y2} -text $text ] 
    } else {
	$w tag configure $tag2 -text $text 
	$w tag coords $tag2 "$x2 $y2"
    }
    set coords {
	$x1 $y1 $x1 $y2 $x1 $y1 $x2 $y1 $x2 $y1 $x2 $y2 $x1 $y2 $x2 $y2 
    }
    if { $outline == "" } then {
	set outline [$w tag create line $coords]
    } else {
	$w tag coords $outline $coords
    }
}

set lastX 0
set lastY 0

proc scan.xy { w sx sy } {
    # w   widget
    # sx  screen x position
    # sy  screen y position
    
    global x1 y1 x2 y2
    global lastX lastY
    set deltaX [expr abs($lastX-$sx)]
    set deltaY [expr abs($lastY-$sy)]
    if { ($deltaX < 5) && ($deltaY < 5) } {
	return
    }	
    set lastX $sx
    set lastY $sy
    get.coords $w $sx $sy x2 y2
    if { $x1 > $x2 } { 
	box $w $x2 $y2 $x1 $y1
    } else {
	box $w $x1 $y1 $x2 $y2
    }
}

proc zoom.xy { w sx sy } {
    # w   widget
    # sx  screen x position
    # sy  screen y position
    
    global x1 y1 x2 y2
    global tag1 tag2 outline
    # Go back to original bindings
    bind $w <ButtonPress-1> { get.anchor %W %x %y }
    catch {$w tag delete $tag1 $tag2} msg
    set tag1 {} ; set tag2 {}
    bind $w <B1-Motion> {}
    if { $x2 == "" } then {
	catch "$w tag delete $outline " msg
	set outline {} 
	$w xaxis configure -min {} -max {} 
	$w yaxis configure -min {} -max {}
	return
    }
    if { $x1 > $x2 } { 
	$w xaxis configure -min $x2 -max $x1 
    } else { 
	if { $x1 < $x2 } {
	    $w xaxis configure -min $x1 -max $x2 
	}
    }
    if { $y1 > $y2 } { 
	$w yaxis configure -min $y2 -max $y1
    } else {
	if { $y1 < $y2 } {
	    $w yaxis configure -min $y1 -max $y2
	}
    }
    $w configure -cursor crosshair 
    $w tag delete $outline
    set outline {}
}

bind .graph <ButtonPress-1> { 
    get.anchor %W %x %y ; %W configure -cursor {crosshair red black}
}

bind .graph <ButtonPress-2> { 
    catch {%W tag delete $outline}
    set outline {} 
    %W yaxis configure -min {} -max {} 
    %W xaxis configure -min {} -max {}
}

bind .graph <ButtonPress-3> { 
    %W postscript trig.ps -pagewidth 6.5i -pageheight 8.5i -landscape true
}

