
pro print_event, ev
common order1, diskexp, control, generic, comment
common printit, outwin, windoe, check

@uvspkeys.common
@uvspstruct.common

 widget_control, ev.id, get_uvalue=input	; which widget was clicked
 if n_elements(input) eq 0 then input = ''

 type = strmid(tag_names(ev, /structure_name),7, 4)    ; get widget type

 case type of 
    'BUTT' : case input of
       'CANCEL' : begin                         ; leave printing section
	  widget_control, ev.top, /destroy 		; kill top widget
	  widget_control, generic, sensitive=1          ; bring back needed
	  widget_control, control, sensitive=1          ;  control widgets
          end
       'CHICKEN' : begin                        ; user is not taking this chance
	  widget_control, ev.top, /destroy 		; kill top widget
	  widget_control, windoe, sensitive=1           ; user can try again
          end

       'CONTINUE' : begin                       ; user is printing anyhow
	  widget_control, ev.top, /destroy 		; kill top widget

	  ; go for printing
	  wset,outwin				      ; activate desired window
          widget_control, comment, set_value= $
		'Started reading window number ' + strtrim(outwin,2), /append

          snap = byte(tvrd(0,0,!d.x_size,!d.y_size))
          widget_control, comment, set_value= $
		'Finished reading window number ' + strtrim(outwin,2), /append

	  ; output orientation is determined by window size
          if !d.x_size le !d.y_size then begin         ; do set_plot inside if
		          set_plot,'ps',/copy         
       	                  device, /port, color=colo, xoffset=3.7,xsize=14,   $
	       	       	     yoffset=2.5,ysize=14,bits_per_pixel=8,/helvetica  
	  endif else begin
		          set_plot,'ps',/copy
			  device,/land,color=colo,xoffset=3,xsize=19,  $
			     yoffset=24,ysize=16,bits_per_pixel=8,/helvetica
          endelse

       	  tv,snap        &  snap = 0 		   ; clear array
	  device,/close  &  set_plot,'X',/copy     ; close and set to screen

          widget_control, comment, set_value= $
		'Output spawned to printer', /append

          psplot, filename='idl.ps', color=colo, /delete

          widget_control, windoe, sensitive=1


	  end

       'READY'  : begin    ; User is TVRD and printing selected window.
          		   ;	Will have black background just like screen.
      
          widget_control, windoe, sensitive=0

	  ; check for lack of user input
	  if n_elements(outwin) eq 0 then ok=0 $ 	  ; no input
	     else if strtrim(outwin,2) eq '' then ok=0 $  ; null input
             else ok=1

	  if not(ok) then begin 		; not ready to do this, yet.
               widget_control, comment, set_value='ERROR: ' + $
			  'No input was entered ', /append
               widget_control, windoe, sensitive=1
	       return
	  endif

          ; if not a number jump to error message and let them try again
	  alright=0  
	  on_ioerror, WRONG
          outwin = fix(outwin)  		; will be array index
	  alright=1 	

	  ; test outwin for valid/active window
;l	  if outwin ge n_elements(struc.st5.window) then ok=0 $ ; beyond program
;l	     else if struc.st5.window(outwin) ge 0 then ok=0 $  ; not active
;l	     else ok=1

	  device, window=winexists
	  if not(winexist(outwin)) then begin
		widget_control, comment, set_value='ERROR: No such window, ' + $
					 strtrim(outwin,2) ,/append
		return
	  endif

