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

# Copyright(c) 1998, Space Science and Engineering Center, UW-Madison
# Refer to "McIDAS Software Acquisition and Distribution Policies"
# in the file  mcidas/data/license.txt

# $Id: imgdisp.gui,v 1.37 1998/10/13 20:26:36 dglo Exp $

set guipath $env(GUIDIR)
set auto_path "$guipath $auto_path"
set level 1
setoptions

#
#
#	GUI for IMGDISP command
#
#
#
wm title . "McIDAS: IMGDISP"
wm iconname . "Display Image"
placegui .
frame .frame  -borderwidth 10
#
#	One line explanation of the command
#

#	Create buttons which line up at the top and will pack and
#	unpack frames depending upon the users level of expertise.
#	These will not be needed for all commands , as some are
#	too easy for progressive disclosure.
#
frame .top
button .top.low -text "Less" -command {set level 1
		pack forget .frame.hi
		pack forget .frame.hi
		pack forget .top.low
		pack .top.hi	-side left -expand yes
		pack .frame.low	-side top  -expand yes}
button .top.hi  -text "More" -command {set level 3;
	pack append .frame  \
		.frame.low			{top fill} \
		.frame.hi			{top fill}
	pack forget .top.hi
	pack append .top \
		.top.low		{left expand} }

button .top.help -text "Help" -command "mkHelp"
pack append .top \
	.top.hi			{left expand} \
	.top.help		{right expand}

message .msg -width 500 \
-font $messfont -borderwidth 1 -text "Display Image"

frame .frame.low

frame .frame.low.options -relief raised -borderwidth 2

frame .frame.low.options.top

set getpos NO

radiobutton .frame.low.options.top.dataset -text "Load Dataset" \
	-variable option -width 18	\
	-value dataset -anchor w		\
	-command {set getpos NO
		set image ""
		pack forget .frame.low.latest
		pack forget .frame.low.single
		pack forget .frame.low.update
		pack .frame.low.dataset -side top -fill x -expand yes}

radiobutton .frame.low.options.top.latest -text "Load Latest Image" \
	-variable option  -width 18	\
	-value latest -anchor w		\
	-command {set getpos NO
		set image ""
		pack forget .frame.low.dataset
		pack forget .frame.low.single
		pack forget .frame.low.update
		pack .frame.low.latest -side top -fill x -expand yes}

pack .frame.low.options.top.dataset -side left
pack .frame.low.options.top.latest -side right
pack .frame.low.options.top -side top

frame .frame.low.options.bottom

radiobutton .frame.low.options.bottom.single -text "Load One Image" \
	-variable option  -width 18	\
	-value single -anchor w		\
	-command { pack .frame.low.single -side top -fill x -expand yes
		set image ""
		set getpos YES
		pack forget .frame.low.dataset
		pack forget .frame.low.latest
		pack forget .frame.low.update }

radiobutton .frame.low.options.bottom.update -text "Update Loop" \
	-variable option  -width 18	\
	-value update -anchor w		\
	-command { pack .frame.low.update -side top -fill x -expand yes
		set image ""
		set getpos NO
		pack forget .frame.low.dataset
		pack forget .frame.low.latest
		pack forget .frame.low.single }

pack .frame.low.options.bottom.single -side left  -expand yes
pack .frame.low.options.bottom.update -side right  -expand yes
pack .frame.low.options.bottom -side top \
	-fill x -padx 10 -pady 10 -expand yes

pack .frame.low.options -side top -pady 2

set nextproc ListDataset
set datatype IMAGE
set thevar image

frame .frame.low.ds  -borderwidth 2 -relief groove

menubutton .frame.low.ds.menu -relief raised -text "Dataset"\
	-menu .frame.low.ds.menu.options
set posvar getpos
menu .frame.low.ds.menu.options -tearoff 0
makegroupbuttons .frame.low.ds.menu.options $nextproc $datatype $posvar $thevar

