#!/usr/local/bin/wish -f

# Copyright 1994 by Frank Adelstein.  All Rights Reserved.
# 
#  Permission to use, copy, modify, and distribute this software and its
#  documentation for any purpose is hereby granted without fee, provided that
#  the above copyright notice appear in all copies and that both that
#  copyright notice and this permission notice appear in supporting
#  documentation, and that the authors name not be used in advertising or
#  publicity pertaining to distribution of the software without specific,
#  written prior permission.  The author makes no representations about the
#  suitability of this software for any purpose.  It is provided "as is"
#  without express or implied warranty.
# 
#  THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 
#  INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, 
#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT 
#  OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 
#  LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 
#  NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
#  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#
# cool little tcl/tk program to let you put up the bitmaps and move them 
# around in order to figure out the offsets, since each image is cropped 
# to save space.
#

frame .menubar -relief raised -borderwidth 1
button .menubar.b1 -text quit -command {destroy .}
button .menubar.b2 -text "New Bitmap" -command {newBitmap }
button .menubar.b3 -text "Set Zero" \
	-command {set plot(zeroX) $plot(lastX) ; set plot(zeroY) $plot(lastY)}
label .menubar.l1 -relief raised -borderwidth 2 -text "X = 0 Y = 0"
canvas .canvas -width 500 -height 500
pack append . .menubar {top fillx}
pack append .menubar .menubar.b1 { left pady 0.2c } \
                     .menubar.b2 { left pady 0.2c } \
                     .menubar.b3 { left pady 0.2c } \
            .menubar.l1 {right pady 0.2c}
pack append . .canvas {bottom fillx filly}

.canvas create bitmap 0 0 -bitmap @[lindex $argv 0] -tag logo -anchor nw
.canvas create line 250 0 250 500
.canvas create line 0 250 500 250
.canvas bind logo <B1-Motion> "plotMove .canvas %x %y"
set plot(lastX) 0
set plot(zeroX) 0
set plot(lastY) 0
set plot(zeroY) 0

proc plotMove {w x y} {
  global plot
  $w move logo [expr $x-$plot(lastX)] [expr $y-$plot(lastY)]
  set plot(lastX) $x
  set plot(lastY) $y
  set tempx [expr $plot(lastX)-$plot(zeroX)]
  set tempy [expr $plot(lastY)-$plot(zeroY)]
  .menubar.l1 configure -text "X = $tempx ($plot(lastX)) Y = $tempy ($plot(lastY))"
}

proc newBitmap {} {
  toplevel .popup -height 50
  wm transient .popup .
  wm geometry .popup 300x50+300+300
  frame .popup.frame -relief raised -border 1 -height 50
  pack append .popup .popup.frame {top fillx}
  label .popup.frame.label -relief raised -border 1  \
                           -text "File name of bitmap to use:"
  text .popup.frame.text -relief raised -border 1 -height 50
  .popup.frame.text insert 1.0 [lindex [.canvas itemconfig logo -bitmap] 4]
  .popup.frame.text delete 1.0 1.1
  bind .popup.frame.text <Return> {.canvas itemconfig logo -bitmap @[.popup.frame.text get 1.0 end]; destroy .popup}
  pack append .popup.frame .popup.frame.label {top fillx} \
                          .popup.frame.text {top fillx}
}
