;+
; NAME        : EIT_SUB_DRAW
;
; PURPOSE     :
;
; CATEGORY    :
;
; EXPLANATION : 
;
; SYNTAX      : 
;
; EXAMPLES    : none
; 
; CALLED BY   :
;
; CALLS TO    : none
;
; ARGUMENTS   :	none
;
; KEYWORDS    : none
;
; OUTPUTS     : none
;
; COMMON      : none
;
; RESTRICTIONS: none
;
; SIDE EFFECTS: none
;
; PROJECT     : SOHO - EIT
;
; HISTORY     : V1,  Elaine Einfalt (HSTX)
; 		2003/07/21 _ Einfalt - Spacecraft antenna problem result
;				 in spending part of the year rolled 180
;				 Images are still displayed as taken, 
;				 but Arcsec, Helio, and R/theta outputs 
;				 need to be adjusted.
;
; CONTACT     : eit@xanado.nascom.nasa.gov
;-

pro EIT_SUB_DRAW, sub_ids=sub_ids, substruc=substruc, event=event

common local, process, last_block, $	; just needed for the eventhandler
	      orig_x,  orig_y,     $
	      prev_x,  prev_y

 event_x = 0L > event.x < long(substruc.resize_x)  ; force position value to be
 event_y = 0L > event.y < long(substruc.resize_x)  ; within the draw window

 if EVENT.PRESS then begin	 ; mouse down event occurred.

    ; Save what the window looks like NOW, before user starts cursor boxing it.

    wset, substruc.pix_index
    device, copy=[0,0, substruc.resize_x, substruc.resize_x, $
		  0,0, substruc.draw_index]
    wset, substruc.draw_index

    process    =  1		; Begin window processing
    last_block = -1		; Reset for this block

    ; When the user is doing cursor boxing (highlighting mode, not pattern 
    ; dragging mode), then just remember where the mouse down click occurred.

    if substruc.draw_flag eq 1 then $			; start boxing an area
	ok = EIT_SUB_UTIL_BOX(/init, event_x, event_y, size=substruc.pix_per)

 endif


 if EVENT.RELEASE then begin	; Mouse button up occurred

  process    =  0		; Stop window processing
  last_block = -1		; Reset for next block	

  if substruc.draw_flag ne 3 then begin		; not in rotate mode

    ; When the user is doing cursor boxing (highlighting mode, not pattern 
    ; dragging mode), then the new "to be" highlighted area has been
    ; finalized by the mouse release, so highlight the new area.

    ; When in highlighting mode, this indicates stop boxing area.
    ; Take existing selected pattern (sub_ids) and the just added blocks,
    ; process/merge them together.

    if substruc.draw_flag eq 1 then $ 			; stop boxing an area
       EIT_SUB_UTIL_AREA, sub_ids=sub_ids, substruc=substruc 

    ; Save this window, now that either the new blocks are highlighted 
    ; or the same blocks have been dragged, to the pixmap window in memory.

    wset, substruc.pix_index
    device, copy=[0,0, substruc.resize_x,substruc.resize_x,0,0, $
		   substruc.draw_index]
    wset, substruc.draw_index

  endif else begin 				; in rotate mode

	; Do the rotation of the point selected by the number
        ; of hours already showing in the rotation window.

	widget_control, hourglass=1

	widget_control, substruc.rotate_id, get_value=rot_hours
	EIT_SUB_ROTATE, rot_hours=rot_hours(0), substruc=substruc, $
			event_x=event_x, event_y=event_y

	widget_control, hourglass=0
        return
  endelse

 endif

 ; 
 ; For all movement inside the draw window, the arcsec, heliographic 
 ; and R/theta coordinates under the cursor are rewritten to the display.
 ;

 data_coord = convert_coord(event_x,event_y, /device, /to_data)

 ; If we are rolled 180 then the cursor inputs are inverse 2003/07/21 - einfalt

 if substruc.antenna_roll eq 180. then data_coord = -(data_coord)

 ; In arcsecs

 widget_control, substruc.sol_curs_pos_id, set_value= $
		 strtrim(round(data_coord(0)),2) + ',  ' + $
		 strtrim(round(data_coord(1)),2) 


 ; In heliographic

 image_date = anytim2utc(substruc.date, /ecs, /date)

 lat_lon = round(ARCMIN2HEL([data_coord(0)/60.], [data_coord(1)/60.], $
				date=image_date, off_limb=off_limb)) ;, /soho))

 if not(off_limb) then begin
    if lat_lon(0) ge 0 then lat = strtrim(abs(lat_lon(0)),2) + ' N,  ' $
		       else lat = strtrim(abs(lat_lon(0)),2) + ' S,  ' 

    if lat_lon(1) ge 0 then lon = strtrim(abs(lat_lon(1)),2) + ' W' $
                       else lon = strtrim(abs(lat_lon(1)),2) + ' E' 

 endif else begin
    lat = 'off'
    lon = 'limb'
 endelse

 widget_control, substruc.hel_curs_pos_id, set_value= lat + ' ' + lon


 
 ; In R and Theta

 theta_r = cv_coord(/degrees, from_rect=[data_coord(0),data_coord(1)], $
		    /to_polar)

 if theta_r(0) lt 0 then theta = 180 + theta_r(0) + 90 $	
 else if theta_r(0) ge 90 then theta = theta_r(0) - 90 $
 else theta = 270 + theta_r(0)

 widget_control, substruc.thet_curs_pos_id, $
		 set_value= string(theta_r(1)/960.,'(f3.1)') + ' R, ' + $
			    strtrim(fix(theta),2) + ' deg'

 ;
 ; Block process ONLY when the cursor has moves into a new subfield.
 ; The subfield block number is rewritten ONLY when it has changed.
 ;
 ; BLOCK_NUM is the EIT subfield grid number (0-1023),
 ; There are 32 blocks across an image, the image size is variable
 ; depending on window size, but the PIX_PER is the number of device 
 ; pixels contained in one EIT subfield block.
 ;    

 block_num = fix(event_x/long(substruc.pix_per))  + $
		(32 * (31 - fix(event_y/long(substruc.pix_per))))


 if (last_block ne block_num) then begin

    last_block = block_num			; remember this new block
    widget_control, substruc.blk_curs_pos_id, 		$
 		    set_value=strtrim(block_num,2)

    ; 1996/3/22
    ; Pixel p1row,p1col,p2row,p2col of this block for port B

    widget_control, substruc.pix_curs_pos_id, 		$
 		    set_value=eit_sub_blkpix(block_num=block_num, /text)

    ;
    ; When the mouse button is currently pressed down (PROCESS will equal 1), 
    ; then:
    ;        If highlight mode : User is boxing/highlighting subfields.
    ;	     If drag mode      : User is moving/dragging the subfield pattern.
    ;   

    if (PROCESS) then $		

       if substruc.draw_flag eq 1 $	

	  then ok = EIT_SUB_UTIL_BOX(  event_x, 			 $
				       event_y, 			 $
				       resize_x   = substruc.resize_x, 	 $	
			    	       draw_index = substruc.draw_index, $
		    		       pix_index  = substruc.pix_index)  $

	  else if  substruc.draw_flag eq 2 then $

		EIT_SUB_DISP,	offset	 = block_num,			 $
				refile 	 = (substruc.grid_flag ne 1),	 $
				sub_ids  = sub_ids,			 $
				substruc = substruc
 endif

return
end

