; $Id: fans_droplist.pro,v 1.6 2007/10/18 20:53:40 azuah Exp $
;###############################################################################
;
; NAME:
;   fans_droplist
;
; 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 3 droplist widgets that select
;   or change the current data.
;
; 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.
;
;###############################################################################



;===============================================================================
; updatePlotSlider
;
; PURPOSE:
;    Updates the plot and slider widgets with the current data array
;
; PARAMETERS:
;   sPtr [in] - pointer to state structure
;
;   dataType [in] - data type being displayed: 'samp'|'bkgd'|'fast'
;
;   dataPtr [in] - pointer to data to be plotted.
;
; KEYWORDS:
;   err [out] - err number, if an error occured
;
;   emsg [out] - explanatory error message, if an error occured
;
pro updatePlotSlider, sPtr,dataType,dataPtr,err=err,emsg=emsg
; update plot
fans_plotData,sPtr,dataType

; update energy slider
fans_syncSlider,(*sPtr).eSliderID,(*dataPtr).energyPtr,0,err=err,emsg=emsg
if (err ne 0) then begin
    if (!debug) then printerror,err,emsg
    return
endif

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



;===============================================================================
; fans_dropList
;
; PURPOSE:
;    Event handler. Selection has changed in one of the 3 droplist widgets.
;
; PARAMETERS:
;   event [in] - event dtructure
;
pro fans_dropList, 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_droplist: 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

; Even when there is only one element in a droplist widget, on windows
; (but not linux), an event is still generated. This is a problem when
; the list _is_'empty' which in this case will contain an item 
; 'no files yet'. Deal with this by ignoring events when the number of
; items < 1.
nosItems = widget_info(event.id,/droplist_number)
if (nosItems le 1) then return

widget_control, event.top, get_uvalue=sPtr

dataType = widget_info(event.id,/uname)

switch dataType of
    
    'samp': begin

        ;; save selected index - useful when not in 'separate' mode
        (*sPtr).sampSepIndex = event.index

        ;; nothing to do if files are not analysed seperately
        if ((*(*sPtr).mfoPtr)[0] ne 2) then break

        ;; make selected data the current data
        (*sPtr).sampIndex = event.index
        
        ;; reset output calculation flag
        (*sPtr).doOutpCalc = 1

        ;; if the display option is 'Sample', then update plot and slider
        widget_control, (*sPtr).sViewID, get_uvalue=on
        if (on) then $
          updatePlotSlider, sPtr,dataType,(*(*sPtr).sDataPtr)[event.index],err=err,emsg=emsg

        ;; 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


    'bkgd': begin
        ;; save selected index - useful when not in 'separate' mode
        (*sPtr).bkgdSepIndex = event.index

        ;; nothing to do if files are not analysed seperately
        if ((*(*sPtr).mfoPtr)[1] ne 2) then break

        ;; make selected data the current data
        (*sPtr).bkgdIndex = event.index

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

        ;; if the display option is 'Sample', then update plot
        widget_control, (*sPtr).bViewID, get_uvalue=on
        if (on) then $
          updatePlotSlider, sPtr,dataType,(*(*sPtr).bDataPtr)[event.index],err=err,emsg=emsg

        ;; 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

    'fast': begin

        ;; save selected index - useful when not in 'separate' mode
        (*sPtr).fastSepIndex = event.index

        ;; nothing to do if files are not analysed seperately
        if ((*(*sPtr).mfoPtr)[2] ne 2) then break

        ;; make selected data the current data
        (*sPtr).fastIndex = event.index

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

        ;; if the display option is 'Sample', then update plot
        widget_control, (*sPtr).fViewID, get_uvalue=on
        if (on) then $
          fans_plotData, sPtr, 'fast', err=err,emsg=emsg
        ;;updatePlotSlider, sPtr,dataType,(*(*sPtr).fDataPtr)[event.index],err=err,emsg=emsg

        break
    end


endswitch


end

