;+
; NAME:
;       XCAL
;
; PURPOSE:
;
; CATEGORY:
;       Hansteen/Wikstol Data analysis SW
;
; CALLING SEQUENCE:
;
; INPUTS:
;
; KEYWORD PARAMETERS:
;
; Outputs:
;       None
;
; CALLS:
;
;
; COMMON BLOCKS:
;
;
; PROCEDURE:
;
; RESTRICTIONS:
;
;
; MODIFICATION HISTORY:
;       15-Jun-2004: Oivind Wikstol. Version 1.0
;        6-Nov-2007: AG : disabled the ff file selection and set
;                         the button values to calstat.
;        8-Nov-2007: AG : modified the xcaldef_dcfile method to implement 
;                         the selection of a valid file.
;       12-Dec-2007: AG : FF commented.
;        4-Jan-2008: AG : Prevented the selection of "dc" and "ff"
;        8-Jan-2008: AG : "ff" option commented
; $Id: xcaldef.pro,v 1.6 2008/01/08 14:23:13 gardini Exp $
;-
;
; read in DC data: either manually pick, given in dc_file, or default set in eis_cal object
  
pro xcaldef_dcfile, event
  widget_control, event.top, get_uvalue = info
  title = 'Pick Dark Current File'
  dc_file = dialog_pickfile(get_path=dirname, $
            path = (*info).dirs.dc_dir, filter = '*.idlsave', title = title)
  if dc_file ne dirname then begin ; a valid file has been selected
    dum=strsplit(dc_file,path_sep(),/extract)
    filename=dum[n_elements(dum)-1]
    widget_control,(*info).dcfiletitle, set_value = filename
    widget_control,(*info).dcdirtitle,set_value = dirname
    (*info).dirs.dc_dir=dirname
    (*info).dirs.dc_file=filename
  endif
end

;pro xcaldef_ffile, event
;  widget_control, event.top, get_uvalue = info
;  title = 'Pick Flat Field File'
;  ff_file = dialog_pickfile(get_path=dirname, $
;            path = (*info).dirs.ff_dir, filter = '*.idlsave', title = title)
;  if ff_file eq '' then ff_file=(*info).dirs.ff_file
;  dum=strsplit(ff_file,path_sep(),/extract)
;  filename=dum[n_elements(dum)-1]
;; widget_control,(*info).ffiletitle, set_value = filename
;; widget_control,(*info).ffdirtitle,set_value = dirname
;  (*info).dirs.ff_dir=dirname
;  (*info).dirs.ff_file=filename 
;end

function xcaldef_csopt, event
  widget_control, event.top, get_uvalue = info
  widget_control, (*info).csopt, get_value = calstat
  calstat[0] = 1 
; calstat[4] = 0 
  widget_control, (*info).csopt, set_value = calstat
  (*info).calstat = calstat
  return,0
end
  

; close xcaldef
pro xcaldef_destroy, event
  widget_control, event.top, get_uvalue = info
  (*(*info).control).calinfo.calstat=(*info).calstat
  (*(*info).control).calinfo.dirs=(*info).dirs
  widget_control, event.top,/destroy
end

pro xcaldef_cleanup, tlb
  widget_control, tlb, get_uvalue = info
  ptr_free,info
end

pro xcaldef, event, group_leader = group

  widget_control, event.top, get_uvalue = control
  
  if n_params() ne 1 then begin
    message, $
      'No data object given as input',/cont
    return
  endif
;
  dirsep = get_delim()
  dirs=(*control).calinfo.dirs
  calstat=(*control).calinfo.calstat
  calinfo=(*control).calinfo
         
  tlb = widget_base(/row, title='Xcaldef: EIS Calibration control window', mbar=menubar, $
                    xoffset = 100,yoffset = 100)
                    
  lcol = widget_base(tlb, /frame, /column)      ;left column.
  rcol = widget_base(tlb, /frame, /column)      ;right column.

  ; create pulldown menus on the base widget menubar
  filemenu=widget_button(menubar, value='File',/menu, uvalue='file')
  exitmenu=widget_button(filemenu, value='Close', event_pro='xcaldef_destroy')

  ; calibration buttons -1 Dark Current file
  filefield = widget_base(lcol, /col, /frame)
  filetitle = widget_label(filefield, value = 'File selection   ')  
  
  
  dcdirtitle = widget_label(filefield, value = dirs.dc_dir) 
  dcfiletitle = widget_label(filefield, value = dirs.dc_file) 
  dcbutton = widget_button(filefield,  value = 'Reset DC file', $
                            event_pro = 'xcaldef_dcfile')
                            
; ffdirtitle = widget_label(filefield, value = dirs.ff_dir) 
; ffiletitle = widget_label(filefield, value = dirs.ff_file)
; ffbutton = widget_button(filefield,  value = 'Reset FF file', $
;                           event_pro = 'xcaldef_ffile')
; widget_control,ffbutton,sensitive=0
                            
  closefield = widget_base(lcol, /column)
  closebutton = widget_button(closefield, value = '   Finished   ', $
                              event_pro = 'xcaldef_destroy')
                              
  ; calibration options
  cs_base = widget_base(rcol, /row)
  title = 'Select Calibration'    ; line select label string
  cslabel = widget_label(cs_base, value=title)

  cs_id=calinfo.calstat_id
  csopt = cw_bgroup(cs_base, /nonexclusive, $
             cs_id, event_func = 'xcaldef_csopt')
  widget_control, csopt, set_value = calstat

  ; realize main window:
  widget_control, tlb, /realize, tlb_get_size = tlb_sz

  ; define the info structure, used send information around
  info = {tlb:tlb, $
          dirs:dirs, $
          dcdirtitle:dcdirtitle, $
          dcfiletitle:dcfiletitle, $
;         ffdirtitle:ffdirtitle, $
;         ffiletitle:ffiletitle, $
          csopt:csopt,$
          calstat:calstat,$
          control:control}
          
  info = ptr_new(info, /no_copy)

  ; set user value of tlb widget to be the info ptr
  
  widget_control, tlb, set_uvalue = info
  
  ; check if we have done any calibration before
;  (*info).dc_done=(dobj->getcalstat()).dc
;  if (*info).dc_done then begin
;    widget_control,(*info).dcqdbutton,sensitive=0
;    widget_control,(*info).dcbutton,sensitive=0
;  endif

  xmanager, 'xcaldef', tlb, /no_block, group_leader = group, cleanup = 'xcaldef_cleanup'

end
