; $Id: fans_filemenu.pro,v 1.10 2007/10/18 20:53:40 azuah Exp $
;###############################################################################
;
; NAME:
;   fans_filemanu
;
; PURPOSE:
;   Collection of event handlers and other support routines for the
;   FANS data reduction application defined in the file fans_datreduc.pro.
;   Deal with events triggered from the file menu.
;
; 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
;   May, 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.
;
;###############################################################################



;===============================================================================
; getFileList
;
; PURPOSE:
;   Get a list of files to be reduced from the user.
;
; PARAMETERS:
;   sPtr [in] - pointer to state structure
;
;   title [in] - to be used for file chooser dialog
;
;   lFileList [out] - fully qualified chosen files.
;
;   fileList [out] - basename of selected files.
;
;   count [out] - number of files chosen.
;
pro getFileList, sPtr, title,lFileList, fileList, count

datDir = (*sPtr).datDir         ; get current data directory

cd, datDir[0], cur=curDir       ; cd to fans data dir
lFileList = dialog_pickfile(title=title,/multiple_files,get_path=newDir, $
                            filter='*.bt4', /must_exist,dialog_parent=(*sPtr).tlbID)
cd, curDir                      ; go back to previous dir

count = (strtrim(lFilelist[0],2) eq '') ? 0 : n_elements(lFileList)

if (count le 0) then return     ; no need to carry on!

(*sPtr).datDir = newDir[0]      ; make newDir the current data dirctory
;; strip dir name from lFileList and store as fileList
fileList = strarr(count)
for i = 0,count-1 do begin
    pos = strpos(lFileList[i],!delimiter,/reverse_search) + 1
    length = strlen(lFileList[i]) - pos
    fileList[i] = strmid(lFileList[i],pos,length)
endfor


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



;===============================================================================
; getFileList
;
; PURPOSE:
;   Event handler. From the file top-level menu
;
; PARAMETERS:
;   event [in] - event structure
;
pro fans_fileMenu, 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_filemenu: 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

dataType = widget_info(event.id,/uname)

switch dataType of
    
    ;; change directory
    'dir': begin
        oldDir = (*sPtr).datDir ; get current setting
        
        newDir = dialog_pickfile(dialog_parent=event.top,/directory,path=oldDir[0]) ; set new dir
        
        if (strlen(newDir) le 0) then newDir = oldDir[0] ; use oldDir if nothing selected
        
        (*sPtr).datDir = newDir 
        
        break
    end

    ;; Choose sample file(s) to reduce
    'samp': begin
        
        ;; get list of files to be load
        getFileList, sPtr,'Select Sample Files',lFileList,fileList,count
        if (count le 0) then break ; bail out

        ;; read the data from the files
        fans_loadFiles, dataType,sPtr,lFileList,fileList,count,err=err,emsg=emsg 
        if (err ne 0) then begin
            printerror,err,emsg
            return
        endif

        ;; make samp and output views sensitive
        widget_control, (*sPtr).sViewID, sensitive=1
        widget_control, (*sPtr).oViewID, sensitive=1

        ;; reset status of multiple sample files processing
        (*sPtr).sampStatus = 0

        ;; if there are multiple files, sum or merge them if required
        if ((count gt 1) and ((*(*sPtr).mfoPtr)[0] ne 2)) then begin
            case (*(*sPtr).mfoPtr)[0] of
                0: fans_sumData, sPtr, dataType, err=err, emsg=emsg
                1: fans_apdData, sPtr, dataType, err=err, emsg=emsg
            endcase 
            if (err ne 0) then begin
                printerror,err,emsg
                return
            endif
        endif 


        ;; if the display option is 'Sample', then display the current
        ;; dataset in the sample list
        widget_control, (*sPtr).sViewID, get_uvalue=on
        if (on) then begin 

            ;; display current data in list
            fans_plotdata,sPtr,dataType

            ;; Reset the Energy Slider - it should be in sync with the
            ;; current data (ie being plotted). 
            index = (*sPtr).sampIndex
            fdStructPtr = (*(*sPtr).sDataPtr)[index]
            fans_syncSlider,(*sPtr).eSliderID,(*fdStructPtr).energyPtr, 1,err=err, emsg=emsg
            if (err ne 0) then begin
                printerror,err,emsg
                return
            endif
            
            ;; make plot window and eslider sensitive 
