; $Id: fcs_reduce_prefs.pro,v 1.3 2005/02/08 17:20:27 azuah Exp $
;###############################################################################
; 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.
;
;###############################################################################
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_prefs_cleanup,tlb
widget_control,tlb,get_uvalue = pState
ptr_free,pState
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
function fcs_reduce_prefs_get_values,event
widget_control,event.top,get_uvalue = pState

elch_id = widget_info(event.top,find_by_uname = 'ELCHAN')
widget_control,elch_id,get_value = val
(*pState).prefs.ELCH = fix(val[0])

bglo_id = widget_info(event.top,find_by_uname = 'BGLO')
widget_control,bglo_id,get_value = val
(*pState).prefs.BGLO = fix(val[0])

bghi_id = widget_info(event.top,find_by_uname = 'BGHI')
widget_control,bghi_id,get_value = val
(*pState).prefs.BGHI = fix(val[0])

t_id = widget_info(event.top,find_by_uname = 'T')
widget_control,t_id,get_value = val
(*pState).prefs.T = float(val[0])

sigma_id = widget_info(event.top,find_by_uname = 'SIGMA')
widget_control,sigma_id,get_value = val
(*pState).prefs.SIGMA = float(val[0])

elo_id = widget_info(event.top,find_by_uname = 'ELO')
widget_control,elo_id,get_value = val
(*pState).prefs.ELO = float(val[0])

ehi_id = widget_info(event.top,find_by_uname = 'EHI')
widget_control,ehi_id,get_value = val
(*pState).prefs.EHI = float(val[0])

ne_id = widget_info(event.top,find_by_uname = 'NEBINS')
widget_control,ne_id,get_value = val
(*pState).prefs.NEBINS = fix(val[0])

qlo_id = widget_info(event.top,find_by_uname = 'QLO')
widget_control,qlo_id,get_value = val
(*pState).prefs.QLO = float(val[0])

qhi_id = widget_info(event.top,find_by_uname = 'QHI')
widget_control,qhi_id,get_value = val
(*pState).prefs.QHI = float(val[0])

nq_id = widget_info(event.top,find_by_uname = 'NQBINS')
widget_control,nq_id,get_value = val
(*pState).prefs.NQBINS = fix(val[0])

daveout_id = widget_info(event.top,find_by_uname = 'DAVE_OUT')
widget_control,daveout_id,get_value = val
(*pState).prefs.DAVE_OUT = val

return,1
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_prefs_restore_defaults,event
widget_control,event.top,get_uvalue = pState

elch_id = widget_info(event.top,find_by_uname = 'ELCHAN')
widget_control,elch_id,set_value = (*pState).defaults.ELCH

bglo_id = widget_info(event.top,find_by_uname = 'BGLO')
widget_control,bglo_id,set_value = (*pState).defaults.BGLO

bghi_id = widget_info(event.top,find_by_uname = 'BGHI')
widget_control,bghi_id,set_value = (*pState).defaults.BGHI

t_id = widget_info(event.top,find_by_uname = 'T')
widget_control,t_id,set_value = (*pState).defaults.T

sigma_id = widget_info(event.top,find_by_uname = 'SIGMA')
widget_control,sigma_id,set_value = (*pState).defaults.SIGMA

elo_id = widget_info(event.top,find_by_uname = 'ELO')
widget_control,elo_id,set_value = (*pState).defaults.ELO

ehi_id = widget_info(event.top,find_by_uname = 'EHI')
widget_control,ehi_id,set_value = (*pState).defaults.EHI

ne_id = widget_info(event.top,find_by_uname = 'NEBINS')
widget_control,ne_id,set_value = (*pState).defaults.NEBINS

qlo_id = widget_info(event.top,find_by_uname = 'QLO')
widget_control,qlo_id,set_value = (*pState).defaults.QLO

qhi_id = widget_info(event.top,find_by_uname = 'QHI')
widget_control,qhi_id,set_value = (*pState).defaults.QHI

nq_id = widget_info(event.top,find_by_uname = 'NQBINS')
widget_control,nq_id,set_value = (*pState).defaults.NQBINS

