; $Id: dcs_reduction_pathname.pro,v 1.13 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_pathname_handler,event
;************************************************************************************************
;
compile_opt strictarr
;
widget_control,event.top,get_uvalue=pstate,/no_copy
base=(*pstate).base
done=(*pstate).done
help=(*pstate).help
cancel=(*pstate).cancel
pathname=(*pstate).pathname
getpathname=(*pstate).getpathname
defaultpathname=(*pstate).defaultpathname
intro=(*pstate).intro
;
evid=event.id
;
;	Respond to "getpathname" button.
if (evid eq getpathname) then begin
	extension=".log"
	repeat begin
		fullfilename=dialog_pickfile(filter="*"+extension,/fix_filter,$
			path=!dcs_outp_directory,$
			file=!dcs_default_root,get_path=file_path,title="Define journal file name. DON''T CHANGE DIRECTORY.")
		if (fullfilename eq "") then begin
			widget_control,event.top,set_uvalue=pstate,/no_copy
			return
		endif
		test=strcmp(file_path,!dcs_outp_directory,/fold_case)
		if (not test) then result=dialog_message($
			"You may not change the directory where the output will be written "+$
			"(the ''working'' directory). "+$
			"If you want to change the working directory you should click ''OK'',"+$
			"''Cancel'' in the ''Define journal file name'' window, then ''Main Menu'' "+$
			"to return to the main DAVE menu, and then select "+$
			"''File|Working directory''.",/error)
	endrep until (test)
	if (strlowcase(strmid(fullfilename,3,/reverse)) ne extension) then $
		fullfilename=fullfilename+extension
	filename=strmid(fullfilename,strlen(file_path)); extract filename
	filenameroot=strmid(filename,0,strlen(filename)-4); extract root
	if (filenameroot eq "") then filenameroot=!dcs_default_root
	defsysv,'!dcs_rootname',filenameroot
	widget_control,done,/sensitive
	widget_control,base,default_button=done
	widget_control,pathname,set_value=$
		[intro,'Output file root name is "'+!dcs_rootname+'".']
end
;
;	Respond to "defaultpathname" button.
if (evid eq defaultpathname) then begin
	defsysv,'!dcs_rootname',!dcs_default_root
	widget_control,event.top,/destroy
	ptr_free,pstate
	return

end
;
;	Respond to "Main Menu" button.
if (evid eq cancel) then begin
	defsysv,'!dcs_rootname',""
	widget_control,event.top,set_uvalue=pstate,/no_copy
	widget_control,event.top,/destroy
	return
end
;
;	Respond to "Help" button.
if (evid eq help) then begin
	helptext=[$
		"It is first necessary to define the root name of a journal "+$
		"file that will contain a summary of the data reduction session. "+$
		"The journal file's extension will be ''.log''.","",+$
		"Note that at this stage you cannot change your "+$
		"''output'' (''Working'') or your ''input'' (raw data) directories. ",$
		"If you want to change the working directory you should click ''OK'', "+$
		"then ''Main Menu'' to return to the main DAVE menu, and then select "+$
		"''File|Working directory''."+$
		"Similarly select ''File|Raw data directory'' to change the "+$
		"raw data directory.","",$
		"All files created during this session, including intermediate and final "+$
		"data files, plus masking, detector efficiency, and detector grouping files, "+$
		"will use the same root name."+$
		"You may choose your own root name or use the default root name."]
	result=dialog_message(helptext,title="Choosing the root file name",/information)
endif
;
;	Respond to "Proceed" button.
if (evid eq done) then begin
	widget_control,event.top,/destroy
	ptr_free,pstate
	return
endif
;
(*pstate).pathname=pathname
;
widget_control,event.top,set_uvalue=pstate,/no_copy
end


;************************************************************************************************
pro dcs_reduction_pathname,dcs_base,davePtr=davePtr,logentry=logentry
;************************************************************************************************
; This procedure creates a widget that defines the root name of a journal
;	file that will contain a summary of data reduction operations.
;
compile_opt strictarr
;
;
extension=".log"
;
cd,current=curdir
if (strmid(curdir,0,/reverse_offset) ne !delimiter) then $
	curdir=curdir+!delimiter
;
intro1=['The raw data directory is "'+!dcs_data_directory+'".',$
	'The "working" (output) directory is "'+!dcs_outp_directory+'".',$
	'The current directory is "'+curdir+'".']
intro2=['To change any of these directories you must return to '+$
	'the main DAVE menu (click "Main Menu").']
intro3=['For further information click the "Help" button.']
intro4=['The default root name for output files is "'+!dcs_default_root+'".','',$
	'To use this root name click "Use default root name".',$
	'To use another root name click "Choose root name".','']
intro=intro4
width=max(strlen(intro))
;
; Create widget
base=widget_base(title="Choose root name for output files.",/col,$
	group_leader=dcs_base,/modal)
	if (!windows) then dcs_fixedfont="courier*12"
	if (!unix) then dcs_fixedfont="courier-12"
	if (!macos) then dcs_fixedfont="courier*12"
	pathname=widget_text(base,xsize=width,ysize=8,/wrap,$
		value=intro,font=dcs_fixedfont,/editable)
	firstrow=widget_base(base,/row)
		defaultpathname=widget_button(firstrow,value="Use default root name")
		getpathname=widget_button(firstrow,value="Choose root name")
		cancel=widget_button(firstrow,value="Main Menu")
		help=widget_button(firstrow,value="Help")
		done=widget_button(firstrow,value="Proceed",sensitive=0)
;
; Realize widget
dave_position_tlb,base
widget_control,base,/realize
;
; Define default and cancel buttons.
widget_control,base,default_button=defaultpathname
widget_control,base,cancel_button=cancel
;
state={base:base,done:done,help:help,getpathname:getpathname,defaultpathname:defaultpathname,$
	intro:intro,cancel:cancel,pathname:pathname}
;
pstate=ptr_new(state)
widget_control,base,set_uvalue=pstate,/no_copy
xmanager,'dcs_reduction_pathname',base,event_handler='dcs_reduction_pathname_handler'
;
if (!dcs_rootname eq "") then return
;
defsysv,'!logfile',!dcs_outp_directory+!dcs_rootname+extension
;
; Create line to be included in the log file.
datetime=systime(0)
datetime=strmid(datetime,0,11)+$
	strmid(datetime,20)+" at "+strmid(datetime,11,8)
;
logentry="DAVE-DCS session log file created "+datetime+"."
;
if (not lmgr(/demo)) then begin
	openw,unit,!logfile,/get_lun
	printf,unit,logentry
	free_lun,unit
endif
;
;
end
