; MODIFICATION HISTORY:
;     18-Jul-2001 GN
;       Replaced get_index by ovsa_get_index to avoid conflict with Yohkoh
;-
pro assocscan_event, event

   ; Read the state structure from the child base
   base = event.handler
   stash = widget_info(base,/child)

   widget_control,stash,get_uvalue=state,/no_copy

   WIDGET_CONTROL,Event.Id,GET_UVALUE=Ev

   CASE Ev OF
   'TAB_SEL': BEGIN
         if (Event.Type eq 4) then begin
            state.select = [Event.sel_left,Event.sel_top,$
                            Event.sel_right,Event.sel_bottom<(state.nscans-1)]
            IF (state.select(0) EQ state.select(2)) THEN BEGIN
               state.column = state.select(0)
               if (state.column eq -1) then goto,write_state
            ENDIF ELSE GOTO,WRITE_STATE
            isegs = where(state.segs[state.column,*] ne 0,nsegs)
            if (nsegs gt 1) then begin
               segs = reform(state.segs[state.column,isegs])
               ptr_free,state.pcurlist
               state.pcurlist = ptr_new(state.seglist[[segs]])
            endif else begin
               segs = state.segs[state.column,isegs]
               ptr_free,state.pcurlist
               state.pcurlist = ptr_new(state.seglist[segs])
            endelse
            widget_control,state.TBL2,set_value=*state.pcurlist
         endif
      END
   'LIST_SEL': BEGIN
         Index = EVENT.INDEX
         widget_control,state.TableID,GET_VALUE=table
         i1 = state.select(1)
         i2 = state.select(3)
         col = state.column
         for i = i1, i2 do begin
            table(col,i) = strmid((*state.pcurlist)[index],13,5)
         endfor
         widget_control,state.TableID,SET_VALUE=table
      END
    'SAVE': BEGIN
         widget_control,state.TableID,GET_VALUE=table
         ncodes = n_elements(table[*,0])
         for i = 0, ncodes-1 do (*state.idx.pscan).recs[i] = reform(table[i,*])
         lun = openarc(state.filename,a,nrec,/update)
         if (lun gt 0) then begin
            result = put_index(a,nrec,state.idx)
            free_lun,lun
         endif
         WIDGET_CONTROL,base,/DESTROY
         return
      END
   ENDCASE

WRITE_STATE:
   WIDGET_CONTROL, stash, SET_UVALUE=state, /NO_COPY

return
end

pro assocscan,filename, group = Group

   IF N_ELEMENTS(Group) EQ 0 THEN GROUP=0

   if (n_elements(filename) eq 0) then begin
      filename = dialog_pickfile(path=!defaults.datadir,filter='*.arc')
      if (filename eq '') then return
   endif

   lun = openarc(filename,a,nrec)
   if (lun eq 0) then return

   idx = ovsa_get_index(a,nrec)

   if (size(idx,/type) ne 8) then return
   scan = (*idx.pscan)
   nscans = idx.nscans
   ntempl = idx.n_templ

   ; Get list of records of various segment types
   seglist = get_segments(a,nrec)
   segs = intarr(ntempl,100)    ; Array of records for each segment type (max of 100)
   for i = 0, ntempl-1 do begin
      typstr = get_segtyp(idx.codes[i])
      ilist = where(strpos(seglist,typstr) ne -1,nlist)
      if (nlist ne 0) then begin
         segs[i,0:(nlist-1)<99] = ilist[0:(nlist-1)<99]
      endif
   endfor

   col_lab = (tag_names(!segm))[idx.codes[0:ntempl-1]-1]
   row_lab = strarr(nscans)
   recs = scan.recs[0:ntempl-1,*]
   for i = 0, nscans-1 do begin
      row_lab[i] = scan[i].srcname+strmid(msec2str(scan[i].smsec),1,8)+'-'+strmid(msec2str(scan[i].emsec),1,8)
   endfor
   ; Define SCNTRY structure
;   scn = {scntry, scancode: 0, segcode: 0, srec: 0, syear: 0, sday: 0, smsec: 0L,$
;          srcname:' ', hao: 0.0, deco:0.0, erec: 0, eyear: 0, eday: 0, emsec: 0L,$
;          recs: intarr(20)}

   base = widget_base(TITLE='Assoc Scan',GROUP_LEADER=Group,/column)
   base2 = widget_base(base,/ROW)
   lbase = widget_base(base2,/column)
   rbase = widget_base(base2,/column)

   maxlines = idx.nscans
   fmt = strarr(5,maxlines)
   for i = 0, maxlines-1 do begin
      fmt(*,i) = ['(I3.3)','(I2.2)','(I2.2)','(a)','(a)']
   endfor
   TableId = WIDGET_TABLE(LBASE,VALUE=recs,COLUMN_WIDTHS=replicate(40,ntempl),$
       COLUMN_LABELS=col_lab, ROW_LABELS=row_lab,Y_SCROLL_SIZE=15,font='Lucida Console*10',$
       ALIGNMENT=1,UVALUE='TAB_SEL',/ALL_EVENTS)

   TBL2 = WIDGET_LIST(LBASE,VALUE='',font='Lucida Console*10',$
       UVALUE='LIST_SEL',ysize=10)

   BTN = WIDGET_BUTTON(RBASE,VALUE='Save and Exit',UVALUE='SAVE')

   ; Realize the heirarchy
   widget_control,base,/realize

   state = { base:base,base2:base2,TableId:TableId,tbl2:tbl2,select:[0,0,0,0],$
    nscans:nscans,column:-1, seglist:seglist, segs:segs, pcurlist:ptr_new(0),$
    idx:idx,filename:filename}

   widget_control,widget_info(base,/child),set_uvalue=state, /no_copy

   free_lun,lun

   ; Call XMANAGER to handle the event messages
   xmanager,'assocscan',base, /NO_BLOCK

return
end