;$Id: db_scalefactor.pro,v 1.7 2006/08/29 20:22:41 azuah Exp $
;###############################################################################
;
; NAME:
;   DB_ScaleFactor
;
; PURPOSE:
;   Modal widget dialog that allows user to change the multiplicative
;   scalefactor for a DAVE data set.
;
; CATEGORY:
;   Data Browser
;
; AUTHOR:
;   Robert M. Dimeo, Ph.D.
;   NIST Center for Neutron Research
;   100 Bureau Drive
;   Gaithersburg, MD 20899
;   Phone: (301) 975-8135
;   E-mail: robert.dimeo@nist.gov
;   http://www.ncnr.nist.gov/staff/dimeo
;   Dec, 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 or if the code in this file is
;  included in another product.
;
;###############################################################################



;===============================================================================
; DB_SF_Update
;
; PURPOSE:
;   Event handler. Update fields in main dialog with stored quantities
;   from state structure.
;
; PARAMETERS:
;   event [in] - the event structure to be handled
;
pro DB_SF_Update,event
widget_control,event.top,get_uvalue = pState
id_old = widget_info(event.top,find_by_uname = 'CURRENT')
widget_control,id_old,set_value= $
	strtrim(string((*pState).oldSF[(*pState).fileIndex]),2)
id_new = widget_info(event.top,find_by_uname = 'NEW')
widget_control,id_new,set_value=(*pState).newSF[(*pState).fileIndex]
               ;;strtrim(string((*pState).newSF[(*pState).fileIndex]),2)
end
;-------------------------------------------------------------------------------



;===============================================================================
; DB_SF_Init
;
; PURPOSE:
;   Event handler. Retrieve current scalefactor values from the dataset(s)
;   and store then in the state structure.
;
; PARAMETERS:
;   event [in] - the event structure to be handled
;
pro DB_SF_Init,event
widget_control,event.top,get_uvalue = pState
oContainer = (*pState).oContainer
oall = oContainer->get(/all)
nFiles = oContainer->count()
for i = 0,nFiles-1 do begin
    ;;void = oall[i]->get(dataref = o)
    o = oall[i]
    r = o->get(SCALEFACTOR = sf)
    (*pState).oldSF[i] = sf
    (*pState).newSF[i] = sf
endfor
end
;-------------------------------------------------------------------------------



;===============================================================================
; DB_SF_ChangeFiles
;
; PURPOSE:
;   Event handler. Make new selection for datasets to be modified.
;
; PARAMETERS:
;   event [in] - the event structure to be handled
;
pro DB_SF_ChangeFiles,event
widget_control,event.top,get_uvalue = pState
id_new = widget_info(event.top,find_by_uname = 'NEW')
widget_control,id_new,get_value = thisVal
thisVal = float(thisVal[0])
(*pState).newSF[(*pState).oldIndex] = thisVal
(*pState).fileIndex = event.index[0]
(*pState).oldIndex = (*pState).fileIndex
db_sf_update,event
end
pro DB_SF_Events,event
widget_control,event.top,get_uvalue = pState
uname = widget_info(event.id,/uname)

case uname of
'NEW': begin
    widget_control,event.id,get_value = myValue
    ;myValue = float(myValue[0])
    (*pState).newSF[(*pState).fileIndex] = myValue
end

'DATASELECTION': db_sf_changeFiles,event

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

'CANCEL': begin
    (*pState).cancel = 1
    widget_control,event.top,/destroy
end

else:

endcase

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



;===============================================================================
; DB_ScaleFactor
;
; PURPOSE:
;   Entry point and dialog builder for application.
;
; PARAMETERS:
;   oContainer [in|out;req] - A container for the DAVEGR2D or DAVEGR1D
;                             objects that are to be modified.
;
; KEYWORDS:
;   group_leader [in] - the ID of the parent widget from which this
;                       dialog was launched.
;
; RETURN VALUE:
;   A structure containing the following two fields:
;   CANCEL: 0 if false, 1 if true(ie cancel button used)
;   OFFSET: array of scalefactors (1 for each dataset)
;
function DB_ScaleFactor, group_leader=group_leader, oContainer
; Pull out all of the "available" files
oall = oContainer->get(/all)
n_objects = oContainer->count()
validClasses = ['DAVEDAT1D','DAVEDAT2D']
for i = 0,n_objects-1 do begin
    o = oall[i]
    class = obj_class(o)
    res = where(validClasses eq class,okay)
    if (not okay) then return,{cancel:1}
    r = o->get(nameTag = nameTag)
    if i eq 0 then begin
        nameTags = nameTag
    endif else begin
        nameTags = [nameTags,nameTag]
    endelse
endfor

; Widget definition module
title='DATADROWSER: Scalefactor'
if n_elements(group_leader) eq 0 then begin
	group_leader = 0L
	modal = 0
        tlb = widget_base(/col, title=title)
endif else begin
	modal = 1
        tlb = widget_base(group_leader=group_leader,/col,title=title,modal=modal)
endelse

;selections = ['select 2D Data',filenames]
cb = widget_base(tlb,/col,/align_center,/frame)
drop = widget_droplist(cb,value=nameTags,uname='DATASELECTION',/align_center)
void = widget_label(cb,value= 'After editing text field, you must use')
void = widget_label(cb,value= 'carriage return to keep modified value!')

current = cw_field(cb,title='Current scalefactor',/row,value='',/string,uname='CURRENT', $
                   /noedit,xsize=25)
new     = cw_field(cb,title='New scalefactor    ',/row,value='',/float,uname='NEW', $
                   /return_events,xsize=25)
rowBase = widget_base(tlb,/row,/align_center)
acptTip = 'Save changes and exit'
cancTip = 'Discard changes and exit'
void = widget_button(rowBase,value='Accept',uname='ACCEPT',tooltip=acptTip)
void = widget_button(rowBase,value='Cancel',uname='CANCEL',tooltip=cancTip)

widget_control,tlb,/realize
nfiles = n_elements(nameTags)
oldSf = replicate(1.0,nfiles)
newSf = oldSf

state = {cancel:0, $
         filenames:nameTags, $
         oldSf:oldSf, $
         newSf:newSf, $
         fileIndex:0, $
         oldIndex:0, $
         oContainer:oContainer $
        }

pState = ptr_new(state,/no_copy)
widget_control,tlb,set_uvalue = pState

ev = {peEvent,id:void,top:tlb,handler:0L}
db_sf_init,ev
db_sf_update,ev

registerName = 'db_sf'
xmanager,registerName,tlb,no_block=modal,event_handler='db_sf_events'

db_sf_structure = {cancel:(*pState).cancel,scaleFactor:(*pState).newSf}

ptr_free,pState
return,db_sf_structure
end
