;+
; NAME:
;             WBSA
; PURPOSE:
;             to provide widget interface to MKBSA/MKBSA programs
; CATEGORY: 
;             widgets 
; CALLING SEQUENCE:
;             WBSA,BSA_CON
; OUTPUTS:
;             BSA_CON - Control structure to guide MKBSC
; RESTRICTIONS: 
;             needs X-windows 
; MODIFICATION HISTORY:
;             written Dec'92 by DMZ (ARC) 
;-

pro wbsa_event, event                         ;WBSA event handler

common wbsa_con,i_bsa_con

;-- get event widget id's and type

widget_control,event.top,get_uvalue=widget_id


wids=tag_names(widget_id) & nwids=n_elements(wids)
for i=0,nwids-1 do begin
 state=wids(i)+' = '+'widget_id.('+strtrim(string(i,'(i2)'),2)+')'
 s=execute(state)
endfor

widget_control, event.id, GET_UVALUE = USERVALUE
if (n_elements(uservalue) eq 0) then uservalue=''
wtype=strmid(tag_names(event,/structure_name),7,1000)

;-- SLIDER widget?

if (wtype eq 'SLIDER') then begin
 accum=float(event.value)
 sindex=where(event.id eq saccum)
 i_bsa_con.accum(sindex(0))=accum(0)
endif

;-- BUTTON widget?

if wtype eq  'BUTTON' then begin
 buttname=strtrim(uservalue,2)

 case buttname of 


  'DEADTIME' : i_bsa_con.deadtime=event.select

  'FLUXCAL' : begin
               i_bsa_con.fluxcal=event.select
               if (i_bsa_con.fluxcal eq 1) and (i_bsa_con.xcorr eq 0) then begin
                i_bsa_con.xcorr=1 & widget_control,corrb(1),set_button=1
               endif
              end

   'READY'  : begin
               find=where(i_bsa_con.chans ne 0, count)
               if count eq 0 then begin
                widget_control,info,set_value='NO CHANNELS SELECTED',/append
                widget_control,info,set_value='PLEASE MAKE A SELECTION',/append
                return
               endif
               widget_control,event.top,/destroy,/clear_events,bad_id=destroyed
               return
              end

  'WAVECAL' : i_bsa_con.wavecal=event.select

  'ERRCAL'  : i_bsa_con.errcal=event.select

  'XCORR'  :  begin
               i_bsa_con.xcorr=event.select
               if (i_bsa_con.fluxcal eq 1) and (i_bsa_con.xcorr eq 0) then begin
                widget_control,corrb(2),set_button=0
                i_bsa_con.fluxcal=0
               endif
              end

  else:       begin
               det=strpos(buttname,'D_')    ;-- channel button
               if det ge 0 then begin
                detno=fix(strmid(buttname,2,2))
                widget_control,saccum(detno-1),sensitive=(event.select)
                i_bsa_con.chans(detno-1)=detno*event.select
               endif
              end
 endcase
 return
endif

return & end

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

pro wbsa,bsa_con,err_flag=err_flag,group=group

common wbsa_con,i_bsa_con

;-- initialize

err_flag=0
if ((!d.flags and 65536) eq 0) or (!d.name ne 'X') then begin
 message,'widgets are unavailable',/cont
 err_flag=1 & return
endif


if n_elements(group) eq 0 then begin
 widget_control,/reset,/clear_events
endif else begin
 if xregistered('wbsa') then begin
  message,'WBSA already registered',/continue
  return
 endif
endelse


;-- autosize screen

device,get_screen_size=sc
fspace=.0146*sc(0)
fxpad=.0117*sc(0)
fypad=.0146*sc(1)
scx=sc(0)/1280. & scy=sc(1)/1024.

;-- parent widget

btitle='MKBSA WIDGET INTERFACE'
base = widget_base(TITLE =btitle, XPAD =fxpad, YPAD =fypad,$
                   SPACE = fspace,/frame,/column)

;-- message window

row2 = widget_base(base, XPAD =fxpad, YPAD =fypad,$
                   SPACE = fspace,/frame,/column)
rlabel=widget_label(row2)
widget_control,rlabel,set_value='MESSAGE WINDOW'
info=widget_text(row2,ysize=2)
widget_control,info,set_value='START BY SELECTING A CHANNEL',/append

;-- row channel buttons

bions = ['Fe XXVI', 'Fe XXV', 'Ca XIX','S XV']
channels=strtrim(string(indgen(4)+1),2)
detb=lonarr(4)
xmenu,channels+' - '+bions,base,buttons=detb,/nonexclusive,$
 title='SELECTABLE BCS CHANNELS', /row,space=fspace,uvalue='D_'+strtrim(channels,2)


;-- corrections

popts=['Deadtime','Curvature','Flux','Wavelength','Uncertainties']
pvalues=['DEADTIME','XCORR','FLUXCAL','WAVECAL','ERRCAL']

xmenu,popts,base,buttons=corrb,/nonexclusive,/row,title='CALIBRATIONS TO APPLY (ALL CHANNELS)',$
 uvalue=pvalues,space=fspace


;-- accumulation sliders

row1 = widget_base(base, XPAD =fxpad, YPAD =fypad,$
                   SPACE = fspace,/frame,/column)
rlabel=widget_label(row1)
saccum=lonarr(4)
widget_control,rlabel,set_value='SELECTABLE ACCUMULATION TIMES (SECS)'
                              
for i=0,3 do begin
 saccum(i)=widget_slider(row1,minimum=3,maximum=100.,title=bions(i))
 widget_control,saccum(i),sensitive=0
endfor


;-- ready button

main = widget_button(base,value='PRESS WHEN READY TO GO',uvalue='READY',/no_release,/frame)

;-- internal BSA Control structure to store new selections

if n_elements(i_bsa_con) eq 0 then i_bsa_con=mk_bsa_ans() else begin
 
;-- recall last settings
                         
 chans=i_bsa_con.chans
 accum=i_bsa_con.accum
 
 for i=0,3 do begin
  widget_control,detb(i),set_button=(chans(i) < 1)
  widget_control,saccum(i),sensitive=(chans(i) < 1),set_value=(accum(i) > 3)
 endfor

 for i=0,n_elements(corrb)-1 do begin
  str='widget_control,corrb('+string(i,'(i1)')+'),set_button=i_bsa_con.'+pvalues(i)
  s=execute(str)
 endfor
endelse

widget_control,base,/realize

;-- save widget id's in structure

widget_id={ base:base		,$
            info:info           ,$
            saccum:saccum       ,$
            corrb:corrb           ,$
            detb:detb           }

widget_control,base,set_uvalue=widget_id
                                                

xmanager,'wbsa',base,group_leader=group
xmanager

if n_elements(group) eq 0 then begin
 message,'cleaning up...',/continue
endif

;-- pass data out of widget

if n_elements(i_bsa_con) ne 0 then bsa_con=i_bsa_con

return & end

