;==============
; file uvspinquiz.pro  - includes UVSPINQ_EV, UVSPINQUIZ
;==============

pro uvspinq_ev, event

common inquiz, $
		cancelled,  	$		; 1=don't change, 0=change
		ipc, 		$		; doppler opt condition
		pdet,           $               ; non-dopp opt condition
		first_rep,      $		; first raster to display
		last_rep,       $		; last rast to display
		s_rep,          $		; slider ID for user's 1st ras
		e_rep,          $		; slider ID for user's last
		newdopp,        $		; user's dopp distribution
		doppinput,      $               ; ID for user's input
		newscal,        $               ; user's input for scale
		newpsize,	$       	; user's input for size
		newdivis                        ; user's input for tick marks


 widget_control, event.id, get_uvalue=input
 if n_elements(input) eq 0 then input=''

 type = strmid(tag_names(event, /structure_name),7, 4)

 case type of
    'BUTT'   : case input of

        'IMAGECAN' : widget_control, event.top, /destroy  ; kill top widget

        'IMAGE_GO'  : begin
		      widget_control, event.top, /destroy     ; kill top widget
		      cancelled = 0 			      ; make changes
	 	      end

	; detector selection for dopplers

        'First det'	  : if event.select ne 0 then ipc = '1'
	'Second det'	  : if event.select ne 0 then ipc = '2'
	'Sum of'    	  : if event.select ne 0 then ipc = '3'
	'All Det'      	  : if event.select ne 0 then ipc = '4'
	'Sum and dif/sum' : if event.select ne 0 then ipc = '5' 

	; detector selection for non-dopplers

        '1'   : if event.select ne 0 then pdet = input
	'2'   : if event.select ne 0 then pdet = input
	'3'   : if event.select ne 0 then pdet = input
	'4'   : if event.select ne 0 then pdet = input
	'5'   : if event.select ne 0 then pdet = input
	'all' : if event.select ne 0 then pdet = input

 
        'MYES' : if event.select ne 0 then myesno=1
        'MNO'  : if event.select ne 0 then myesno=0
        'BYES' : if event.select ne 0 then byesno=1
        'BNO'  : if event.select ne 0 then byesno=0
        'TYES' : if event.select ne 0 then tyesno=1
        'TNO'  : if event.select ne 0 then tyesno=0
 
        else : print,'Programmer error, UVSPINQUIZ.PRO. (code: 22)'

     endcase

  'SLID'   : begin
	   widget_control, s_rep, get_value=first_rep	; read slider value
	   widget_control, e_rep, get_value=last_rep    ; read slider value
     end

  'TEXT' : begin
   	widget_control, event.id, get_value=value, set_value=''

	case input of
	   'DOPPTXT' : begin
	   	on_ioerror, TRYAGAIN
          	  if value(0) ne '' then newdopp = double(value(0))
	       	on_ioerror, null
            end

	   'AVERTXT' : begin
	   	on_ioerror, TRYAGAIN
          	  if value(0) ne '' then newscal = long(value(0))
	       	on_ioerror, null
	    end

	   'PIXLTXT' : begin
	   	on_ioerror, TRYAGAIN
          	  if value(0) ne '' then newpsize = long(value(0))
	       	on_ioerror, null
	    end

	   'RECTXT' : begin
		on_ioerror, TRYAGAIN
         	   if strtrim(value(0),2) ne '' then newdivis = fix(value(0)) 
		on_ioerror, null
	    end

	    else : print,'Programmer error, UVSPINQUIZ.PRO (code: 33).'
	endcase
     end

    else : print,'Programmer error, UVSPINQUIZ.PRO. (code: 35)'
 endcase

TRYAGAIN:

return
end

;========================
;
; Returns 0 if widget's READY button was pushed else 
; Returns 1 if the CANCEL button was pushed
;


function uvspinquiz, struc=struc, hd=hd, type=type, row_det=row_det, $
		     newscalto=scalto, defpix=defpix, newpix=psize 

common inquiz, $
		cancelled,  	$		; 1=don't change, 0=change
		ipc, 		$		; doppler opt condition
		pdet,           $               ; non-dopp opt condition
		first_rep,      $		; first raster to display
		last_rep,       $		; last rast to display
		s_rep,          $		; slider ID for user's 1st ras
		e_rep,          $		; slider ID for user's last
		newdopp,        $		; user's dopp distribution
		doppinput,      $               ; ID for user's input
		newscal,        $               ; user's input for scale
		newpsize,	$       	; user's input for size
		newdivis                        ; user's input for tick marks

common recit, myesno, byesno, tyesno, t_divis	; borders and tick marks

