; $Id: fans_settings.pro,v 1.9 2007/10/18 20:53:40 azuah Exp $
;###############################################################################
;
; NAME:
;   fans_settings
;
; PURPOSE:
;   Utility routines for FANS data reduction application defined in
;   the file fans_datreduc.pro. The routines in this file presents a
;   dialog in which the user can make choices about how multiple input
;   files should be dealt with. 
;
; CATEGORY:
;   FANS Data Reduction
;
; AUTHOR:
;   Richard Tumanjong Azuah
;   NIST Center for Neutron Research
;   100 Bureau Drive, Gaithersburg, MD 20899
;   United States
;   azuah@nist.gov; (301) 9755604
;   June, 2002
;
; LICENSE:
;  The software in this file is written by an employee of 
;  National Institute of Standards and Technology 
;  as part of the DAVE software project.
;
;  The DAVE software package is not subject to copyright protection
;  and is in the public domain. It should be considered as an
;  experimental neutron scattering data reduction, visualization, and
;  analysis system. As such, the authors assume no responsibility
;  whatsoever for its use, and make no guarantees, expressed or
;  implied, about its quality, reliability, or any other
;  characteristic. The use of certain trade names or commercial
;  products does not imply any endorsement of a particular product,
;  nor does it imply that the named product is necessarily the best
;  product for the stated purpose. We would appreciate acknowledgment
;  if the DAVE software is used of if the code in this file is
;  included in another product.
;
;###############################################################################



;===============================================================================
; fans_settings_event
;
; PURPOSE:
;   Event handler. Main handler for settings dialog.
;
; PARAMETERS:
;   event [in] - event structure
;
pro fans_settings_event, event

; Basic error Handler
if (n_elements(!debug) && (!debug eq 0)) then begin
    catch, catchError
    if (catchError ne 0) then begin
        ;;print, 'Error handled!'
        eTitle = 'fans_settings_event: Error encountered'
        eMsg = 'An error or unusual condition was encountered!'
        eMsg = [eMsg,'Please, report the following to the DAVE team:']
        eMsg = [eMsg,!error_state.msg]
;    eMsg = [eMsg,'Function/module name: calledfunc()',!error_state.msg]
        void = dialog_message(/error,eMsg,title=eTitle,dialog_parent=event.top)
        catch, /cancel
        return
    endif
endif

;***********************
; 1 meV = 8.065541 cm-1
;***********************
widget_control, event.top, get_uvalue=sPtr

sname = tag_names(event,/structure_name)
if (sname eq 'WIDGET_DROPLIST') then begin
    (*sPtr).index = event.index
    return
endif

ename = widget_info(event.id,/uname)

if (event.select eq 0) then begin ; ==> a radio button is being released
    case ename of               ; need to know when switching from 'separate'
        'ssep': (*sPtr).stype = 1
        'bsep': (*sPtr).stype = 2
        'fsep': (*sPtr).stype = 3
        else: return 
    endcase 
    return
endif

; If reach here ==> radio button is being set
case ename of

    'ssum': (*(*sPtr).mfoPtr)[0] = 0
    'sapd': (*(*sPtr).mfoPtr)[0] = 1
    'ssep': (*(*sPtr).mfoPtr)[0] = 2
    
    'bsum': (*(*sPtr).mfoPtr)[1] = 0
    'bapd': (*(*sPtr).mfoPtr)[1] = 1
    'bsep': (*(*sPtr).mfoPtr)[1] = 2

    'fsum': (*(*sPtr).mfoPtr)[2] = 0
    'fapd': (*(*sPtr).mfoPtr)[2] = 1
    'fsep': (*(*sPtr).mfoPtr)[2] = 2

    'mev': (*(*sPtr).mfoPtr)[3] = 0
    'wnos': (*(*sPtr).mfoPtr)[3] = 1

    'cancel': widget_control, event.top, /destroy

    'ok': begin
        (*sPtr).exitOK = 1
        widget_control, event.top, /destroy
    end
 
endcase

end
;-------------------------------------------------------------------------------



;===============================================================================
; fans_settings_event
;
; PURPOSE:
;   Event handler. For event generated when 'Change Options' button is
;   clicked. Build modal dialog that lets user set preferences on how
;   multiple files are to be handled.
;
; PARAMETERS:
;   event [in] - event structure
;
pro fans_settings, event

; Basic error Handler
if (n_elements(!debug) && (!debug eq 0)) then begin
    catch, catchError
    if (catchError ne 0) then begin
        ;;print, 'Error handled!'
        eTitle = 'fans_settings: Error encountered'
        eMsg = 'An error or unusual condition was encountered!'
        eMsg = [eMsg,'Please, report the following to the DAVE team:']
        eMsg = [eMsg,!error_state.msg]
;    eMsg = [eMsg,'Function/module name: calledfunc()',!error_state.msg]
        void = dialog_message(/error,eMsg,title=eTitle,dialog_parent=event.top)
        catch, /cancel
        return
    endif
endif


widget_control, event.top, get_uvalue = sPtr
mfoPtr=(*sPtr).mfoPtr
;mfoPtr = ptr_new([1,1,1,0,0])

; build the settings dialog
tlbID = widget_base(/col,title='CONFIGURE OPTIONS',/modal,group_leader=event.top)