;l	  if not(ok) then begin       ; requires user to make a command decision
;l	     widget_control, windoe, sensitive=0
;l	     check = widget_base(title='VERIFY', /column, xpad=20, ypad=20, $
;l				space=20)
;l	      lab = widget_label(check, value='This program has no record of '+$
;l				'window index ' + strtrim(outwin,2) + '.' )
;l	      lab2= widget_label(check, value='THIS PROGRAM WILL CRASH if ' + $
;l				'this window DOES NOT EXIST.')
;l	      lab3= widget_label(check, value='Do you still want to print '+$
;l				'window index ' + strtrim(outwin,2) )
;l
;l              check2 = widget_base(check, /row, xpad=50, space=30)
;l	       yes = widget_button(check2, value='Yes', uvalue='CONTINUE')
;l	       no  = widget_button(check2, value='NO', uvalue='CHICKEN')
;l
;l	     widget_control, check, /realize
;l	     xmanager,'uvspprint', check, event_handler='print_event'
;l             return                  ; user will have to respond to widget
;l          endif
 
	  ; go for TVRD and printing if program made it to here
	  wset,outwin				      ; activate desired window
          widget_control, comment, set_value= $
		'Started reading window number ' + strtrim(outwin,2), /append

          snap = byte(tvrd(0,0,!d.x_size,!d.y_size))
          widget_control, comment, set_value= $
		'Finished reading window number ' + strtrim(outwin,2), /append

	  ; output orientation is determined by window size
          if !d.x_size le !d.y_size then begin        ; do set_plot inside if
		          set_plot,'ps',/copy
       	                  device,/port,color=colo,xoffset=3.7,xsize=14,   $
	       	       	     yoffset=2.5,ysize=14,bits_per_pixel=8,/helvetica  
	  endif else begin
		          set_plot,'ps',/copy
			  device,/land,color=colo,xoffset=3,xsize=19,  $
			     yoffset=24,ysize=16,bits_per_pixel=8,/helvetica
          endelse

       	  tv,snap        &  snap = 0 		     ; clear array 
	  device,/close  &  set_plot,'X',/copy	     ; close and set to screen

          widget_control, comment, set_value= $
		'Output spawned to printer', /append

          psplot, filename='idl.ps', color=colo, /delete

WRONG:    if not(alright) then widget_control, comment, set_value= $
		'ERROR: Invalid window index ' + outwin, /append
          widget_control, windoe, sensitive=1

         
       end

       else : widget_control, comment, set_value='ERROR-UVSPPRINT: ' + $
			'Invalid BUTTON value in UVSPPRINT' + input, /append
       endcase

    'TEXT' : begin
	widget_control, ev.id, get_value=value, set_value=''
        outwin = value(0)
	end
	
    else   : widget_control, comment, set_value='ERROR-UVSPPRINT: ' + $
			'Invalid widget type in UVSPPRINT' + input, /append

                                    
 endcase

end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


pro uvspprint, curany=curany

@uvspkeys.common
common printit, outwin, windoe, check
common order1, diskexp, control, generic, comment
common curser, topper, bottom, left, right, horz, hsiz, vsiz, m_m
common uvspsav, savexp, exptyp, saviwin, banner, savwinfile, $
		 widstatus, save_index
common locals, widgt,  expran, yaxbase, detcount, binbase, lastmexe

@uvspstruct.common

widget_control, control, sensitive=0      ; until finished keep the users
widget_control, generic, sensitive=0	  ;   hand off the dials

