#!xtk01 -f
# Geoffrey Furnish                     -*-tcl-*-
# 11 April 1994
#
# @> A script for using Tk to control xtk01
#
# $Id: tk01,v 1.12 1996/02/23 21:04:56 furnish Exp $
#
# $Log: tk01,v $
# Revision 1.12  1996/02/23  21:04:56  furnish
# Add some support for showing off the new rubber banding support in the
# plframe.
#
# Revision 1.11  1995/08/22  16:18:38  mjl
# Rearranged and changed the locator handling to be modal.  Hit 'L' to
# activate.
#
# Revision 1.10  1995/06/29  18:46:57  mjl
# Added code to make it easy to switch between using plframe's and plxframes,
# as a test of each.
#
# Revision 1.9  1995/05/08  20:31:24  mjl
# Eliminated the old enter-leave crosshair bindings since now this is done
# by the plframe automatically (and done better).
#
# Revision 1.8  1995/04/17  19:24:13  furnish
# Embellished to demonstrate (test :-) ways of using Tk bindings with
# plframes.  Boundless possibilities.  Time to stand up and SHOUT!!!
#
# Revision 1.7  1994/07/24  07:42:57  mjl
# Eliminated "destroy ." in favor of "exit".
#
# Revision 1.6  1994/07/01  20:43:59  mjl
# Modified to use startup proc plstdwin when configuring main window.
#
# Revision 1.5  1994/06/17  21:23:09  mjl
# Removed option database settings since they were the same as those set
# in the pldefaults proc.
#
# Revision 1.4  1994/06/09  20:28:15  mjl
# Changed to new improved megawidget instantiation method.
#
###############################################################################

wm title . "x01c -- TK version"
plstdwin .

# If you just want a bare plframe, set this to 0.

set use_plxframe 1

if { $use_plxframe == 1 } then {
    set plwin .plw.plwin
} else {
    set plwin .plwin
}

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

frame .menu -relief raised -borderwidth 3

button .menu.one -text "One" -command "myplot 1"
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 "myplot 3"
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.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 "TK01: Control x01c from TK"

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

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

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

if { $use_plxframe == 1 } then {
    plxframe .plw
    pack append . .plw {left expand fill}

} else {
    plframe .plwin
    pack append . .plwin {left expand fill}
}

bind $plwin L locate_on
bind $plwin <Escape> locate_off

bind $plwin <Control-Button-1> rband_on
bind $plwin <Control-ButtonRelease-1> rband_off

proc rband_on {} {
    global plwin
    $plwin configure -rubberband 1
}

proc rband_off {} {
    global plwin
    $plwin configure -rubberband 0
}

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

# Punch eject and hold onto your seat !!!

proc quit a {
    exit
}

# Utility routine.

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

###############################################################################
# Here is a binding which allows you to obtain the world coordinates of a
# point or points through invoking a "Locate" mode.  You could instead just
# turn it on somewhere in the script and leave it on if you prefer modeless
# behavior, although you can sometimes get into trouble if you move the
# crosshairs while a graphic draw is being performed (X is not forced to
# handle these events serially -- the crosshairs may be updated before the
# graphic draw is complete, and if the graphic draw then obscures the
# crosshairs it will leave traces of the crosshairs on the plot when you
# next move them).  Note especially that in the body of this example
# "get_coords" proc, you can do anything you can do from Tcl.  In
# particular, you need not put the data out to stdout (which is pretty
# boring).  Instead, you can use this to feed the world coords to some more
# complicated Tcl proc of your choosing.  Use the regexp parser to pull out
# the world x and y coordinates, etc.  Have fun!

proc locate_on {} {
    global plwin
    $plwin configure -xhairs 1
    bind $plwin <Shift-1> { get_coords %x %y }
}

proc locate_off {} {
    global plwin
    $plwin configure -xhairs 0
    bind $plwin <Shift-1> {}
}

proc get_coords {x y} {
    global plwin
    puts "world coordinates: [$plwin report wc $x $y]"
}

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