; $Id: dcs_reduction_strategy.pro,v 1.12 2005/02/14 17:50:14 azuah Exp $
; Written by J.R.D. Copley.
;************************************************************************************************
;###############################################################################
;
; 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.
;
;###############################################################################
pro dcs_reduction_strategy_handler,event
;************************************************************************************************
;
compile_opt strictarr
;
widget_control,event.top,get_uvalue=pstate,/no_copy
ptr=(*pstate).ptr; pointer to istrategy
base=(*pstate).base
restore=(*pstate).restore
restoredef=(*pstate).restoredef
clear=(*pstate).clear
done=(*pstate).done
done2=(*pstate).done2
save=(*pstate).save
savedef=(*pstate).savedef
buttons=(*pstate).buttons
modifiable=(*pstate).modifiable
default_file=(*pstate).default_file
cancel=(*pstate).cancel
n_skipped=(*pstate).n_skipped
;
nbuts=n_elements(buttons)
;
evid=event.id
;
;	Respond to MAIN MENU button.
if (evid eq cancel) then begin
	(*ptr)[0]=999
	widget_control,event.top,set_uvalue=pstate,/no_copy
	widget_control,event.top,/destroy
	return
end
;
;	Respond to SAVE button.
if (evid eq save) then begin
	istrategy=*ptr
	repeat begin
		fullfilename=dialog_pickfile(filter="*.drs",/fix_filter,$
			path=!dcs_outp_directory,$
			file=!dcs_rootname+".drs",get_path=file_path,$
			title="Define the strategy file to be saved.")
		if (fullfilename eq "") then begin
			widget_control,event.top,set_uvalue=pstate,/no_copy
			return
		endif
		compok=strcmp(file_path,!dcs_outp_directory,/fold_case)
		if (not compok) then res=dialog_message("You may not change the directory.")
	endrep until compok
	if (strlowcase(strmid(fullfilename,3,/reverse)) ne ".drs") then $
		fullfilename=fullfilename+".drs"
	save,filename=fullfilename,istrategy
endif
;
;	Respond to RESTORE button.
if (evid eq restore) then begin
	file=!dcs_outp_directory+!dcs_rootname+".drs"
	if (not file_test(file)) then file=!dcs_default_root+".drs" else $
		file=!dcs_rootname+".drs"
	fullfilename=dialog_pickfile(filter="*.drs",file=file,/must_exist,/read,$
		/fix_filter,path=!dcs_outp_directory,get_path=file_path,$
		title="Define the strategy file to be restored.")
	if (fullfilename eq "") then begin
		widget_control,event.top,set_uvalue=pstate,/no_copy
		return
	endif
	if (strlowcase(strmid(fullfilename,3,/reverse)) ne ".drs") then $
		fullfilename=fullfilename+".drs"
	restore,filename=fullfilename
	if (n_elements(istrategy) lt nbuts) then $
		istrategy=[istrategy,intarr(nbuts-n_elements(istrategy))]
	*ptr=istrategy
	for ibut=0,nbuts-1 do $
		widget_control,buttons[ibut],set_button=istrategy[ibut]
endif
;
;	Respond to SAVE DEFAULT button.
if (evid eq savedef) then begin
	istrategy=*ptr
	save,filename=default_file,istrategy
endif
;
;	Respond to RESTORE DEFAULT button.
if (evid eq restoredef) then begin
	restore,filename=default_file
	*ptr=istrategy
	for ibut=0,nbuts-1 do $
		widget_control,buttons[ibut],set_button=istrategy[ibut]
endif
;
;	Respond to CLEAR button.
if (evid eq clear) then begin
	for ibut=0,nbuts-1 do begin
		widget_control,buttons[ibut],set_button=0
		(*ptr)[ibut]=0
	endfor
endif
;
; Respond to individual strategy button clicks.
for ibut=0,nbuts-1 do begin
	if (evid eq buttons[ibut]) then begin
		if (modifiable[ibut] lt 0) then begin
			(*ptr)[ibut]=event.select
		endif else begin
			widget_control,buttons[ibut],set_button=modifiable[ibut]
		endelse
	endif