cancelled = 1				; not cancelled yet

case type of 

 0: if (hd(60) gt 1) or (struc.st7.nrpt gt 1) then begin

      if struc.st4.twodet then ipc = struc.st1.tempipc  $
      else if hd(60) gt 1 then pdet = struc.st3.tempdet
   
      newdopp = struc.st4.doppto
   
      first_rep = struc.st1.tempfrst
      last_rep  = struc.st1.tempfrst+struc.st1.tempnfr-1

       ;
       ; Choosing detectors and repeats, if any.
       ;

	imgbase = widget_base(title='More Display Options', /frame, /column, $
				xpad=20, ypad=20, space=10)

     	   row1 = widget_base(imgbase, /row)

       	      if struc.st4.twodet then begin

          	 row1col1 = widget_base(row1, /column, /frame)

            	    displ = ['First det', 'Second det', 'Sum of', $
				 'All Det', 'Sum and dif/sum']
	    	    udispl = displ 
	    	    xmenu, displ, row1col1, /exclusive, /frame, $
				uvalue=udispl, button=dbase

		
		    col1_5 = widget_base(row1col1, /column, /frame)
              	    lab = widget_label(col1_5, value='Enter red/blue intensity')
		    lab = widget_label(col1_5, value='compression and RETURN.')
              	    lab = widget_label(col1_5, value='Nominal value is 1.0')
              	    lab = widget_label(col1_5, value='Default [' + $
					  strtrim(newdopp,2) + ']')
	      	    doppinput = widget_text(col1_5, /editable, uvalue='DOPPTXT')


       	      endif else if (hd(60) gt 1) then begin

          		row1col1 = widget_base(row1, /column)

            		howmany = struc.st7.ndet + 1 
	    		displ = strarr(howmany) 
 	    		udispl = displ & dbase=lonarr(howmany)
	    		for d = 0, struc.st7.ndet-1 do begin
				udispl(d) = strtrim(hd(137+d),2)
				displ(d)='Detector ' + udispl(d)
	    		endfor
	    		udispl(struc.st7.ndet) = 'all'
	    		displ(struc.st7.ndet)  = 'All detectors'

	    		xmenu, displ, row1col1, /exclusive, /frame, $
				 uvalue=udispl, button=dbase
             endif
	

             if struc.st7.nrpt gt 1 then begin

        	row1col2 = widget_base(row1, /column, /frame)
          	   lab = widget_label(row1col2, value='Select the ' + $
			     'raster repeats to display.')

          	   stick1  = widget_base(row1col2, /row)
	   		lab   = widget_label(stick1, value='Start raster : ')
           		s_rep = widget_slider(stick1, min=1, $
				max=struc.st7.nrpt, xsize=150,  $
				value=first_rep)
          	   stick2  = widget_base(row1col2, /row)
	   		lab   = widget_label(stick2, value='End raster   : ')
           		e_rep = widget_slider(stick2, min=1, $
				max=struc.st7.nrpt, xsize=150,  $
				value=last_rep)
            endif

	   canit = widget_button(imgbase, value='CANCEL', uvalue='IMAGECAN')
	   doit  = widget_button(imgbase, value='READY', uvalue='IMAGE_GO')

	   widget_control, imgbase, /realize  

	   if struc.st4.twodet then $
		widget_control, dbase(ipc-1), set_button=1 $
	   else if hd(60) gt 1 then $
		widget_control, dbase(pdet-1), set_button=1

	   xmanager, 'uvspinquiz', imgbase, event_handler='uvspinq_ev', $
				/modal, just_reg=0


	   if not(cancelled) then begin 

	      if struc.st4.twodet then begin
	      	   if n_elements(ipc) ne 0 then $
					struc.st1.tempipc = fix(ipc)
		   if n_elements(newdopp) ne 0 then $
					struc.st4.doppto = newdopp
	      endif else if hd(60) gt 1 and (n_elements(pdet) ne 0) then begin

        	   if pdet eq 'all' then begin  		; use all det
		    struc.st3.tempnd  = struc.st7.ndet 		; reset to all 
		    struc.st3.d1      = 0  			; start w/ first
		    struc.st3.d2      = struc.st7.ndet-1	; going to last
	            struc.st3.tempdet = struc.st7.ndet 
		   endif else begin				; single det 
		    detpos = where(hd(137:140) eq fix(pdet))
		    struc.st3.tempnd  = 1 			; one det
	            struc.st3.d1      = detpos(0)		; choosen det 
	 	    struc.st3.d2      = detpos(0)		
	            struc.st3.tempdet = detpos(0)
	          endelse
	      endif

   	      if n_elements(first_rep) ne 0 then begin
     		struc.st1.tempfrst = first_rep<last_rep 	; Start frame 
     		struc.st1.tempnfr  = (last_rep>first_rep) - $
					struc.st1.tempfrst + 1	; # rep
   	      endif                                        	

	   endif  			; end of not(cancelled)

 endif 

 1: begin

	newscal = scalto
	newpsize = defpix     		; unless the user changes this

	if n_elements(myesno) eq 0 then begin
	   myesno = 1  &  byesno=1  &  tyesno=1	; initialize draw options
	endif	


	mainopt = widget_base(title='More UVSP options', /column, space=20, $
					xpad=25, ypad=25)

	miniopt = widget_base(mainopt, /row)

         recbase = widget_base(miniopt, /frame, /column, space=10)
	  bordr = widget_base(recbase, /column, /frame)
	   lab = widget_label(bordr, value='Draw margin border')
	     q1 = widget_base(bordr, /exclusive, /column)
		myes = widget_button(q1, value='YES', uvalue='MYES')
		mno = widget_button(q1, value='NO', uvalue='MNO')
       
	  box = widget_base(recbase, /column, /frame)
	  lab = widget_label(box, value='Draw image border')
	     q2 = widget_base(box, /exclusive, /column)
		byes = widget_button(q2, value='YES', uvalue='BYES')
		bno = widget_button(q2, value='NO', uvalue='BNO')

	  tick = widget_base(recbase, /column, /frame)
	   lab = widget_label(tick, value='Draw tick marks')
	     q3 = widget_base(tick, /exclusive, /column)
		tyes = widget_button(q3, value='YES', uvalue='TYES')
		tno = widget_button(q3, value='NO', uvalue='TNO')
	     sep = widget_base(tick, /column, /frame)
	     lab2 = widget_label(sep, value='if yes, you may...')
	     lab3 = widget_label(sep, value='Enter tick separation in arcsec')
	     lab4 = widget_label(sep, Value='Negative value to reset')

	     if n_elements(t_divis) eq 0 then tickdif = 'size dependent' $
 	      else tickdif = strtrim(t_divis,2)
	     lab5 = widget_label(sep, value='Default is ' + tickdif)

	     tictxt = widget_text(sep, /editable, uvalue='RECTXT')

 	 moreopt = widget_base(miniopt, /column, /frame, space=30)

	    mdata = widget_base(moreopt, /frame, /column, xpad=20)
              la2 = widget_label(mdata, value='Current data maximum is ' + $
				  strtrim(scalto,2) )
              lab = widget_label(mdata, value='Enter value for scaling')
              lab = widget_label(mdata, value='and press RETURN.')
				
	      avr = widget_text(mdata, /editable, uvalue='AVERTXT')
              lab = widget_label(mdata, value='Valid for one display only.')

	    pixs = widget_base(moreopt, /frame, /column, xpad=20) 
              lab = widget_label(pixs, value='Nominal screen size of '+$
			'one image pixel : ' + strtrim(fix(psize),2) )
              lab = widget_label(pixs, value='Current screen size of '+$
			'one image pixel : [' + strtrim(fix(defpix),2)+']' )

              lab = widget_label(pixs, value='Enter value and press RETURN')
	      txt = widget_text(pixs, /editable, uvalue='PIXLTXT')


        canit = widget_button(mainopt, value='CANCEL', uvalue='IMAGECAN')
        doit  = widget_button(mainopt, value='READY', uvalue='IMAGE_GO')

	widget_control, mainopt, /realize
	widget_control, myes, set_button=myesno
	widget_control, byes, set_button=byesno
	widget_control, tyes, set_button=tyesno

        xmanager, 'uvspaver', mainopt, event_handler='uvspinq_ev', $
                        /modal, just_reg=0


 	if not(cancelled) then begin 

	   if n_elements(newscal) ne 0 then scalto = newscal
	   if n_elements(newdivis) ne 0 then t_divis = newdivis

	   if n_elements(psize) ne 0 then begin
	      psize = newpsize

	      ;
	      ; don't let user make image too big
	      ;

              device, get_screen_size = scrsz

	      if (psize*struc.st7.nx) gt scrsz(0) then $
	       			psize = scrsz(0)/struc.st7.nx 
	      if (psize*struc.st7.ny* row_det) gt scrsz(1) then $
			psize = scrsz(1) / (struc.st7.ny * struc.st3.tempnd) 

  	   endif
	endif

 end

 else: print,'Programmer error, UVSPINQUIZ.PRO. (code: 11)'

endcase
return, cancelled
end