PRO sswhere_event, evnt

;event handler for sswhere
; the uvalues contain the menu name
; the values contain the button name
; the wdgtlbs common block contains the menu lists
; the wdgtind common block holds gt_ function indices.

;  rdb, 30-Jan-97  Added ELSE: in WIDGET_BUTTON case statement

COMMON initflg,iflag,maxsat
COMMON wdgtid,whole,complete,satur,butts,b1,b2,b3,b4,b5,b6,dpes,all,txtwgt
COMMON msks,totmsk,fovmsk,obsmsk,zero,one
COMMON wdgtlbs,mode_labs,dpmode_labs,res_labs,comp_labs,filta_labs,filtb_labs,dpe_labs
COMMON wdgtind,unq_modes,unq_dpmodes,unq_res,unq_comp,unq_filta,unq_filtb,unq_dpes
COMMON wdgtvecs,mode_vec,dpmode_vec,res_vec,comp_vec,filta_vec,filtb_vec,dpe_vec,cmplt,satrtn
COMMON pssbck,indmp,ssw
COMMON lgc,logic,dlogic

;If this is the first time through then initialize things

if (iflag eq 0) then begin

   iflag = 1

   if (n_elements(unq_filta) eq 1) or (n_elements(unq_filtb) eq 1) then begin

     dlogic = 'AND'               ;default logic is "AND"

   endif else begin
 
     dlogic = 'OR'                ;default logic is "OR"

   endelse

   logic=dlogic			  ;set initially

  ;Selection vectors to keep track of what selections have been made.

  cmplt=100                     ;default initialization
  satrtn=maxsat

  mdsz=size(unq_modes)
  mode_vec=replicate(0,mdsz(1))

  dmsz=size(unq_dpmodes)
  dpmode_vec=replicate(0,dmsz(1))

  rssz=size(unq_res)
  res_vec=replicate(0,rssz(1))

  cpsz=size(unq_comp)
  comp_vec=replicate(0,cpsz(1))

  fasz=size(unq_filta)
  filta_vec=replicate(0,fasz(1))

  fbsz=size(unq_filtb)
  filtb_vec=replicate(0,fbsz(1))

  dpsz=size(unq_dpes)
  dpe_vec=replicate(0,dpsz(1))

  ;initalize the overall masks

  rdsz=size(indmp)
  zero=replicate(0,rdsz(1))
  one=replicate(1,rdsz(1))

  totmsk=zero
  fovmsk=one
  obsmsk=one

endif

type= TAG_NAMES(evnt, /structure)

  WIDGET_CONTROL, evnt.id, GET_VALUE=value     ;get widget value
  WIDGET_CONTROL, evnt.id, GET_UVALUE=uvalue    ;get widget user value

;Handle sliders            Make these case statments in the future.

IF (type EQ 'WIDGET_SLIDER') THEN BEGIN

   CASE uvalue OF

   'completeness':  cmplt = evnt.value

   'saturation':   satrtn = evnt.value

  ENDCASE  ;uvalue

ENDIF

;Handle buttons