entry .frame.low.ds.entry	\
	-textvariable image	\
	-relief sunken		\
	-width 30

pack .frame.low.ds.menu -side left -padx 2
pack .frame.low.ds.entry -side right -padx 2
pack .frame.low.ds -side top -fill both -pady 10

frame .frame.low.locate

menubutton .frame.low.locate.menu	\
	-text "Center Location"		\
	-menu .frame.low.locate.menu.choices\
	-relief raised -width 15

set location " "
set station " "
set latlon " "
menu .frame.low.locate.menu.choices -tearoff 0

.frame.low.locate.menu.choices add command	\
	-label "Station"			\
	-command {set type SURF
		  set latlon " "
		  set location " "
		  getsta 0}

.frame.low.locate.menu.choices add command	\
	-label "Latitude/Longitude"		\
	-command {set station " "
		  set location " "
		  getlatlon}

entry .frame.low.locate.entry	\
	-textvariable location	\
	-relief sunken		\
	-width	8

pack .frame.low.locate.menu -side left -padx 10
pack .frame.low.locate.entry -side right -padx 10
pack .frame.low.locate -side top -fill both -pady 2

set band " "
frame .frame.low.band

button .frame.low.band.button	\
	-command {GetBand $image $band }	\
	-text Band		\
	-relief raised -width 13

entry .frame.low.band.entry	\
	-textvariable band	\
	-relief sunken		\
	-width		8

pack .frame.low.band.button -side left -padx 10
pack .frame.low.band.entry -side right -padx 10
pack .frame.low.band -side top	-fill both -pady 2

frame .frame.low.dataset
button .frame.low.dataset.button -text "Frame Range"\
	-command "pickfrange" -width 13
entry .frame.low.dataset.bframe -textvariable bframe\
	-relief sunken -width 4
entry .frame.low.dataset.eframe -textvariable eframe\
	-relief sunken -width 4

pack .frame.low.dataset.button -side left -padx 10
pack .frame.low.dataset.eframe -side right
pack .frame.low.dataset.bframe -side right

frame .frame.low.latest
button .frame.low.latest.button -text "Frame "\
	-command "pickframe" -width 13
entry .frame.low.latest.frame -textvariable dframe\
	-relief sunken -width 8

pack .frame.low.latest.button -side left -padx 10
pack .frame.low.latest.frame -side right -padx 10

set dframe [UCpeek current_frame]
set bframe $dframe
set eframe $dframe
set dframe Current
frame .frame.low.single
button .frame.low.single.button -text "Frame "\
	-command "pickframe" -width 13
entry .frame.low.single.frame -textvariable dframe\
	-relief sunken -width 8

pack .frame.low.single.button -side left -padx 10
pack .frame.low.single.frame -side right -padx 10

frame .frame.low.update
button .frame.low.update.button -text "Frame Range"\
	-command "pickfrange" -width 13
entry .frame.low.update.bframe -textvariable bframe\
	-relief sunken -width 4
entry .frame.low.update.eframe -textvariable eframe\
	-relief sunken -width 4

pack .frame.low.update.button -side left -padx 10
pack .frame.low.update.eframe -side right
pack .frame.low.update.bframe -side right

#
#	Set the default - one image
#

set option latest
pack .frame.low.single -side top -fill x -expand yes

#
#	More options.
#

frame .frame.hi

frame .frame.hi.gray
menubutton .frame.hi.gray.menu	\
	-menu .frame.hi.gray.menu.choices\
	-text "Gray Wedge"\
	-relief raised -width 15

menu .frame.hi.gray.menu.choices -tearoff 0

set gray NO
.frame.hi.gray.menu.choices add radio	\
	-variable gray		\
	-value YES		\
	-label YES

.frame.hi.gray.menu.choices add radio	\
	-variable gray		\
	-value NO		\
	-label NO

entry .frame.hi.gray.entry	\
	-textvariable gray	\
	-relief sunken		\
	-width	8

