; $Id: dcs_getstarted.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_getstarted_event,event
;************************************************************************************************
;
compile_opt strictarr
;
widget_control,event.top,get_uvalue=state
case event.id of
	state.done: begin
		if (!dcs_rootname eq '') then begin
			res=dialog_message("An ''output file prefix'' must be "+$
			"specified. For more information click the ''Help'' button.",/error)
		endif else begin
			widget_control,event.top,/destroy
			return
		endelse
	end
	state.canc: begin
		*(state.flagPtr)=1
		widget_control,event.top,set_uvalue=state
		widget_control,event.top,/destroy
		return
	end
	state.help: begin
		res=dialog_message(state.helptext,/information)
		return
	end
	state.browse:begin
		fileid=(*!dave_defaults).workdir+"*.log"
		files=findfile(fileid,count=nfiles)
		if (nfiles eq 0) then begin
			res=dialog_message("No previously defined file prefixes found in working directory")
		endif else begin
			for k=0,nfiles-1 do begin
				pos=strpos(files[k],"\",/reverse_search)
				len=strlen(files[k])
				files[k]=strmid(files[k],pos+1,len-pos-5)
			endfor
			widget_control,state.drop,set_value=files
			state.pfxes[0:nfiles-1]=files
		endelse
	end
	state.drop: begin
		index=event.index
		widget_control,state.pfx,set_value=state.pfxes[index]
		defsysv,'!dcs_rootname',state.pfxes[index]
	end
	state.raw: begin
		dave_defs=*!dave_defaults
		old_directory=dave_defs.datDir
		directory = DIALOG_PICKFILE(title = 'Select raw data directory',$
			/directory,path=old_directory)
		if (directory eq '' or directory eq ' ') then return
		dave_defs.datDir=directory
		widget_control,state.rawtext,$
			set_value="The raw data directory is "+directory+"   "
		pdd=!dave_defaults
		ptr_free,pdd
		pdd=ptr_new(dave_defs)
		if (not lmgr(/demo)) then save,filename=!defaults_file,pdd
		defsysv,'!dave_defaults',pdd
			data_directory=(*!dave_defaults).datDir
			if (strmid(data_directory,0,/reverse_offset) ne !delimiter) then $
				data_directory=data_directory+!delimiter
			defsysv,'!dcs_data_directory',data_directory,0
			cd,current=curDir
	end
;
state.work: begin
		dave_defs=*!dave_defaults
		old_directory=dave_defs.workDir
		directory = DIALOG_PICKFILE(title = 'Select working directory',$
			/directory,path=old_directory)
		if (directory eq '' or directory eq ' ') then return
		dave_defs.workDir=directory
		widget_control,state.wrktext,$
			set_value="The working directory is "+directory+"   "
		pdd=!dave_defaults
		ptr_free,pdd
		pdd=ptr_new(dave_defs)
		if (not lmgr(/demo)) then save,filename=!defaults_file,pdd
		defsysv,'!dave_defaults',pdd
			outp_directory=(*!dave_defaults).workDir
			if (strmid(outp_directory,0,/reverse_offset) ne !delimiter) then $
				outp_directory=outp_directory+!delimiter
			defsysv,'!dcs_outp_directory',outp_directory,0
		cd,current=curDir
	end
	state.pfx: begin
		widget_control,state.pfx,get_value=rootname
		defsysv,'!dcs_rootname',rootname[0]
		if (tag_names(event,/structure_name) eq 'WIDGET_TEXT_CH') then begin
			if (event.ch eq 10 and rootname[0] ne '') then begin
				widget_control,event.top,/destroy
				return
			endif
		endif
	end
	else:
endcase
widget_control,event.top,set_uvalue=state
end


;************************************************************************************************
pro dcs_getstarted,group_leader=group_leader,flag
;************************************************************************************************
;
compile_opt strictarr
;
helpstring=$
	"You may or may not need to define the input (''raw data'') "+$
	"and output (''working'') directories. "+$
	"To change the raw data directory click "+$
	"''Change raw data directory''. \\"+$
	"To change the working directory click "+$
	"''Change working directory''. \\ \\"+$
	"Unless it has been previously defined, you must "+$
	"also define the ''output file prefix'' for "+$
	"this data reduction session. "+$
	"A typical prefix might be something like ''dna'' or ''CuNO3''. "+$
	"All files created during this session, including intermediate "+$
	"and final data files, plus a log file and various utility files, "+$
	"will have this prefix. \\"+$
	"You may either edit the prefix box or click ''Browse'' to select an "+$
	"existing prefix from the working directory."
maxlen=80
dcs_superstring_wrap,helpstring,maxlen,helptext,err
tlb=widget_base(/col,title="Preliminaries",/modal,$
	group_leader=group_leader,xpad=25,ypad=20)
	rawstring="The raw data directory is "+(*!dave_defaults).datDir+"   "
	wrkstring="The working directory is "+(*!dave_defaults).workDir+"   "
	subbase=widget_base(tlb,/col)
		sb1=widget_base(subbase,/col,/frame)
			rawtext=widget_label(sb1,/dynamic_resize,value=rawstring)
			raw=widget_button(sb1,value='Change the raw data directory')
		void=widget_label(subbase,value=" ")
		sb2=widget_base(subbase,/col,/frame)
			wrktext=widget_label(sb2,/dynamic_resize,value=wrkstring)
			work=widget_button(sb2,value='Change the working directory')
		void=widget_label(subbase,value=" ")
		sb3=widget_base(subbase,/row,/frame)
			sb3a=widget_base(sb3,/col)
				pfxtext=widget_label(sb3a,/dynamic_resize,$
					value='Enter the output file prefix')
				pfx=widget_text(sb3a,value=!dcs_rootname,/editable,/all_events)
			sb3b=widget_base(sb3,/col)
				void=widget_label(sb3b,value=" ")
				browse=widget_button(sb3b,value="Browse")
			drop=widget_droplist(sb3)
			void=widget_label(subbase,value=" ")
	help=widget_button(tlb,value='Help')
	canc=widget_button(tlb,value='Return to main menu')
	done=widget_button(tlb,value='Proceed')
centertlb,tlb
widget_control,tlb,/realize
flagPtr=ptr_new(0)
state={raw:raw,$
	work:work,$
	rawtext:rawtext,$
	wrktext:wrktext,$
	pfx:pfx,$
	browse:browse,$
	drop:drop,$
	help:help,$
	helptext:helptext,$
	canc:canc,$
	done:done,$
	pfxes:strarr(5),$
	flagPtr:flagPtr}
widget_control,tlb,set_uvalue=state
xmanager,'dcs_getstarted',tlb
flag=*flagPtr
ptr_free,flagPtr
end