IF (type EQ 'WIDGET_BUTTON') THEN BEGIN

  CASE uvalue OF

    'Modes': mode_vec(where(mode_labs eq value)) = evnt.select 

    'DP Modes': dpmode_vec(where(dpmode_labs eq value)) = evnt.select 

    'Resolution': res_vec(where(res_labs eq value)) =  evnt.select

    'Compression': comp_vec(where(comp_labs eq value)) =  evnt.select

    'A Filters': filta_vec(where(filta_labs eq value)) = evnt.select

    'Logics': 	if evnt.select then logic = value

    'B Filters':  filtb_vec(where(filtb_labs eq value))  = evnt.select

    'all DPE':  BEGIN

       dpe_vec(*) = evnt.select

       WIDGET_CONTROL,dpes,SET_BUTTON=evnt.select
 
     end    ;all dpe

    'DPE Values':  BEGIN

       dpe_vec(where(dpe_labs eq value)) = evnt.select  

       if n_elements(unq_dpes) ne 1 then WIDGET_CONTROL,all,SET_BUTTON=0
     
     end   ;dpe values

    'Field of View': BEGIN

       pfi_chk=where((gt_pfi_ffi(indmp) eq 0) or (gt_pfi_ffi(indmp) eq 2))
       ffi_chk=where((gt_pfi_ffi(indmp) eq 1) or (gt_pfi_ffi(indmp) eq 3))

       if pfi_chk(0) ne -1 and ffi_chk(0) ne -1 then $
	 print,'Caution: This is a mixed data set containing both FFIs and PFIs'

       fovnum=new_win(512,512)
       plot_fov,indmp,fovmsk
       wait,1
       wdelete,fovnum

    end   ;fov

    'CL FOV': fovmsk=one

    'Time, Sequence': BEGIN

       obsnum=new_win(512,512)
       show_obs3,indmp,obsmsk		;SHOW_OBS3 seems to work for either
					;index or roadmap? Should change to SHOW_OBS4
					;but this dosen't seem to work for either?
       wait,1
       wdelete,obsnum

    end   ;fov

    'CL OBS': obsmsk=one

    ;Allow user to check how many images will be selected

    'Test': BEGIN

    message=['.....ZZZZZZzzzzzzz.']
    WIDGET_CONTROL,txtwgt,set_value= message

    msk=mk_mask(indmp,logic)             ; create the ss mask

    tst=where((msk and fovmsk and obsmsk) eq 1 )     ; how many entries are there

    if (tst(0) eq -1) then begin

       message=['No Data Match Selected','Parameters.']
       WIDGET_CONTROL,txtwgt,set_value= message

       return

    endif

    dumm=size(tst)
    frmcnt=dumm(1)

    message=[strcompress(string(frmcnt))+' Frames Match Selected','Parameters.']
    WIDGET_CONTROL,txtwgt,set_value= message

    end  ;test

    ;Selection handler, calculate the ss vector

    'Select': BEGIN

    msk=mk_mask(indmp,logic)              		; create the full mask

    totmsk = totmsk OR (msk and fovmsk and obsmsk)     	;add selection to the total mask

    ;check total number of images selected

    tst=where(totmsk eq 1)         

    if (tst(0) eq -1) then begin        

       message=['No Images Selected So Far.']
       WIDGET_CONTROL,txtwgt,set_value= message

       return

    endif

    dumm=size(tst)
    frmcnt=dumm(1) 

    message=['A Total of'+strcompress(string(frmcnt))+' Frames Selected.', $
             'Add Another Selection, or Exit.']
    WIDGET_CONTROL,txtwgt,set_value= message

    end   ;select

;Clear handler

    'Clear': BEGIN

      totmsk = zero
      fovmsk = one
      obsmsk = one
      iflag=0

      message=['SS variable cleared']

      WIDGET_CONTROL,txtwgt,set_value= message
      WIDGET_CONTROL,whole,SET_BUTTON=0
      WIDGET_CONTROL,complete,set_value=100
      if maxsat ne 0 then WIDGET_CONTROL,satur,set_value=maxsat
      if dlogic eq 'AND' then WIDGET_CONTROL,butts(0),set_button=1 $
			 else WIDGET_CONTROL,butts(1),set_button=1 
      if n_elements(unq_dpes) eq 1 then WIDGET_CONTROL,dpes,set_button=1

      logic=dlogic

      if n_elements(unq_modes) eq 1 then WIDGET_CONTROL,b1(0),set_button=1
      if n_elements(unq_dpmodes) eq 1 then WIDGET_CONTROL,b2(0),set_button=1
      if n_elements(unq_res) eq 1 then WIDGET_CONTROL,b3(0),set_button=1
      if n_elements(unq_comp) eq 1 then WIDGET_CONTROL,b4(0),set_button=1
      if n_elements(unq_filta) eq 1 then WIDGET_CONTROL,b5(0),set_button=1
      if n_elements(unq_filtb) eq 1 then WIDGET_CONTROL,b6(0),set_button=1

    end  ;clear

;Exit handler, set the SS vector, clean up and exit

    'Exit': BEGIN

     if not iflag then totmsk = mk_mask(indmp,logic)     ;if no choices made     

     ssw=where(totmsk eq 1)            ; make the SS vector

     WIDGET_CONTROL, /DESTROY, evnt.top

     iflag = 0    ;clear the init flag
     logic = 'AND' 

     END    ;exit

  ELSE:  print,'No match found in case :',uvalue
  ENDCASE  ;uvalue

ENDIF   ;button case

END
