#!/usr/share/doc/plplot-doc/examples/tk/xtk02 -f
#                     -*-tcl-*-
# Geoffrey Furnish
# 11 April 1994
#
# @> A script for using Tk to control xtk02, using the PLplot itcl interface.
###############################################################################

package require Pltk
eval package require $pl_itk_package_name

wm title . "x01c -- Tk/Itcl version"
plstdwin .

###############################################################################
# Set up the menubar and message widgets.

frame .menu -relief raised -borderwidth 3

button .menu.one -text "One" -command "myplot1"
pack append .menu .menu.one {left expand fill}

button .menu.two -text "Two" -command "myplot 2"
pack append .menu .menu.two {left expand fill}

button .menu.three -text "Three" -command "plot2"
pack append .menu .menu.three {left expand fill}

button .menu.four -text "Four" -command "myplot 4"
pack append .menu .menu.four {left expand fill}

button .menu.five -text "Five" -command "gumbyplot"
pack append .menu .menu.five {left expand fill}

button .menu.six -text "Six" -command "snoopy"
pack .menu.six -side left -expand 1 -fill x

button .menu.exit -text "Exit" -command "quit 0"
pack append .menu .menu.exit {right expand fill}

message .msg \
	-font -Adobe-helvetica-medium-r-normal--*-240* -aspect 200 \
	 -width 500 -borderwidth 1 \
	-text "TK02: Demo \[incr Tcl\] interface to PLplot"

#PLXWin .plw
Pltkwin .plw

pack append . .menu {top fillx} \
	.msg {top padx 5 pady 5 fill} \
    .plw {bottom expand fill}

update

tk_menuBar .menu .menu.one .menu.two .menu.three .menu.four .menu.exit

###############################################################################
# Definitions of procedures used in this script.

proc myplot1 {} {
    global xscale yscale xoff yoff

    set xscale 6
    set yscale 1
    set xoff 0
    set yoff 0

    plot1
}

# This is supposed to work just like the plot1() in x01c.c/xtk02.c

proc plot1 {} {
    global xscale yscale xoff yoff

    matrix x1 float 10
    matrix y1 float 10

    for {set i 2; set n1 0} {$i < 60} {incr i 10; incr n1} {
	set x [expr $xoff + $xscale * ($i + 1) / 60]
	x1 $n1 = $x
	y1 $n1 = [expr $yoff + $yscale * pow($x,2)]
    }

    set n2 60
    matrix x2 float $n2
    matrix y2 float $n2

    for {set i 0} {$i < $n2} {incr i} {
	set x [expr $xoff + $xscale * ($i + 1) / $n2]
	x2 $i = $x
	y2 $i = [expr $yoff + $yscale * pow($x,2)]
    }

    set xmax [x2 [expr $n2-1]]
    set ymax [y2 [expr $n2-1]]

    .plw plcol0 1
    .plw plenv $xoff $xmax $yoff $ymax 0 0
    .plw plcol0 6
    .plw pllab "(x)" "(y)" "#frPLPLOT Example 1 - y=x#u2"

    # plot the data points

    .plw plcol0 9
    matrix xplot f $n1 = [x1 :]
    matrix yplot f $n1 = [y1 :]
    .plw plpoin xplot yplot 9

    # plot the data points

    .plw plcol0 4
    .plw plline x2 y2
}

# This is supposed to work just like the plot2() in x01c.c/xtk02.c

proc plot2 {} {
    .plw plcol0 1
    .plw plenv -2 10 -.4 1.2 0 1
    .plw plcol0 2
    .plw pllab "(x)" "sin(x)/x" "#frPLPLOT Example 1 - Sinc Function"

    # Fill up the array

    matrix x1 float 101
    matrix y1 float 101

    for {set i 0} {$i < 101} {incr i} {
	set x [expr ($i - 19.)/6.]
	x1 $i = $x
	y1 $i = 1
	if {$x != 0} { y1 $i = [expr sin($x)/$x] }
    }

    .plw plcol0 3
    .plw plline x1 y1
}

proc gumbyplot {} {
    .plw plcol0 1
    .plw plenv 0 1 0 1 0 0
    .plw plcol0 6
    .plw pllab "(x)" "(y)" "#frPLplot Example 1 - y=1-2x+2x#u2"

    matrix x1 float 101
    matrix y1 float 101

    for {set i 0} {$i < 101} {incr i} {
	set x [expr $i * .01]
	x1 $i = $x
	y1 $i = [expr 1 - 2 * $x + 2 * $x * $x]
    }

    .plw plline x1 y1
}

# This proc shows off the use of a matrix extension subcommand.

proc snoopy {} {

    matrix x float 101
    matrix y float 101

    for {set i 0} {$i < 101} {incr i} {
	set xx [expr $i * .01]
	x $i = $xx
    }

    y stuff

    .plw plcol0 1
    .plw plenv 0 1 0 1 0 0
    .plw plcol0 6
    .plw pllab "(x)" "(y)" "#frDemo of Matrix extension subcommand"
    .plw plline x y
}

# Punch eject and hold onto your seat !!!

proc quit a {
    exit
}

# Utility routine.

proc dpos w {
    wm geometry $w +300+300
}

###############################################################################
