;---------------------------------------------------------------------------
; Document name: move_image.pro
; Created by:    Andre Csillaghy, June 1991
;
; Last Modified: Thu Dec 10 11:07:37 1998 (csillag@soleil)
;---------------------------------------------------------------------------
;

PRO Move_Image,image, $
	xAxis, yAxis, xMin,xMax,yMin,yMax, $
	TITLE = title, SIZES_ON = sizes_on, $
	WINCHANGE = winChange

;+
; PROJECT:
;       Phoenix 2 solar radio spectrometer
;
; NAME:
;	Move_Image
;
; PURPOSE:
;	Switches the part of an image displayed with its
;       neighbour either on the right, left,
;	up,down, up/right, up/left, down/right or down/left,
;	using a fixed scale factor (i.e. a give number of pixels to
;	display).
;
; CALLING SEQUENCE
;	Move_Image, image, xAxis, yAxis 
;		,xMin, xMax, yMin, yMax
;
; CATEGORY:
;	User Interface.
;
; INPUT ARGUMENTS:
;	image: the image in which one wants to move, a 2D 
;		real array;
;	xAxis, yAxis: the axes of the image;
;	xMin, xMax, yMin, yMax: the boundaries of the image
;		part actually displayed, in array indices. 
;
; OUTPUTS:
;	xMin, xMax, yMin, yMax: the boundaries of the new
;		part that will be displayed with Show_Image
;
; KEYWORDS:
;	TITLE: The title of the window
;	WINCHANGE: If set, the moves are displayed in a new
;		window.
;
; SIDE EFFECT:
;	The image moves are displayed.
;
; RESTRICTION
;	Works only with a display produced with "Show_Image".
; 	otherwise, the display will be adapted to the layout of
;	"Show_Image".
;
; MODIFICATION HISTORY:
;	Created 4/91, A.Csillaghy
;       Adapted for use in IDL 5.1 w/ xchoice,
;           csillag@ssl.berkeley.edu, November 1998 
;-

IF  N_Elements( image ) EQ 0 THEN BEGIN
  Error, 1, 'Move Image'
  RETURN
ENDIF
font = (get_dfont())(0)
IF font EQ '' THEN font = 'fixed'
 
nx = N_Elements( image( *, 0 ) )
ny = N_Elements( image( 0, * ) )

IF N_Elements( xAxis) EQ 0 THEN xAxis = IndGen( nx )
IF N_Elements( yAxis ) EQ 0 THEN yAxis = IndGen( ny )
IF N_Elements( xMax ) EQ 0 THEN xMax = nx-1
IF N_Elements( xMin ) EQ 0 THEN xMin = 0
IF N_Elements( yMax ) EQ 0 THEN yMax =ny-1
IF N_Elements( yMin ) EQ 0 THEN yMin = 0

IF NOT Keyword_Set( WINCHANGE ) THEN winChange = 0

menuLength = 9
menu = StrArr(menuLength)

menu(0)='Up'
menu(1)='Down'
menu(2)='Left'
menu(3)='Right'
menu(4)='Up/Left'
menu(5)='Up/Right'
menu(6)='Down/Left'
menu(7)='Down/Right'
menu(8)='Exit Move Image'

maxImageX = N_Elements(image(*,0))
maxImageY = N_Elements(image(0,*))
  
text = StrArr(2)

REPEAT BEGIN
  
    choice = ETHZ_XChoice('Select Move Direction', menu, $
                          TITLE='Move Image', TFONT=font, BFONT=font, $
                          XOFF=1, YOFF=10)+1
    
    up = (choice EQ 1) OR (choice EQ 5) OR (choice EQ 6)
    down = (choice EQ 2) OR (choice EQ 7) OR (choice EQ 8)  
    left = (choice EQ 3) OR (choice EQ 5) OR (choice EQ 7) 
    right = (choice EQ 4) OR (choice EQ 6) OR (choice EQ 8)

  nx=xMax-xMin + 1
  ny=yMax-yMin + 1

  IF up THEN BEGIN
    yMin = yMax + 1
    yMax = yMax + ny < maxImageY-1
    IF yMax EQ maxImageY-1 THEN BEGIN
      yMin = yMax-ny + 1> 0
      text(0) = 'This is the highest part of the image'
    ENDIF ELSE text(0) = ''
  ENDIF ELSE IF down THEN BEGIN
    yMax = yMin - 1
    yMin = yMin - ny > 0
    IF yMin EQ 0 THEN BEGIN
      yMax = ny - 1
      text(0) = 'This is the lowest part of the image'
    ENDIF ELSE text(0) = ''
  ENDIF
  IF right THEN BEGIN
    xMin = xMax + 1
    xMax = xMin + nx < maxImageX-1
    IF xMax EQ maxImageX-1 THEN BEGIN
      xMin = xMax-nx+1> 0
      text(1) = 'This is the right end of the image'
    ENDIF ELSE text(1) = ''
  ENDIF ELSE IF left THEN BEGIN
    xMax = xMin - 1
    xMin = xMin - nx > 0
    IF xMin EQ 0 THEN BEGIN
      xMax = nx - 1
      text(1) = 'This is the left end of the image'
    ENDIF ELSE text(1) = ''
  ENDIF

  Show_Image, image(xMin:xMax, yMin:yMax), $
      xAxis(xMin:xMax), yAxis(yMin:yMax), $
      WINNR=winChange*GetWNr(), $
      SIZES_ON=sizes_on
  Image_Info, image(xMin:xMax, yMin:yMax), $
      xAxis(xMin:xMax), yAxis(yMin:yMax), FILE=title
  
  IF TOTAL( text EQ '' ) NE 2  THEN BEGIN 
      Xmessage, text, TITLE='Move Image', XOFF=100, WAIT=3
  ENDIF

END UNTIL choice EQ menuLength

END
 
