;$Id: db_offset.pro,v 1.10 2005/02/08 17:01:16 azuah Exp $
;###############################################################################
;
; NAME:
;   DB_OFFSET
;
; PURPOSE:
;   Modal widget dialog that allows user to change the offset 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_OFF_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_OFF_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).oldOS[(*pState).fileIndex]),2)
id_new = widget_info(event.top,find_by_uname = 'NEW')
widget_control,id_new,set_value=(*pState).newOS[(*pState).fileIndex]
	;;strtrim(string((*pState).newOS[(*pState).fileIndex]),2)
end
;-------------------------------------------------------------------------------



;===============================================================================
; DB_OFF_Init
;
; PURPOSE:
;   Event handler. Retrieve current offset values from the dataset(s)
;   and store then in the state structure.
;
; PARAMETERS:
;   event [in] - the event structure to be handled
;
pro DB_OFF_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(OFFSET = os)
    (*pState).oldOS[i] = os
    (*pState).newOS[i] = os
endfor
end
;-------------------------------------------------------------------------------



;===============================================================================
; DB_OFF_ChangeFiles
;
; PURPOSE:
;   Event handler. Make new selection for datasets to be modified.
;
; PARAMETERS:
;   event [in] - the event structure to be handled
;
pro DB_OFF_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).newOS[(*pState).oldIndex] = thisVal
(*pState).fileIndex = event.index[0]
(*pState).oldIndex = (*pState).fileIndex
db_off_update,event
end
;-------------------------------------------------------------------------------



;===============================================================================
; DB_OFF_Events
;
; PURPOSE:
;   Event handler. Main event handler.
;
; PARAMETERS:
;   event [in] - the event structure to be handled
;
pro DB_OFF_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).newOS[(*pState).fileIndex] = myValue
end

'DATASELECTION': db_off_changeFiles,event

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

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

else:

endcase

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



;===============================================================================
; DB_Offset
;
; 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 offsets (1 for each dataset)
;
function DB_Offset, group_leader = group_leader, oContainer
; Pull out all of the "available" files
oall = oContainer->get(/all)
n_objects = oContainer->count()
for i = 0,n_objects-1 do begin
    ;void = oall[i]->get(dataref = o)
    o = oall[i]
    class = obj_class(o)
    
    if class ne 'DAVEDAT1D' 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='DATABROWSER: Offset'
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

cb = widget_base(tlb,/col,/align_center,/frame)
selections = ['select 2D Data',nameTags]
drop = widget_droplist(cb,value=nameTags,uname='DATASELECTION',/align_center)
current = cw_field(cb,title='Current offset',/row,value='',/string,uname='CURRENT', $
                   /noedit,xsize = 25)
new     = cw_field(cb,title='New offset    ',/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)
oldOS = replicate(1.0,nfiles)
newOS = oldOS

state = {cancel:0, $
         filenames:nameTags, $
         oldOS:oldOS, $
         newOS:newOS, $
         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_off_init,ev
db_off_update,ev

registerName = 'db_of'

xmanager,registerName,tlb,no_block=modal,event_handler='db_off_events'

db_sf_structure = {cancel:(*pState).cancel,offset:(*pState).newOS}

ptr_free,pState
return,db_sf_structure
end
