;+
; NAME        : EIT_SUB_UTIL_BOX
;
; PURPOSE     :
;
; CATEGORY    :
;
; EXPLANATION : 
;
; SYNTAX      : 
;	 	result = eit_sub_util_box(/init,size=size)
;	 	result = eit_sub_util_box(x,y)
;	 	result = eit_sub_util_box(/get_coord)
;
; EXAMPLES    : none
; 
; CALLED BY   :
;
; CALLS TO    : none
;
; ARGURMENTS  :	none
;
; KEYWORDS    :
; 	SIZE 	The size (squared) the area boxing will be incremented
;
; OUTPUTS     : none
;
; COMMON      : none
;
; RESTRICTIONS: none
;
; SIDE EFFECTS: none
;
; PROJECT     : SOHO - EIT
;
; HISTORY     : V1,  Elaine Einfalt (HSTX)
;
; CONTACT     : eit@xanado.nascom.nasa.gov
;-

function EIT_SUB_UTIL_BOX,  $
			new_x, 				$
			new_y, 				$
			init		= init,		$ 
			size		= size, 	$
			get_coord	= get_coord, 	$
			draw_index	= draw_index, 	$
			pix_index	= pix_index, 	$
			resize_x	= resize_x


common EIT_SUB_UTIL_BOX, orig_x, orig_y, prev_x, prev_y, square, coord

 ;
 ; Start boxing with a mouse down event.
 ;

 if keyword_set(init) then begin

    square = fix(size)

    ; Remember initial top left corner at time of mouse down

    orig_x = fix(new_x/square) * square
    orig_y = !d.y_size - ( ((!d.y_size/square-1) - fix(new_y/square)) * square)

    prev_x = orig_x
    prev_y = orig_y
	
    return,1 					; wait for more calls

 endif

 ;--------------------------------------------------------------------------

 ;
 ; Just returns the cursor box coordinate values.
 ;

 if keyword_set(get_coord) ne 0 then return, coord


 ;--------------------------------------------------------------------------

 ; 
 ; Draw the cursor box from the initial mouse down coord, to the 
 ; current cursor coordinate.
 ;

 col = !d.n_colors -1

 ; Lower right corner of this square

 new_x = (fix(new_x/square) * square) + square
 new_y = (!d.y_size - $
	   ( ((!d.y_size/square-1) - fix(new_y/square)) * square) ) - square

 ; Upper left corner of this square, if box has been inverted.

 if new_x - square lt orig_x then new_x = new_x - square
 if new_y + square gt orig_y then new_y = new_y + square

 px = [orig_x, new_x,  new_x, orig_x, orig_x]
 py = [orig_y, orig_y, new_y, new_y,  orig_y]


 ; Rewrite the saved pixmap to the viewable window.  
 ; This is the image, grid and all the previously highlighted blocks, 
 ; NOT including anything in the current cursor box, which isn't 
 ; definite until the user releases the mouse button.
 ; 

 wset, draw_index
 device, copy=[0,0, resize_x,resize_x, 0,0, pix_index]

 ; Now, on top of that just displayed window, draw the ever changing
 ; cursor box.

 plots, px, py, col=col, /dev, lines=0  		; Draw new box

 prev_x = fix(new_x)   &   prev_y = fix(new_y)		; Spock says "Remember"

 coord = {get_coord, $
		     up_left_x   : fix(min([orig_x, new_x])),  $
		     up_left_y 	 : fix(max([orig_y, new_y])), $
		     low_right_x : fix(max([orig_x, new_x])), $
		     low_right_y : fix(min([orig_y, new_y])) }


return,1
end