pack .frame.hi.gray.menu -side left  -padx 10
pack .frame.hi.gray.entry -side right -padx 10

pack .frame.hi.gray -side top -expand yes -fill both -pady 2

set emag 1
set lmag 1

scale .frame.hi.emag		\
	-from -32		\
	-to   32		\
	-orient	horizontal	\
	-label	"Element Magnification"\
	-length 250	\
	-variable emag

scale .frame.hi.lmag		\
	-from -32		\
	-to   32		\
	-orient	horizontal	\
	-label	"Line Magnification"\
	-variable lmag		\
	-length 250	\
	-command ".frame.hi.emag set "

pack .frame.hi.lmag -side top -pady 2 -expand ye
pack .frame.hi.emag -side top -pady 2 -expand ye

#
#	Text field
#

set textannot " "
frame .frame.hi.text -borderwidth 2 -relief groove
label .frame.hi.text.label -text "Annotation Line Text"
entry .frame.hi.text.entry -textvariable textannot \
	-relief sunken -width 40
pack .frame.hi.text.label -side top
pack .frame.hi.text.entry -side bottom
pack .frame.hi.text -side top -fill x -pady 2
bind .frame.hi.text.entry <KeyPress> \
	{monitorLength %W .frame.hi.text.entry 70 %A}

set refresh ""
frame .frame.hi.refresh -borderwidth 2 -relief groove
label .frame.hi.refresh.label -text "Refresh Commands"
entry .frame.hi.refresh.entry -textvariable refresh\
	-relief sunken -width 40
pack .frame.hi.refresh.label -side top
pack .frame.hi.refresh.entry -side bottom
pack .frame.hi.refresh -side top -fill x -pady 2

# Pack all the command widgets into the command frame
#
if {$level == 1} {
	pack append .frame \
		.frame.low		{top fill}
}
if {$level == 3} {
	pack append .frame  \
		.frame.low			{top fill} \
		.frame.hi			{top fill}
}

frame .bottom  -relief raised
button .bottom.ok -text OK -width 10 \
	-command {

#	set location keywords
		   set locate " "
		   if {[string compare $station " "] != 0 && \
				[string compare $station ""] != 0} {
			set station [string toupper $station]
			set locate "STATION=$station"
		   }
		   if {[string compare $latlon " "] != 0 && \
				[string compare $latlon ""] != 0} {
			set locate "LATLON=$latlon"
		   }
		   if {[string compare $location " "] != 0 && \
				[string compare $location ""] != 0} {
			if {[llength $location] == 1 } {
			   set location [string toupper $location]
			   set locate "STATION=$location"
			} else {
			   set locate "LATLON=$location"
			}
		   }

#	set band keyword
		   set bandkey " "
		   if {[string compare $band " "] != 0 && \
				[string compare $band ""] != 0} {
			set bandkey "BAND=$band"
		   }
#	text keyword
		   set textkey " "
		   if {[string compare $textannot " "] != 0 && \
				[string compare $textannot ""] != 0} {
			set textkey [format "TEXT='$textannot'"]
		   }
		   set refkey " "
		   if {[string compare $refresh " "] != 0 && \
				[string compare $refresh ""] != 0} {
			set refkey [format "REFRESH='$refresh'"]
		   }
		   if {[string compare $dframe "Current"] == 0} {
			set frame ""
		   } else {
			set frame $dframe
		   }

#	make sure mandatory fields were set
		   if {![checkmandfield $image "Dataset"]} {
		      if {[string compare $option "dataset"] == 0} {
			 RunCommand "IMGDISP $image ALL=$bframe $eframe \
				$locate MAG=$lmag $emag  GRAY=$gray $textkey \
				$refkey $bandkey"
		      } elseif {[string compare $option "latest"] == 0} {
			 RunCommand "IMGDISP $image $frame \
				$locate MAG=$lmag $emag GRAY=$gray $textkey \
				$refkey $bandkey"
		      } elseif {[string compare $option "single"] == 0} {
			 RunCommand "IMGDISP $image $frame \
				$locate MAG=$lmag $emag GRAY= $gray $textkey \
				$refkey $bandkey"
		      } elseif {[string compare $option "update"] == 0} {
			 RunCommand "IMGDISP $image SEQ=$bframe $eframe \
				$locate MAG=$lmag $emag GRAY= $gray $textkey \
				$refkey $bandkey"
		      }
		   }
		}
