Pro thm_ui_config_temp_mess, id, text_in
  widget_control, id, get_uval = state, /no_copy
  widget_control, state.messw, set_val = text_in
  widget_control, id, set_uval = state, /no_copy
  If(obj_valid(!themis.progobj)) Then $
    !themis.progobj -> update, 0.0, /history_too, text = text_in
  Return
End

;+
;NAME:
; thm_ui_config
;PURPOSE:
; A widget that allows the user to set some of the fields in the
; !themis system variable: Also allows the user to set the themis
; configuration text file, and save it
;HISTORY:
; 17-may-2007, jmm, jimm@ssl.berkeley.edu
; 2-jul-2007, jmm, 'Add trailing slash to data directories, if necessary
;$LastChangedBy$
;$LastChangedDate$
;$LastChangedRevision$
;$URL$
;-
Pro thm_ui_config_event, event
  widget_control, event.id, get_uval = uval
  Case uval Of
    'LOCALDIR': Begin
      widget_control, event.id, get_val = temp_string
      temp_string = strtrim(temp_string, 2)
      ll = strmid(temp_string, strlen(temp_string)-1, 1)
      If(ll Ne '/' And ll Ne '\') Then temp_string = temp_string+'/'
      !themis.local_data_dir = temporary(temp_string)
      thm_ui_config_temp_mess, event.top, '!themis.local_data_dir = '+!themis.local_data_dir
    End
    'REMOTEDIR': Begin
      widget_control, event.id, get_val = temp_string
      temp_string = strtrim(temp_string, 2)
      ll = strmid(temp_string, strlen(temp_string)-1, 1)
      If(ll Ne '/' And ll Ne '\') Then temp_string = temp_string+'/'
      !themis.remote_data_dir = temporary(temp_string)
      thm_ui_config_temp_mess, event.top, '!themis.remote_data_dir = '+!themis.remote_data_dir
    End
    'NODOWNLOAD':Begin
      widget_control, event.id, get_val = temp_string
      If(event.type Le 2) Then Begin
        tt = fix(temp_string)
        If(tt Eq 0 Or tt Eq 1) Then Begin
          !themis.no_download = tt 
          thm_ui_config_temp_mess, event.top, '!themis.no_download ='+strcompress(string(tt))
        Endif Else Begin
          thm_ui_config_temp_mess, event.top, '!themis.no_download, Invalid Input, please input 0 or 1'
        Endelse
      Endif
    End
    'NOUPDATE':Begin
      widget_control, event.id, get_val = temp_string
      If(event.type Le 2) Then Begin
        tt = fix(temp_string)
        If(tt Eq 0 Or tt Eq 1) Then Begin
          !themis.no_update = tt 
          thm_ui_config_temp_mess, event.top, '!themis.no_update ='+strcompress(string(tt))
        Endif Else Begin
          thm_ui_config_temp_mess, event.top, '!themis.no_update, Invalid Input, please input 0 or 1'
        Endelse
      Endif
    End
    'DOWNLOADONLY':Begin
      widget_control, event.id, get_val = temp_string
      If(event.type Le 2) Then Begin
        tt = fix(temp_string)
        If(tt Eq 0 Or tt Eq 1) Then Begin
          !themis.downloadonly = tt 
          thm_ui_config_temp_mess, event.top, '!themis.downloadonly ='+strcompress(string(tt))
        Endif Else Begin
          thm_ui_config_temp_mess, event.top, '!themis.downloadonly, Invalid Input, please input 0 or 1'
        Endelse
      Endif
    End
    'VERBOSE':Begin
      widget_control, event.id, get_val = temp_string
      If(event.type Le 2) Then Begin
        tt = fix(temp_string)
        If(tt le 10  And tt Ge 0) Then Begin
          !themis.verbose = tt 
          thm_ui_config_temp_mess, event.top, '!themis.verbose ='+strcompress(string(tt))
        Endif Else Begin
          thm_ui_config_temp_mess, event.top, '!themis.verbose, Invalid Input, must be LE 10'
        Endelse
      Endif
    End
    'RESET': Begin
      thm_ui_config_temp_mess, event.top, 'Restoring Original Configuration'
      widget_control, event.top, get_uval = state, /no_copy
      !themis = state.thm_cfg_sav
      widget_control, state.localdir, set_val = !themis.local_data_dir
      widget_control, state.remotedir, set_val = !themis.remote_data_dir
      widget_control, state.nodownload, set_val = string(!themis.no_download)
      widget_control, state.noupdate, set_val = string(!themis.no_update)
      widget_control, state.downloadonly, set_val = string(!themis.downloadonly)
      widget_control, state.verbose, set_val = string(!themis.verbose)
      widget_control, event.top, set_uval = state, /no_copy
    End
   'RESETTODEFAULT': Begin
      dir =  thm_config_filedir(/app_query)
      If(dir[0] Ne '') Then Begin
;Is there a trailing slash? Not for linux or windows, not sure about
;Mac                                 
        ll =  strmid(dir, strlen(dir)-1, 1)
        If(ll Eq '/' Or ll Eq '\') Then filex =  dir+'thm_config.txt' $
        Else filex =  dir+'/'+'thm_config.txt'
;Does the file exist? If so, delete it
        If(file_search(filex) Ne '') Then file_delete, filex, /noexpand_path
      Endif
      thm_ui_config_temp_mess, event.top, 'Restoring Default Configuration'
;No config file, so just run themis_init
      thm_init,  /reset
;Now you need to put the valuse that you have back into the widget
      widget_control,  event.top,  get_uval =  state,  /no_copy
      widget_control,  state.localdir,  set_val =  !themis.local_data_dir
      widget_control,  state.remotedir,  set_val =  !themis.remote_data_dir
      widget_control,  state.nodownload,  set_val =  string(!themis.no_download)
      widget_control,  state.noupdate,  set_val =  string(!themis.no_update)
      widget_control,  state.downloadonly,  set_val =  string(!themis.downloadonly)
      widget_control,  state.verbose,  set_val =  string(!themis.verbose)
      widget_control,  event.top,  set_uval =  state,  /no_copy
    End
    'SAVE': Begin
      thm_write_config
      filex = thm_config_filedir()+'/'+'thm_config.txt'
      thm_ui_config_temp_mess, event.top, 'Saved New Configuration file: '+filex
    End
    'EXIT': Begin
      widget_control, event.top, /destroy
    End
  Endcase

Return
End
Pro thm_ui_config

;If !themis does not exist, set it
  thm_init
  thm_cfg_sav = !themis

;Build the widget
  master = widget_base(/col, $
                       title = 'THEMIS Science Software: Configuration', $
                       /align_top)
;widget base for values to set
  vmaster = widget_base(master, /col, /align_left)
  vlabel = widget_label(vmaster, value = 'Configuration Settings')
;Widget base for save, reset and exit buttons
  bmaster = widget_base(master, /row, /align_left)
  ll = max(strlen([!themis.local_data_dir, !themis.remote_data_dir]))+12
;Now editable text widgets
  lbase = widget_base(vmaster, /row, /align_left)
  localdir = widget_text(lbase, /edit, /all_events, xsiz = ll, $
                         uval = 'LOCALDIR', val = !themis.local_data_dir)
  flabel = widget_label(lbase, value = 'Local data directory')
  rbase = widget_base(vmaster, /row, /align_left)
  remotedir = widget_text(rbase, /edit, /all_events, xsiz = ll, $
                          uval = 'REMOTEDIR', val = !themis.remote_data_dir)
  flabel = widget_label(rbase, value = 'Remote data directory')
  ndbase = widget_base(vmaster, /row, /align_left)
  nodownload = widget_text(ndbase, /edit, /all_events, xsiz = 10, $
                           uval = 'NODOWNLOAD', val = string(!themis.no_download))
  flabel = widget_label(ndbase, value = 'no_download: 0 for auto download, 1 to use local data only')
  nubase = widget_base(vmaster, /row, /align_left)
  noupdate = widget_text(nubase, /edit, /all_events, xsiz = 10, $
                         uval = 'NOUPDATE', val = string(!themis.no_update))
  flabel = widget_label(nubase, value = 'no_update: 0 - update files if newer on server, 1 - never overwrite local files')
  dwbase = widget_base(vmaster, /row, /align_left)
  downloadonly = widget_text(dwbase, /edit, /all_events, xsiz = 10, $
                             uval = 'DOWNLOADONLY', val = string(!themis.downloadonly))
  flabel = widget_label(dwbase, value = 'downloadonly: 0 to download and load data, 1 to only download')
  vbbase = widget_base(vmaster, /row, /align_left)
  verbose = widget_text(vbbase, /edit, /all_events, xsiz = 10, $
                        uval = 'VERBOSE', val = string(!themis.verbose))
  flabel = widget_label(vbbase, value = 'Verbose: from 0 to 10, higher value = more comments')

;buttons
  savebut = widget_button(bmaster, value = '     Save     ', uvalue = 'SAVE')
  resetbut = widget_button(bmaster, value = '     Reset     ', uvalue = 'RESET')
  reset_to_dbutton =  widget_button(bmaster,  value =  '  Reset to Default   ',  uvalue =  'RESETTODEFAULT')
  exitbut = widget_button(bmaster, value = '    Close     ', uvalue = 'EXIT')

;message widget
  messw = widget_text(master, value = '', xsize = ll, ysize = 1, /scroll)


  state = {thm_cfg_sav:thm_cfg_sav, $
          localdir:localdir, remotedir:remotedir, $
          nodownload:nodownload, noupdate:noupdate, $
          downloadonly:downloadonly, verbose:verbose, $
          messw:messw}
  
  widget_control, master, set_uval = state, /no_copy
  widget_control, master, /realize
  xmanager, 'thm_ui_config', master, /no_block

End