case curany of   	; current data (0) or any window (1)
   0 :  begin	; doing the current display again, this time to the printer

	widget_control, comment, set_value= $
		'Will print currently active window.', /append


        if !d.window eq struc.st5.imagewin then begin
		; Current window is an Image window, so all keywords are 
		; 	set, just make processing call

		!p.background = 249b      ; white background
		!p.font=0		  ; postscript fonts
	      	set_plot,'ps',/copy
       	        device,/portrait,color=colo,xoffset=3.7,xsize=14, $
	       	       yoffset=3.5,ysize=14,bits_per_pixel=8,/helvetica

	        uvspprocess, struc=struc, data=data, hd=hd, ave_sep=ave_sep, $
			scal=scal, p_scal=p_scal, contur=contur, $
			silent=silent, solgrid=solgrid, point=point, $
			rollo=rollo, $
			smuth=smuth, templev=templev, cycle=cycle, $
			exptyp=exptyp, timfil=timfil, hard=1, $
			abscissa=abscissa, plotover=plotover, one_det=one_det, $
			c_det=c_det, mytick=mytick, maxcyc=maxcyc, $
			ylohi=ylohi, xlohi=xlohi, nsome=nsome, mypsym=mypsym

	        ; some information to go along with image
		uvsphard, struc=struc, point=point, hd=hd, maxdata=max(data)

            endif else if !d.window eq struc.st5.headwin then begin
                ; Hard copy the current information page

                set_plot,'ps'
		!p.font=0		  ; postscript fonts
                device,/portrait,xsize=17.5,ysize=24,xoffset=2,yoffset=2, $
		       /helvetica

     		; Making hard copy, but must use current page
     		if cycle ne 1 then cycle = cycle - 1 else cycle = maxcyc

  		; Call to do information page
       		uvspform, struc=struc, data=data,    maxcyc=maxcyc, $
			   cycle=cycle, point=point, ih=hd ,lastmexe=lastmexe

	    endif else if !d.window eq struc.st5.plotwin then begin
	 	; Current window is an plot window
		; Could be plot experiment, or max/mean plot of raster
			
  		set_plot,'ps'  
		device,/landscape,/helvetica

		if exptyp eq 0 then $		; normal plot processing
	           uvspprocess, struc=struc, data=data, hd=hd, ave_sep=ave_sep, $
			scal=scal, p_scal=p_scal, contur=contur, $
			silent=silent, solgrid=solgrid, point=point, $
			rollo=rollo, $
			smuth=smuth, templev=templev, cycle=cycle, $
			exptyp=exptyp, timfil=timfil, hard=1, $
			abscissa=abscissa, plotover=plotover, one_det=one_det, $
			c_det=c_det, mytick=mytick, maxcyc=maxcyc, $
			ylohi=ylohi, xlohi=xlohi, nsome=nsome, mypsym=mypsym $
	    	else $				; plot raster max or mean
		  if struc.st1.tempnfr gt 1 then begin
		    hard=1
	   	    uvspm_m, struc=struc, data=data, abscissa=abscissa, $
					  hd=hd, m_m=m_m  
	 	    hard=0
		  endif else widget_control, comment, set_value= $
		     		'ERROR: Can not plot max/mean ' + $
				'single repeat.', /append


	    endif else if !d.window eq struc.st5.areawin then begin
                ; plot raster area, already have coordinates
                if struc.st1.tempnfr gt 1 then begin         ; Multiple repeats
		   if hsiz ge vsiz then begin      ; wide output
  		      set_plot,'ps'  
		      device, /landscape, /helvetica
		   endif else begin			     ; tall output
  		      set_plot,'ps'  
		      device, /portrait, /helvetica, xoffset=3.0, xsize=15,   $
	       	       	      yoffset=2.5, ysize=23
                   endelse

                   uvsparea, struc=struc, data=data, hd=hd, ave_sep=ave_sep, $
		       timfil=timfil, rollo=rollo, hard=1, abscissa=abscissa, $
		       topper=topper, bottom=bottom, left=left, right=right, $
		       hsiz=hsiz, vsiz=vsiz, mypsym=mypsym, mytick=mytick, $
			p_scal=p_scal, ylohi=ylohi, xlohi=xlohi 
   

		endif else widget_control, comment, set_value= $
		      'ERROR: Can not plot pixel value of ' + $
					  'single repeat.', /append

	    endif else if !d.window eq struc.st5.slicewin then begin
                ; plot raster slice, already have coordinates
	        if left ne right or bottom ne topper then begin  ; Not one point
  		   set_plot,'ps'  
		   device, /landscape, /helvetica
                   uvspslice, struc=struc, data=data, hd=hd, ave_sep=ave_sep, $
		       rollo=rollo, hard=1, horz=horz,              $
		       topper=topper, bottom=bottom, left=left, right=right, $
		       mypsym=mypsym, mytick=mytick, p_scal=p_scal, $
			ylohi=ylohi 
 
    
		endif else widget_control, comment, set_value= $
		      'ERROR: Can not plot slice of ' + $
				'single point.', /append


            endif else widget_control, comment, set_value= $
		'ERROR: No printable window currently ' + $
				'in operation.', /append


	  
        device,/close  &  set_plot,'X',/copy     ; close and set to screen
	!p.font=-1     &  !p.background = 0b     ; back to a black background


        widget_control, comment, set_value= $
		'Output spawned to printer', /append


        psplot, filename='idl.ps', color=colo, /delete

	widget_control, generic, sensitive=1          ; bring back needed
	widget_control, control, sensitive=1          ;  control widgets

	end
   1 :  begin	; Ask user for which window to read for printing
	   widget_control, control, sensitive=0
	   windoe = widget_base(title='Choose window to output', /column, $
				xpad=20, ypad=20, space=20)

            win1 = widget_label(windoe, value='Enter window index number ' + $
					      ' to be output')

       	    win2 = widget_label(windoe, value='Delete key deletes ' + $
				   	      'characters following cursor.')
            win3 = widget_text(windoe, /editable, xsize=30)

            win4 = widget_button(windoe, value='READY', uvalue='READY')
            win5 = widget_button(windoe, value='CANCEL', uvalue='CANCEL')
	
	   widget_control, windoe, /realize
	   xmanager,'uvspprint', windoe, event_handler='print_event' 

	end
endcase

end