button .bottom.cancel -text Dismiss -command {destroy .} -width 10

pack .bottom.ok	-side left -expand true -padx 20
pack .bottom.cancel -side right -expand true -padx 20

initStatus .bottom.status
if [winfo exists .bottom.status] {
	pack .bottom.status -side left -expand true -padx 20
}

#
# Pack the message, the command and the OK buttons into the base widget
#

pack .top -fill both -side top
pack .msg -fill both -side top
pack .frame -fill both -side top
pack .bottom -fill both -side bottom -expand yes
wm minsize . 10 10

# when bound to <KeyPress>, only allows $len characters in entry widget $w
#
proc monitorLength {w mw len key} {
  # if we don't want to monitor this widget...
  #
  if {[string compare $w $mw] != 0} {
    return
  }

  # if this is a modifier key...
  #
  if {[string length $key] == 0} {
    return
  }

  # if this is a non-printable key...
  #
  if [regexp ^\[\001-\031\177-\377\]\$ $key junk] {
    return
  }

  # if there's room in the widget...
  #
  if {[$w index end] < $len} {
    return
  }

  bell
  return -code break {}
}

proc mkHelp {} {
	global level messfont

	toplevel .help
	wm title .help "Help - Display Image "
	wm minsize .help 10 10
	placehelp .help
	message .help.title -width 300 -font $messfont\
		-text "Display Image"

	frame .help.frame -borderwidth 2 -relief groove
	text .help.frame.low -relief flat -height 40 -width 64\
		-yscroll {.help.frame.scroll set}

	scrollbar .help.frame.scroll -relief sunken -command\
		".help.frame.low yview"

	.help.frame.low insert 0.0 {
  Load Dataset		  display all the images
			  from a dataset in a
			  range of frames

  Load Latest Image	  display the most recent
			  image from a dataset in
			  the current frame

  Load One Image	  display a selected image
			  from a dataset in the
			  current frame

  Update Loop		  update the Frame Range
			  by displaying a selected
			  image in the frame following
			  the frame with the most
			  recent image

  Dataset		  ADDE dataset with the
			  images to display

  Center Location	  station or latitude/
			  longitude coordinates to
			  position at the center of
			  the displayed image

  Band			  image band number to
			  display

  Frame			  frame number to display
			  the image; active only
			  when Load Latest Image or
			  Load One Image is selected

  Frame Range		  range of frames for
			  displaying the dataset
			  or updating the loop;
			  active only when Load
			  Dataset or Update Loop
			  is selected
}
	if {$level == 3} {
	.help.frame.low configure -height 85 -width 64

	.help.frame.low insert end {
  Gray Wedge		  toggles on and off the
			  grayscale bar displayed
			  at the bottom of the image

  Line Magnification      line magnification of the
			  data; positive integers
			  blow up the data; negative
			  integers blow down the data

  Element Magnification   element magnification of
			  the data; positive integers
			  blow up the data; negative
			  integers blow down the data

  Annotation Line Text	  text for labeling the image

  Refresh Commands	  runs specified McIDAS commands
			  after the image displays

  Additional options are available using the IMGDISP
  command from the McIDAS Text and Command Window.
  See the McIDAS-X User's Guide for more information.

}
}
	pack .help.frame.scroll -side right -fill both -expand yes
	pack .help.frame.low -side left -fill both
	button .help.ok -text OK -width 10 -relief raised \
		-borderwidth 2 -command {destroy .help}
		pack append .help \
			.help.title {top} \
			.help.ok  {bottom } \
			.help.frame {top}

}