; $Id: dcs_choose_conditions.pro,v 1.5 2005/02/14 17:50:13 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_choose_conditions_handler,event
;************************************************************************************************
;
compile_opt strictarr
;
widget_control,event.top,get_uvalue=pstate,/no_copy
bs=(*pstate).bs
okbut=(*pstate).okbut
wlbl=(*pstate).wlbl
ptr=(*pstate).ptr
assignments=*ptr
;
nf=n_elements(assignments)
etag=event.id
;
if (etag eq okbut) then begin
	listing=where(assignments eq 0)
	unset=n_elements(listing)
	checksum=fix(total(listing))
	if (checksum ne -1) then begin
		case unset of
			1: warning='  NB: 1 row does not have a button set.  '
			else: warning='  NB:'+strcompress(unset)+' rows do not have a button set.  '
		endcase
		widget_control,wlbl,set_value=warning
		widget_control,event.top,set_uvalue=pstate,/no_copy
	endif else widget_control,event.top,/destroy
	return
endif
;
eval=event.value
etag=where(etag eq bs)
assignments[etag]=eval+1
*(*pstate).ptr=assignments
widget_control,event.top,set_uvalue=pstate,/no_copy
;
end


;************************************************************************************************
pro dcs_choose_conditions,dcs_base,conditions,fnames,assignments
;************************************************************************************************
;
compile_opt strictarr
;
nf=n_elements(fnames)
;
;dcs_sysdep,fonts=fonts
;
ncon=n_elements(conditions);
;
ipass=0
define_base:
ipass=ipass+1
base=widget_base(title='Assign a condition to each data file.',$
	group_leader=dcs_base,/modal,/col)
;	If this is the first attempt to define the widget, allow it to size itself.
	if (ipass eq 1) then subbase=widget_base(base,/col)
; If this is the second attempt, having determined that the widget was too big,
;		size the widget and give it a scroll bar.
	if (ipass gt 1) then $
		subbase=widget_base(base,/col,/scroll,y_scroll_size=!screensize[1]*0.75)
;
	bg=intarr(nf)
	bs=intarr(nf)
	lb=intarr(nf)
	for i=0,nf-1 do begin
		bg[i]=widget_base(subbase,/row)
		bs[i]=cw_bgroup(bg[i],conditions,/row,/exclusive)
		lb[i]=widget_label(bg[i],value=fnames[i])
	endfor
	choices=widget_base(subbase,/row)
	okbut=widget_button(choices,value="OK")
	wlbl=widget_label(choices,/dynamic_resize,/align_right,value="")
;
;	check on the size of the widget. If it's too big go back and redefine it.
	geom=widget_info(base,/geometry)
	if (ipass eq 1) then $
		if (geom.scr_ysize+2*geom.margin gt !screensize[1]*0.8) then $
		goto, define_base
;
dave_position_tlb,base
widget_control,base,/realize
;
for i=0,nf-1 do begin
	if (assignments[i] gt 0) then $
	widget_control,long(bs[i]),set_value=assignments[i]-1
endfor
;
ptr=ptr_new(assignments)
;
state={bs:bs,okbut:okbut,wlbl:wlbl,ptr:ptr}
widget_control,base,set_uvalue=ptr_new(state),/no_copy
;
xmanager,'dcs_choose_conditions',base,event_handler='dcs_choose_conditions_handler'
;
assignments=*ptr
ptr_free,ptr
;
end
