;+
; NAME:
;     WDLA_ADJ
; PURPOSE:
;     Simple table widget to allow adjustment of delay offsets, in case
;     of a bad fit to the delay peak.
; CATEGORY:
;     OVRO APC CALIBRATION
; CALLING SEQUENCE:
;     wdla_adj,in,out
; INPUTS:
;     in     the array of delay offsets to be adjusted, as determined by
;              DLASCAN routine.
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
; OUTPUTS:
;     out    a _pointer_ to the returned array of adjusted delay offsets.
;              The pointer points to a copy of IN, if no adjustments
;              were made.
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 11-Jan-2001 by Dale E. Gary
;     10-Jul-2004  DG
;       Add case of 21 baselines (7 antennas)
;     07-Apr-2006  DG
;       Add EDITABLE keyword to TABLE widget, which is now necessary
;       for IDL 6.2
;-

pro wdla_adj_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.n_in-1)]
            IF (state.select(0) eq 0 and state.select(2) eq 0) THEN WIDGET_CONTROL,state.TableID,EDIT_CELL=[state.select(1),0]
            GOTO,WRITE_STATE
         endif
      END
    'SAVE': BEGIN
         widget_control,state.TableID,GET_VALUE=table
         *state.out = reform(table)
         WIDGET_CONTROL,base,/DESTROY
         return
      END
   ENDCASE

WRITE_STATE:
   WIDGET_CONTROL, stash, SET_UVALUE=state, /NO_COPY

return
end

pro wdla_adj,in,out, group = Group

   IF N_ELEMENTS(Group) EQ 0 THEN GROUP=0

   if (n_elements(out) ne 0) then if (size(out,/type) eq 10) then ptr_free,out
   out = ptr_new(in)

   n_in = n_elements(in)
   col_lab = ['Delay Offset']
   case n_in of
      10: begin
           row_lab = ['1-2','1-4','1-5','1-6','2-4','2-5','2-6','4-5','4-6','5-6']
          end
      15: begin
           row_lab = ['1-2','1-4','1-5','1-6','1-7','2-4','2-5','2-6','2-7',$
                      '4-5','4-6','4-7','5-6','5-7','6-7']
          end
      21: begin
           row_lab = ['1-2','1-4','1-5','1-6','1-7','1-8','2-4','2-5','2-6','2-7','2-8',$
                      '4-5','4-6','4-7','4-8','5-6','5-7','5-8','6-7','6-8','7-8']
          end
   endcase

   base = widget_base(TITLE='Delay Offset Adjust',GROUP_LEADER=Group,/column,/modal)
   base2 = widget_base(base,/ROW)
   lbase = widget_base(base2,/column)
   rbase = widget_base(base2,/column)

   lbl = WIDGET_LABEL(LBASE,VALUE='Select cell to change delay offset')
   fmt = reform(replicate('(f5.2)',n_in),1,n_in)
   TableId = WIDGET_TABLE(LBASE,VALUE=reform(in,1,n_in),COLUMN_WIDTHS=[80],FORMAT=fmt,$
       COLUMN_LABELS=col_lab, ROW_LABELS=row_lab,Y_SCROLL_SIZE=15,font='Lucida Console*10',$
       ALIGNMENT=1,UVALUE='TAB_SEL',/ALL_EVENTS,/EDITABLE)

   BTN = WIDGET_BUTTON(RBASE,VALUE='Save and Exit',UVALUE='SAVE')

   ; Realize the heirarchy
   widget_control,base,/realize

   state = { base:base,base2:base2,TableId:TableId,select:[0,0,0,0],$
    n_in:n_in, column:-1, pcurlist:ptr_new(0), out:out}

   widget_control,widget_info(base,/child),set_uvalue=state, /no_copy

   ; Call XMANAGER to handle the event messages
   xmanager,'wdla_adj',base

return
end