endfor
;
; Respond to "Read summed data" strategy button click.
if (evid eq buttons[0]) then begin
	if (event.select eq 1) then begin
		for ibut=2,n_skipped+1 do widget_control,buttons[ibut],sensitive=0
	endif else begin
		for ibut=2,n_skipped+1 do widget_control,buttons[ibut],sensitive=1
	endelse
endif
;
; Respond to PROCEED buttons.
if (evid eq done or evid eq done2) then begin
	widget_control,event.top,/destroy
	ptr_free,pstate
	return
endif
;
(*pstate).ptr=ptr
;
widget_control,event.top,set_uvalue=pstate,/no_copy
end
;
;
;************************************************************************************************
pro dcs_reduction_strategy,dcs_base,filename=stratagems_filename,strategy
;************************************************************************************************
; This procedure is used to define the strategy for the data reduction session.
;
compile_opt strictarr
;
;
; Define font.
;dcs_sysdep,fonts=fonts
;
; Read in information about stratagems.
; The variable "button_tags" is a corresponding array containing the texts of the widgets.
; An element of the array "modifiable" is -1 if it can be modified by the user.
; If it is 0 the strategy is not pursued and cannot be modified.
;	If it is 1 the strategy is pursued and cannot be modified.
dcs_reduction_stratagems,filename=stratagems_filename,strategy,button_tags,$
	modifiable,nbuts,n_skipped
;
; buttons is an array containing the IDs of the widgets that define the reduction strategy.
;
buttons=intarr(nbuts)
;
; Add the string "(CANNOT MODIFY) " to stratagems that cannot be modified.
for i=0,nbuts-1 do begin
	if (modifiable[i] ge 0) then button_tags[i]="(CANNOT MODIFY) "+button_tags[i]
endfor
;
; Construct the widget.
base=widget_base(title="Strategy",/col,group_leader=dcs_base,/modal)
	topbuts=widget_base(base,/row)
		cancel=widget_button(topbuts,value="Main Menu")
		done=widget_button(topbuts,value="PROCEED");,font=fonts[0])
	medbuts=widget_base(base,/row)
		restoredef=widget_button(medbuts,value="Restore default settings")
		restore=widget_button(medbuts,value="Restore settings")
		clear=widget_button(medbuts,value="CLEAR");,font=fonts[0])
	remainder=widget_base(base,/col,/frame,/nonexclusive)
		for i=0,nbuts-1 do buttons[i]=widget_button(remainder,value=button_tags[i])
	botbuts=widget_base(base,/row)
		savedef=0;;;;;;;widget_button(botbuts,value="Save as default settings")
		save=widget_button(botbuts,value="Save settings")
		done2=widget_button(botbuts,value="PROCEED");,font=fonts[0])
;
;
; Define default and cancel buttons.
widget_control,base,default_button=done
widget_control,base,cancel_button=cancel
;
istrategy=intarr(100)
ptr=ptr_new(istrategy)
;
;default_file=file_which("dcs_default_strategy.drs",/include_current_dir)
default_file=!DAVE_AUXILIARY_DIR+'dcs_default_strategy.drs'
;
restore,filename=default_file
if (n_elements(istrategy) lt nbuts) then $
	istrategy=[istrategy,intarr(nbuts-n_elements(istrategy))]
;
for ibut=0,nbuts-1 do begin
	if (modifiable[ibut] ge 0) then istrategy[ibut]=modifiable[ibut]
	widget_control,buttons[ibut],set_button=istrategy[ibut]
endfor
;
for i=2,n_skipped+1 do widget_control,buttons[i],sensitive=1-istrategy[0]
;
*ptr=istrategy
;
; Realize the widget.
dave_position_tlb,base
widget_control,base,/realize
;
state={ptr:ptr,base:base,restore:restore,restoredef:restoredef,$
	clear:clear,done:done,done2:done2,save:save,savedef:savedef,$
	buttons:buttons,modifiable:modifiable,cancel:cancel,$
	default_file:default_file,n_skipped:n_skipped}
;
pstate=ptr_new(state)
widget_control,base,set_uvalue=pstate,/no_copy
xmanager,'dcs_reduction_strategy',base,event_handler='dcs_reduction_strategy_handler'
;
istrategy=*ptr
ptr_free,ptr
;
for ibut=0,nbuts-1 do strategy.(ibut)=istrategy[ibut]
;
end