;            widget_control, (*sPtr).winVisID, sensitive=1
            widget_control, (*sPtr).eSliderID, sensitive=1

        endif

        ;; switch on output calculation flag
        (*sPtr).doOutpCalc = 1

        ;; Reset the mask detector list and widget display
        ;if ptr_valid((*sPtr).mskListPtr) then ptr_free,(*sPtr).mskListPtr
        ;widget_control, (*sPtr).mskListID, set_value=''

        ;; if the display option is 'Output', then update output plot
        widget_control, (*sPtr).oViewID, get_uvalue=on
        if (on) then begin
            ;; Calculate output data
            fans_outpCalc, sPtr, err=err, emsg=emsg
            if (err ne 0) then begin
                printerror,err,emsg
                return
            endif
            fans_plotData, sPtr, 'outp', err=err,emsg=emsg
        endif
        
        break
    end

   
    ;; choose bkgd file(s)
    'bkgd': begin

        getFileList, sPtr,'Select Background Files',lFileList,fileList,count
        
        if (count le 0) then break ; bail out
        
        fans_loadFiles, dataType,sPtr,lFileList,fileList,count,err=err,emsg=emsg 
        if (err ne 0) then begin
            printerror,err,emsg
            return
        endif

        ;; make bkgd view and subBkgd sensitive
        widget_control, (*sPtr).bViewID, sensitive=1
        widget_control, (*sPtr).subBkgdID, sensitive=1

        ;; reset status of multiple bkgd files processing
        (*sPtr).bkgdStatus = 0

        ;; if there are multiple files, sum or merge them if required
        if ((count gt 1) and ((*(*sPtr).mfoPtr)[1] ne 2)) then begin
            case (*(*sPtr).mfoPtr)[1] of
                0: fans_sumData, sPtr, dataType, err=err, emsg=emsg
                1: fans_apdData, sPtr, dataType, err=err, emsg=emsg
            endcase 
            if (err ne 0) then begin
                printerror,err,emsg
                return
            endif
        endif 

        ;; if the display option is 'Bkgd', then display the current
        ;; dataset in the bkgd list
        widget_control, (*sPtr).bViewID, get_uvalue=on
        if (on) then begin 

            ;; display current data
            fans_plotdata,sPtr,dataType

            ;; Reset the Energy Slider - it should be in sync with the
            ;; current data (ie being plotted).
            index = (*sPtr).bkgdIndex 
            fdStructPtr = (*(*sPtr).bDataPtr)[index]
            fans_syncSlider,(*sPtr).eSliderID,(*fdStructPtr).energyPtr, err=err, emsg=emsg
            if (err ne 0) then begin
                printerror,err,emsg
                return
            endif

            ;; make plot window and eslider sensitive 
;            widget_control, (*sPtr).winVisID, sensitive=1
            widget_control, (*sPtr).eSliderID, sensitive=1

        endif

        ;; switch on output calculation flag
        (*sPtr).doOutpCalc = 1

        ;; if the display option is 'Output', then update output plot
        widget_control, (*sPtr).oViewID, get_uvalue=on
        if (on) then begin
            ;; Calculate output data
            fans_outpCalc, sPtr, err=err, emsg=emsg
            if (err ne 0) then begin
                printerror,err,emsg
                return
            endif
            fans_plotData, sPtr, 'outp', err=err,emsg=emsg
        endif
        
        break
    end
    
    ;; choose Fast bkdg file(s)
    'fast': begin
        
        getFileList, sPtr,'Select Fast Background Files',lFileList,fileList,count
        
        if (count le 0) then break   ; bail out
        
        fans_loadFiles, dataType,sPtr,lFileList,fileList,count,err=err,emsg=emsg 
        if (err ne 0) then begin
            printerror,err,emsg
            return
        endif
        
        ;; make fast view sensitive
        widget_control, (*sPtr).fViewID, sensitive=1

        ;; reset status of multiple fast bkgd files processing
        (*sPtr).fastStatus = 0
        widget_control, (*sPtr).subFastID, sensitive=0

        ;; reset fast bkgd fit parameters to 0.0, 0.0, 0.0
        ;parstr = '0.00  0.00  0.00'
        ;widget_control, (*sPtr).parID, set_value=strtrim(parstr,2)

        ;; if there are multiple files, sum or merge them if required
        if ((count gt 1) and ((*(*sPtr).mfoPtr)[2] ne 2)) then begin
            case (*(*sPtr).mfoPtr)[2] of
                0: fans_sumData, sPtr, dataType, err=err, emsg=emsg
                1: fans_apdData, sPtr, dataType, err=err, emsg=emsg
            endcase 
            if (err ne 0) then begin
                printerror,err,emsg
                return
            endif
        endif 


        ;; if the display option is 'Fast', then display the
        ;; data. Note that, unlike for samp and bkgd, only one fast
        ;; data is possible. Multiple fast files will have been merged
        ;; above if they cover different energy ranges.
        widget_control, (*sPtr).fViewID, get_uvalue=on
        if (on) then $
          fans_plotdata,sPtr,dataType
        
        ;; fast bkgd fit not available
        (*sPtr).fitPresent = 0

        ;; switch on output calculation flag
        (*sPtr).doOutpCalc = 1

        break
    end

    ;; save output file using DAVE file format
    'sdave': begin

        fans_2Dave, sPtr,err=err,emsg=emsg
        if (err ne 0) then begin
            printerror,err,emsg
            return
        endif

        break
    end

    ;; save output file using 3 Col ascii file format
    'ascii': begin

        fans_2Ascii, sPtr,err=err,emsg=emsg
        if (err ne 0) then begin
            printerror,err,emsg
            return
        endif

        break
    end

    ;; save 2D (only individual detector cnts) output file using DAVE file format
    's2dave': begin

        fans_2DDave, sPtr,err=err,emsg=emsg
        if (err ne 0) then begin
            printerror,err,emsg
            return
        endif

        break
    end

    ;; exit program
    'exit': begin

        widget_control, event.top, /destroy

        break
    end
        
        
endswitch 

end
