; $Id: dcs_choose_files.pro,v 1.7 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_files,dcs_base,message_str,maxtypes,maxfiles,files_selected,nfiles_selected,$
	type_of_file,skipdefaultchoices=skipdefaultchoices
;************************************************************************************************
; Choose files to process.
;
compile_opt strictarr
;
decision=0 ; decision <= 0 implies that we're not done yet.
;
files_selected=strarr(maxtypes,maxfiles)
nfiles_selected=intarr(maxtypes)
;
while (decision le 0) do begin
;
	if (decision eq 0) then $
		dcs_multiplechoice,dcs_base,method,"Selecting_files",message_str,60,$
			skipdefaultchoices=skipdefaultchoices else method=0
;
	case method of
;
		0: $ ;files are chosen using the widget procedure dcs_widget_filesel.
		begin
			dcs_widget_filesel,group_leader=dcs_base,drive=drive,directory=directory,$
				filter=filter,start_filter=start_filter,stop_filter=stop_filter,$
				files_selected,nfiles_selected,type_of_file
			cancel=0
		end
;
		1: $ ;files_selected and nfiles_selected are restored.
		begin
			file=!dcs_outp_directory+!dcs_rootname+".fls"
			if (not file_test(file)) then file=!dcs_default_root+".fls" $
				else file=!dcs_rootname+".fls"
			fullfilename=dialog_pickfile(filter="*.fls",/fix_filter,$
				path=!dcs_outp_directory,/must_exist,$
				file=file,get_path=file_path,$
				title="Select the file that contains the file list.")
			cancel=fullfilename eq ""
			if (not cancel) then begin
				if (strlowcase(strmid(fullfilename,3,/reverse)) ne ".fls") then $
					fullfilename=fullfilename+".fls"
				restore,filename=fullfilename
			endif
		end
;
		else: return
	endcase
;
	if (nfiles_selected[0] eq 0) then begin
		decision=0
		if (not cancel) then begin
			dcs_multiplechoice,dcs_base,decision,"Fileselchoices",message_str,60,$
				skipdefaultchoices=skipdefaultchoices
			if (decision eq 1) then begin
				nfiles_selected=intarr(maxtypes)
				return
			endif
		endif
;
	endif else begin
;
		outstring="These are the files you have selected."
		blankstring=" "; Make a blank string of 64 characters!
		blankstring=blankstring+blankstring
		blankstring=blankstring+blankstring
		blankstring=blankstring+blankstring
		blankstring=blankstring+blankstring
		blankstring=blankstring+blankstring
		blankstring=blankstring+blankstring
		blength=strlen(blankstring)-2
		line=0
		for i=0,4 do begin
			nfi=nfiles_selected[i]
			if (nfi gt 0) then begin
				line=line+1
				outstring=[outstring,strcompress(type_of_file[i])+" files"]
				for j=0,nfi-1 do begin
					fstring=files_selected[i,j]
					flength=strlen(fstring)
					if (flength gt blength) then fstring="..."+strmid(fstring,flength-blength+3)
; The following line (plus the "endif else ..." line further down) can be
;	commented out to make one column instead of two.
					if (fix(j/2)*2 eq j) then begin
						line=line+1
						tmpstring=blankstring
						strput,tmpstring,fstring
						outstring=[outstring,tmpstring]
					endif else outstring[line]=outstring[line]+fstring
				endfor
			endif
		endfor
;
		outstring=[outstring,"","You may Revise your file selection, Save your selection,"+$
			" Proceed, or go to the main Menu.",""]
;
		maxlen=60
		blabel=['Revise','Save','Proceed','Main Menu']
		def_button=2
		can_button=3
		dcs_multichoice_modal,outstring,decision,blabel,def_button,$
			group_leader=dcs_base,alignment=-1
;
		if (decision eq 0) then decision=-1
		if (decision eq 1) then begin ; decision is to save
			repeat begin
				file=!dcs_rootname+".fls"
				fullfilename=dialog_pickfile(filter="*.fls",/fix_filter,$
					path=!dcs_outp_directory,$
					file=file,get_path=file_path,$
					title="Define the file that will contain the file list.")
				cancel=fullfilename eq ""
				compok=strcmp(file_path,!dcs_outp_directory,/fold_case)
				if (not cancel and not compok) then res=dialog_message("You may not change the directory.")
			endrep until (cancel or compok)
			if (cancel) then decision=0 else begin
				if (strlowcase(strmid(fullfilename,3,/reverse)) ne ".fls") then $
					fullfilename=fullfilename+".fls"
				save,filename=fullfilename,files_selected,nfiles_selected
			endelse
			outstring=["You may Proceed, or go to the main Menu.",""]
			dcs_multichoice_modal,outstring,decision,blabel,def_button,$
				group_leader=dcs_base,alignment=-1,visibility=[0,0,1,1]
		endif
;
		if (decision eq 3) then begin
			nfiles_selected=intarr(maxtypes)
			return
		endif
;
	endelse
endwhile
;
end
