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


pro spex_evnt_event, event

common spex_evnt_widgets, base, instrument, dformat, wflare, wftext, wmessage, $
    wcurrent, flarenum

; common flare_catalog contains the data read from BATSE_FLARE_CATALOG
;
common flare_catalog, $
       num_flares, $ ; number of flares in catalog
       batse  ; array of structures containing flare information for
                     ; num_flares flares.  Structure is in log_struct.pro.

widget_control, event.id, get_value = value, get_uvalue = uvalue
wtype = widg_type(event.id)

case wtype of

   'BUTTON': begin  ; start button case block

      q=where (event.id eq wflare)
      if q(0) ne -1 then begin
         many = 0
         row = fix(q(0)/5) & col = q(0)-row*5
         widget_control, wflare(col,0), set_value=value
         flarenum = ' '
         for i=0,4 do begin
            widget_control, wflare(i,0), get_value=val
            flarenum = val + flarenum
         endfor
         widget_control, wmessage, /append, set_value = $
            'You selected flare ' + flarenum
     flarenum = long(flarenum)
         goto,goback
      endif

  
      input_type = strmid (uvalue, 0, 2)
      input_value = strmid (uvalue, 2, 100)
   
      case input_type of  ; start input_type case block

         'c_': begin    ; start command (c_) case block
            case input_value of  ; start input_value within c_ case block
   
               'Time': begin
                  widget_control, wcurrent, set_value = $
                     'Starting date selection widget.  Please wait...'
                  twidget, rtime, group_leader=base
                  y = rtime.year-1900 & m = rtime.month & d = rtime.day
                  if (y le 0) or (m le 0) or (d le 0) then begin
                     widget_control, wmessage, /append, set_value = $
                        'Wild card dates not allowed.  Please reselect date.'
                     goto, goback
                  endif
                  t = utime ( strtrim(string(y),2)+'/'+strtrim(string(m),2)+$
                             '/'+strtrim(string(d),2) )
                  widget_control, wmessage, /append, set_value = $
                     'You selected date ' + anytim(t,/date,/yohkoh)
          case 1 of
           strpos(instrument+dformat,'SMMHXRBS') ne -1: begin
            btitle = '  Click on one of the following HXRBS Flares: ' 
            ;/batse means use batse style tagnames in fldata
                        read_minicat, fldata, numfl, time=t, error=error, /batse   
           end
           strpos(instrument+dformat,'BATSE') ne -1:    begin
              btitle = '  Click on one of the following BATSE Flares: ' 
              batse_read_cat
              if n_elements(batse) gt 0 then begin
               test_date = (anytim(t,/sec,/date))(0)
               if strpos(dformat, 'HERS') ne -1 then batse = batse(where(batse.burst_num ge 1))
               wdate = where( anytim(/sec,/date,batse.start_secs) eq test_date or $
                anytim(/sec,/date,batse.start_secs+batse.duration) eq test_date, numfl)
                       if numfl ge 1 then  begin
            fldata=batse(wdate)
                fldata=fldata(sort(fldata.start_secs))
               endif else error = 1
              endif else error=1
           end            
          endcase
                  if fcheck(error,0) then begin
                     widget_control, wmessage, /append, set_value = $
                        'Error reading flare catalog.'
                     goto,goback
                  endif
                  if numfl eq 0 then begin
                     widget_control, wmessage, /append, set_value = $
                        'No flares on ' + anytim(t,/yohkoh,/date)
                     goto,goback
                  endif
                  widget_control, wcurrent, set_value = $
                     'Starting flare selection widget.  Please wait...'
                  choose_fl, fldata, numfl, fl_selected, group_leader=base, $
            info=instrument+dformat
                  flcount = 1
                  flarray = intarr(1) 
                  flarray(0) = fl_selected
          flarenum = fl_selected
                  widget_control, wmessage, /append, set_value = $
                     'You selected flare ' + strtrim(string(fl_selected),2)
                  fl = string(flarray(0), format='(i5.5)')
                  for i=0,4 do begin
                     widget_control, wflare(i,0), $
                          set_value = strmid(fl,4-i,1)
                  endfor
                  end

               'Done': goto,exit

               else: print,'Error in command.'

            endcase  ; end of input_value case block
            end


      else: print,'error in widget'

   endcase   ;end of command (c_) case block

   end  ; end button block

   else: print,'error in input - doesn''t match any of wtypes.'
endcase   ; end wtype case block

goto, goback

exit:
;xbackregister,'flash_bck',event.top,/unregister ;-- kill flashing widget
widget_control, event.top, /destroy
goto, getout

goback:
;widget_flash, 'Select flare(s), plot options, or command button.', wcurrent
widget_control, wcurrent, $
   set_value = 'Select flare'

getout:
return & end



pro spex_evnt, group_leader=group, data_tipe=data_tipe, flare_num
     
common spex_evnt_widgets, base, instrument, dformat, wflare, wftext, wmessage, $
     wcurrent, flarenum

instrument = strupcase(data_tipe(0))
dformat    = strupcase(data_tipe(1))
flarenum=fcheck(flarenum, fcheck(flare_num,0))

if n_elements(group) eq 0 then widget_control, /reset



device, get_screen_size = sc
fspace = .0146 * sc(0) * .5
fxpad = .0117 * sc(0) * .5
fypad = .0146 * sc(1) * .5

btitle = 'SPEX Event Selector'
base = widget_base (title=btitle, xpad=fxpad, ypad=fypad, $
                    space=fspace, /column, /frame)

r0 = widget_base (base, /column, /frame)
whattodo = 'Select flare for '+arr2str(strupcase(data_tipe),delim=' ')
wcurrent = widget_label (r0, ysize=3, value=whattodo, uvalue='BACKGROUND')
widget_flash, whattodo, wcurrent

r1 = widget_base (base, /column, /frame, xpad = fxpad*.8)
w = widget_label (r1, value='Select Flare Number by:  ')
r1group = widget_base (r1, /column, /frame)
r1r1 = widget_base (r1group, /row)
w = widget_label (r1r1, value='Buttons:  ')
wflare = lonarr (5,11)
for i=4,0,-1 do begin
   wflare(i,0) = widget_button(r1r1, value='0',menu=2)
   for j=0,9 do begin
      val = string(j,format='(i1)')
      wflare(i,j+1) = widget_button (wflare(i,0), $
                      value=val, uvalue='f_'+val)
   endfor
endfor
widget_control, wflare(0,0), set_value = '1'
w = widget_label (r1r1, value=' or ')
wtime = widget_button (r1r1, value='Time', uvalue='c_Time')
r1r2 = widget_base (r1group, /column)
;w = widget_label (r1r2, value='or Typing single flare or range of flares ' + $
;                  '(e.g. 2,4,5-10): ')
;wftext = widget_text (r1r2, /edit)

r4 = widget_base (base, /row, xpad=fxpad, ypad=fypad, $
                    space=fspace)
wdone = widget_button (r4, value='Done', uvalue='c_Done')

r5 = widget_base (base, /column, /frame)
wm = widget_label (r5, value='Message Window')
wmessage = widget_text (r5, /scroll, ysize=8)

widget_control, base, /realize
;xmanager, 'hxrbs', base, group_leader=group, background='flash_bck'
xmanager, 'spex_evnt', base, group_leader=group
flare_num = flarenum
return & end