; $Id: dcs_define_conditions.pro,v 1.3 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_define_conditions_handler,event
;************************************************************************************************
;
compile_opt strictarr
;
widget_control,event.top,get_uvalue=state
table=state.table
done=state.done
clear=state.clear
ndefined=state.ndefined
ptr=state.ptr
id=event.id
;
widget_control,table,get_value=conditions
if (id eq table) then begin
	index=where(conditions eq "",count)
	if (count gt 0) then widget_control,table,edit_cell=[index[0],0]
	index=where(conditions ne "",ncondef)
	case ncondef of
		0: warning="No conditions have been defined."
		1: warning=strcompress(ncondef)+" condition has been defined."
		else: warning=strcompress(ncondef)+" conditions have been defined."
	endcase
	widget_control,ndefined,set_value=warning
endif
;
if (id eq clear) then begin
	nconditions=n_elements(conditions)
	conditions=strarr(nconditions)
	widget_control,table,set_value=conditions
	*state.ptr=conditions
	warning="No conditions have been defined."
	widget_control,ndefined,set_value=warning
	widget_control,event.top,set_uvalue=state
endif
;
if (id eq done) then begin
	widget_control,table,get_value=conditions
	*state.ptr=conditions
	widget_control,event.top,/destroy
endif
;
end


;************************************************************************************************
pro dcs_define_conditions,dcs_base,nfiles,conditions,nochange
;************************************************************************************************
;
compile_opt strictarr
;
column_widths=[500]
;
dcs_sysdep,fonts=fonts
;
inconditions=conditions
;
dummy=where(conditions ne "",ncondef)
case ncondef of
	1: warning=strcompress(ncondef)+" condition has been defined."
	else: warning=strcompress(ncondef)+" conditions have been defined."
endcase
;
base=widget_base(title='Define the condition(s) to be associated with data files.',$
	group_leader=dcs_base,/modal,/col)
	lab1=widget_label(base,/align_left,value='Double-click on a cell to edit it. '+$
		'Enter a sample condition description and then hit RETURN to save it.')
	lab2=widget_label(base,/align_left,value='Hit the Done button when you''re done.')
	lab3=widget_label(base,/align_left,value='If no conditions are defined it will be assumed '+$
		'that all sample runs can be added together.')
	table=widget_table(base,/editable,/sensitive,column_labels=["Condition"],$
		row_labels=strcompress(indgen(nfiles)+1),value=transpose(conditions),column_widths=column_widths)
	buttons=widget_base(base,/row)
		done=widget_button(buttons,value='Done')
		clear=widget_button(buttons,value='Clear')
		ndefined=widget_label(buttons,/dynamic_resize,/frame,/align_right,value=warning,font=fonts[0])
;
dave_position_tlb,base
widget_control,base,/realize
;
widget_control,table,edit_cell=[0,0]
;
ptr=ptr_new(conditions)
state={table:table,done:done,clear:clear,ndefined:ndefined,ptr:ptr}
widget_control,base,set_uvalue=state
;
xmanager,'dcs_define_conditions',base,event_handler='dcs_define_conditions_handler'
;
conditions=*ptr
ptr_free,ptr
index=where(conditions ne "",ncon)
;
dummy=where(inconditions ne conditions,ndiffs)
nochange=ndiffs eq 0
;
; This test returns true if no conditions have been defined, otherwise false.
if (ncon eq 0) then conditions=strarr(1) else conditions=conditions[index]
;
end