daveout_id = widget_info(event.top,find_by_uname = 'DAVE_OUT')
widget_control,daveout_id,set_value = (*pState).defaults.DAVE_OUT

end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_prefs_sendInfo,event
widget_control,event.top,get_uvalue = pState

dummy = fcs_reduce_prefs_get_values(event)
s = size((*pState).notifyIDs)
if s[0] eq 1 then count = 0 else count = s[2]-1
for j = 0,count do begin
  FCSInfo = {FCSEvent,$
                        ID:(*pState).notifyIDs[0,j],$
                        Top:(*pState).notifyIDs[1,j],$
                        Handler:0l,$
                        prefs:(*pState).prefs}
  if widget_info((*pState).notifyIDs[0,j],/valid_id) then begin $
    widget_control,(*pState).notifyIDs[0,j],send_event = FCSInfo
  endif
endfor

end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_prefs_temperature,event
widget_control,event.top,get_uvalue = pState
thisEvent = tag_names(event,/structure_name)
case thisEvent of
'TEVENT': $
  begin
	(*pState).prefs.T = event.temperature
	uname = 'T'
	id = widget_info(event.top,find_by_uname = uname)
	widget_control,id,set_value = strtrim(string(event.temperature),2)
  end
else:
endcase
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_prefs_event,event
widget_control,event.top,get_uvalue = pState
uname = widget_info(event.id,/uname)
case uname of
'ELCH_APP':	$
	begin
		(*pState).prefs.ELCH_APP = event.select
	end
'E_APP':	$
	begin
		(*pState).prefs.E_APP = event.select
	end
'Q_APP':	$
	begin
		(*pState).prefs.Q_APP = event.select
	end
'SYMM_APP':	$
	begin
		(*pState).prefs.SYMM_APP = event.select
	end
'VAN_NORM':	$
	begin
		(*pState).prefs.VAN_NORM = event.select
	end
'DEFAULTS':	$
	begin
		fcs_reduce_prefs_restore_defaults,event
	end
'DISMISS':	$
	begin
		widget_control,event.top,/destroy
	end
'TEMPERATURE_UPDATE':	$
	begin
		fcs_reduce_prefs_temperature,event
	end
'APPLY':	$
	begin
		if (*pState).group_leader[0] eq 0L then return
		fcs_reduce_prefs_sendInfo,event
	end
else:
endcase

end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_prefs,	group_leader = group_leader,	$
						notifyIDs = notifyIDs,			$
						prefs = prefs,					$
						err = err,						$
						outputID = outputID,			$
						outputTOP = outputTOP
; Widget definition module
registerName = 'fcs_reduce_prefs'
err = 0
if xregistered(registerName) then begin
  err = 1
  return
endif
if n_elements(group_leader) eq 0 then begin
  group_leader = 0L
  notifyIDS = [0L,0L]
endif

; Specify some default values
defaults =	{	MYPREFS,			$
				ELCH:405,			$
  				ELCH_APP:0,			$
  				BGLO:950,			$
  				BGHI:960,			$
  				T:300.0,			$
  				SIGMA:1.0,			$
  				ELO:-20.0,			$
  				EHI:2.0,			$
  				NEBINS:100,			$
  				E_APP:0,			$
  				QLO:0.25,			$
  				QHI:2.5,			$
  				NQBINS:25,			$
  				Q_APP:0,			$
  				SYMM_APP:0,			$
  				VAN_NORM:0,			$
				DAVE_OUT:intarr(6)	$
  			}
; If the user has not supplied any preferences then define the
; outgoing preferences to be the defaults
if n_elements(prefs) eq 0 then begin
  prefs = defaults
endif

; Set up the widget layout
tsize = 5
tlb = widget_base(group_leader = group_leader,/col, $
	title = 'Time-of-flight reduction preferences', $
	/base_align_left,/tlb_frame_attr)

outputTOP = tlb

rowbase1 = widget_base(tlb,/row,uname = 'TEMPERATURE_UPDATE')
outputID = rowBase1

void = cw_field(rowbase1,value = strtrim(string(prefs.ELCH),2), $
	uname = 'ELCHAN',title = 'Elastic Channel',/row,xsize = tsize,/integer)