cb0ID = widget_base(tlbID,/col,frame=1,/align_center,/grid)

void = widget_label(cb0ID,value='Multiple input files settings')

cb1ID = widget_base(cb0ID,/row)
cb1aID = widget_base(cb1ID,/row,/align_center)
cb1bID = widget_base(cb1ID,/row,/exclusive)

cb2ID = widget_base(cb0ID,/row)
cb2aID = widget_base(cb2ID,/row,/align_center)
cb2bID = widget_base(cb2ID,/row,/exclusive)

cb3ID = widget_base(cb0ID,/row)
cb3aID = widget_base(cb3ID,/row,/align_center)
cb3bID = widget_base(cb3ID,/row,/exclusive)

cb4ID = widget_base(tlbID,/row,frame=1,/align_center)
void = widget_label(cb4ID,value='Energy Units:')
;elist = ['mev','wavenumbers']
cb4aID = widget_base(cb4ID,/row,/exclusive,/align_center)
;cb4aID = widget_droplist(cb4ID,value=elist,2),uname='eunit',uvalue=elist)
;eunit='mev'

cb5ID = widget_base(tlbID,/row,/align_center,frame=1)

cb6ID = widget_base(tlbID,/row,/align_center)

void = widget_label(cb1aID,value='    Sample:')
sSumID = widget_button(cb1bID,value='sum',uname='ssum')
sapdID = widget_button(cb1bID,value='merge',uname='sapd',sensitive=0)
ssepID = widget_button(cb1bID,value='separate',uname='ssep')
case (*mfoPtr)[0] of 
    0: widget_control, sSumID, /set_button
    1: widget_control, sapdID, /set_button
    2: widget_control, ssepID, /set_button
endcase

void = widget_label(cb2aID,value='Background:')
bSumID = widget_button(cb2bID,value='sum',uname='bsum')
bapdID = widget_button(cb2bID,value='merge',uname='bapd',sensitive=0)
bsepID = widget_button(cb2bID,value='separate',uname='bsep')
case (*mfoPtr)[1] of 
    0: widget_control, bSumID, /set_button
    1: widget_control, bapdID, /set_button
    2: widget_control, bsepID, /set_button
endcase

void = widget_label(cb3aID,value=' Fast Bkgd:')
fSumID = widget_button(cb3bID,value='sum',uname='fsum')
fapdID = widget_button(cb3bID,value='merge',uname='fapd',sensitive=0)
fsepID = widget_button(cb3bID,value='separate',uname='fsep')
case (*mfoPtr)[2] of 
    0: widget_control, fSumID, /set_button
    1: widget_control, fapdID, /set_button
    2: widget_control, fsepID, /set_button
endcase

mevID = widget_button(cb4aID,value='meV',uname='mev')
wnosID = widget_button(cb4aID,value='wavenumbers',uname='wnos')
case (*mfoPtr)[3] of
    0: widget_control, mevID, /set_button
    1: widget_control, wnosID, /set_button
endcase 

void = widget_label(cb5ID,value='Number of active detectors:')
dlist = indgen(89)+40           ; can choose from 10 to 128
result = where(dlist eq (*sPtr).nosActDets)
nActDetID = widget_droplist(cb5ID,value=strtrim(string(dlist),2),uname='adet',uvalue=dlist)
widget_control, nActDetID, set_droplist_select=result[0] ; default is 50 detectors

okID = widget_button(cb6ID,value='OK',uname='ok')
cancelID = widget_button(cb6ID,value='CANCEL',uname='cancel')

widget_control, tlbID, /realize

tmpPtr = ptr_new({mfoPtr:ptr_new(*mfoPtr), $       ;ptr_new([*mfoPtr,0,0,50]), $
                  stype:0, $
                  exitOK:0, $
                  index:result[0] $
                 })
widget_control, tlbID, set_uvalue=tmpPtr

;store current mfoPtr settings
old_mfo = *mfoPtr

; lanch dialog and let user choose options
xmanager, 'fans_settings',tlbID

if ( (*tmpPtr).exitOK ) then begin ; ==> dialog exited with 'ok' so apply changes
    ;; note the current nos of active detectors
    (*sPtr).nosActDets = dlist[(*tmpPtr).index]

    ;; if switching from 'separate' analysis, record current indices
    if ((*tmpPtr).stype gt 0) then begin
        case (*tmpPtr).stype of
            1: (*sPtr).sampSepIndex = (*sPtr).sampIndex
            2: (*sPtr).bkgdSepIndex = (*sPtr).bkgdIndex
            3: (*sPtr).fastSepIndex = (*sPtr).fastIndex
        endcase 
    endif
    
    ;; make necessary updates to data
    (*mfoPtr) = (*(*tmpPtr).mfoPtr)
    if ( n_elements(where(*mfoPtr eq old_mfo)) ne 4 ) then begin ; ie if settings have altered
        fans_ApplySettings, sPtr, old_mfo[3], err=err, emsg=emsg
        if (err ne 0) then begin
            printerror, err, emsg
            return
        endif
    endif 
endif

ptr_free,(*tmpPtr).mfoPtr,tmpPtr                 ; no longer required

end