exBase1 = widget_base(rowbase1,/nonexclusive)
void = widget_button(exBase1,value = 'Apply',uname = 'ELCH_APP')
rowBase2 = widget_base(tlb,/row)
void = widget_label(rowBase2,value = 'Background range:')
void = cw_field(rowbase2,value = strtrim(string(prefs.BGLO),2), $
	uname = 'BGLO',title = 'Low',/row,xsize = tsize,/integer)
void = cw_field(rowbase2,value = strtrim(string(prefs.BGHI),2), $
	uname = 'BGHI',title = 'High',/row,xsize = tsize,/integer)

rowBase3 = widget_base(tlb,/row)
void = cw_field(rowbase3,value = strtrim(string(prefs.T),2), $
	uname = 'T',title = 'Temperature (K)',/row,xsize = tsize,/floating)
void = cw_field(rowbase3,value = strtrim(string(prefs.SIGMA),2), $
	uname = 'SIGMA',title = 'Sigma',/row,xsize = tsize,/floating)

colBase4 = widget_base(tlb,/col,/frame)
void = widget_label(colBase4,value = 'Constant E binning')
rowBase4 = widget_base(colBase4,/row)
void = cw_field(rowbase4,value = strtrim(string(prefs.ELO),2), $
	uname = 'ELO',title = 'Low',/row,xsize = tsize,/floating)
void = cw_field(rowbase4,value = strtrim(string(prefs.EHI),2), $
	uname = 'EHI',title = 'High',/row,xsize = tsize,/floating)
void = cw_field(rowbase4,value = strtrim(string(prefs.NEBINS),2), $
	uname = 'NEBINS',title = '# E bins',/row,xsize = tsize,/integer)
exBase4 = widget_base(rowbase4,/nonexclusive)
void = widget_button(exBase4,value = 'Apply',uname = 'E_APP')

colBase5 = widget_base(tlb,/col,/frame)
void = widget_label(colBase5,value = 'Constant Q binning')
rowBase5 = widget_base(colBase5,/row)
void = cw_field(rowbase5,value = strtrim(string(prefs.QLO),2), $
	uname = 'QLO',title = 'Low',/row,xsize = tsize,/floating)
void = cw_field(rowbase5,value = strtrim(string(prefs.QHI),2), $
	uname = 'QHI',title = 'High',/row,xsize = tsize,/floating)
void = cw_field(rowbase5,value = strtrim(string(prefs.NQBINS),2), $
	uname = 'NQBINS',title = '# Q bins',/row,xsize = tsize,/integer)
exBase5 = widget_base(rowbase5,/nonexclusive)
void = widget_button(exBase5,value = 'Apply',uname = 'Q_APP')

rowBase6 = widget_base(tlb,/row)
exBase6 = widget_base(rowbase6,/nonexclusive)
exBase6a = widget_base(rowbase6,/nonexclusive)
void = widget_button(exBase6,value = 'Symmetrize S(Q,E)',uname = 'SYMM_APP')
void = widget_button(exBase6a,value = 'Vanadium Norm.',uname = 'VAN_NORM')

output = ['I(phi,t)','I(phi,E)','S(phi,E)','S(Q,E)','G(E)','Raw']
void = cw_bgroup(tlb, output, /row, /nonexclusive,$
         label_top='Save as DAVE (select as many as you like)',/no_release,$
         frame=1,set_value=prefs.DAVE_OUT,/return_index, $
         uname = 'DAVE_OUT')

rowBase8 = widget_base(tlb,/row)
void = widget_button(rowBase8,value = 'Defaults',uname = 'DEFAULTS')
void = widget_button(rowBase8,value = 'Apply',uname = 'APPLY')
void = widget_button(rowBase8,value = 'Dismiss',uname = 'DISMISS')

widget_control,tlb,/realize
state =		{	prefs:prefs,			$
				defaults:defaults,		$
				group_leader:group_leader, $
				notifyIDs:notifyIDs			}
pState = ptr_new(state,/no_copy)
widget_control,tlb,set_uvalue = pState

xmanager,registerName,tlb,/no_block,cleanup = 'fcs_reduce_prefs_cleanup'
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
