// Filename:    pClamp.ogs
// Creation:    by Ree Migliozzi, 4/1/96
// Purpose:     Modularized Code for importing Axon binary files 


////////////////////////////////////////////////////////////////////////////////
// Main Code which is run based if user hits Axon File Import button
////////////////////////////////////////////////////////////////////////////////
[main]
	dll -a abf lababf32;

	if(run.section(,Initialize)!=0)
		type -b $Template.pClampVersion;
	if (FDLOG.multiopen()==0/0)
		return 1;

	if(FDlog.MultiOpen.Count==1)
	{
		FDlog.Get(A, 1);
		abf.filename$=%A;
		abf.ReadHeader();
		if($(abf.dialog())!=0)
			break;
		run.section(,SetupWorksheet);
		run.section(pclamp.ogs,Import,abf.ImportType); 
		run.section(,FileResults);
		if(abf.TagInfo==1 && abf.P48>0)
			run.section(pclamp.ogs,tag);
		wks.labels();
	};
	else
	{
		abf.MultiSelection=1;
		if($(abf.dialog())!=0)
			break;
		run.section(,SetupWorksheet);
		loop(file,1,FDlog.MultiOpen.Count)
		{
			FDlog.Get(A, file);
			abf.filename$=%A;
			abf.ReadHeader();
			run.section(pclamp.ogs,Import,file); 
			run.section(,FileResults);
		}
		wks.labels();
		delete -v file;
	};
	return 0;

[Initialize]
	FDLOG.DlgName$=pClamp Import;
	FDLOG.multiopen.colview=12;	// AW: Add other two sort options.
	FDLOG.OptionDLG$="";		//Reset button to off
	FDLOG.checkname$="";		//Reset check box to null
	FDLOG.ShowComment=0;		//close the comments box
	FDLOG.CheckStatus=0;		//Set checkbox to off
	FDLOG.default$="";		//Set contents of "File Name" edit box to nothing
	FDLOG.usegroup(pClamp);
	return 0;

[SetupWorksheet]
	if(@v<4.0)
		return 1;		
	else
     	{
		clearworksheet %H;		//This makes the deleting process quicker
		repeat wks.ncols (del(col(1)));	//delete all the columns
		Abf.FirstCol=1;
		abf.wksname$=%H;
		return 0;
	}

[Import]
	if(abf.ImportType==1)
	{
	 	xcol=wks.ncols+1;
		worksheet -a $(abf.maxepisode+1);
		abf.timeflag=1;
		abf.readepisode();
    		wks.col$(xcol).type=4;
		if (FDlog.MultiOpen.Count>1) wks.col$(xcol).name$=F$(%1)Time;
		else  wks.col$(xcol).name$=Time;
		wks.col$(xcol).label$=ms;
		run.section(,EpisodeColumns,%1);	
	}
	else
	{
		xcol=wks.ncols+1;
		worksheet -a abf.maxchannel;
		abf.timeflag=0;
		abf.readchannel();
		if(FDlog.MultiOpen.Count==1);
		{
			set %h -f 0;
			set %h -i (abf.p120 * abf.p122 / 1000);
		}
		run.section(,ChannelColumns,%1);		
	}
	doc -uw;	//refresh
	return 0;

[EpisodeColumns]
	for(ii=xcol+1,jj=1;ii<=wks.ncols;ii++,jj++)
	{
		if(FDlog.MultiOpen.Count>1)
		{
			wks.col$(ii).name$=F$(%1)Ep$(jj);
		}
		else
		{						
			wks.col$(ii).name$=Ep$(jj);
		}	
		wks.col$(ii).label$=abf.p602.$(realchan.v1)$;
		}
	}

[ChannelColumns]
	for(ii=xcol,jj=1;ii<=wks.ncols;ii++,jj++)
	{
		if(FDlog.MultiOpen.Count>1)
		{
			wks.col$(ii).name$=F$(%1)Ch$(jj-1);
		}
		else
		{
			wks.col$(ii).name$=Ch$(jj-1);
		}
		kk=abf.p410.$(jj-1); 
		wks.col$(ii).label$=abf.p602.$(kk)$;
	}


[Tag]
	SaveRedirection=type.Redirection(16,3);	// SDB 1/7/99 v6.0148 REPORT_TO_OUTPUTLOG
	type.BeginResults();		/// RKM 12/10/98 v6.0141 REPORT_TO_OUTPUTLOG

	loop(ii,1,abf.p48) 
	{
		type -a;
		separator 4;
		type -l $Template.pClampTagNum;
		type "$(ii)";
		abf.tagN=ii-1;
		abf.readTag();
			
		type -l $Template.pClampTagTime;
		type "$(abf.tagtime * abf.p122 / 1000)";
		type -l $Template.pClampTagEpisode;
		type "$(abf.tagEpisode)";

		%A=abf.tagComment$;
		type -l $Template.pClampTagComment;
		type %A;
		separator 4;
	};	
	type.Redirection=SaveRedirection; // restore previous redirection.
	delete -v SaveRedirection;
	return 0;

[FileResults]
	SaveRedirection=type.Redirection(16,3);	// SDB 1/7/99 v6.0148 REPORT_TO_OUTPUTLOG
	type.BeginResults();		/// RKM 12/10/98 v6.0141 REPORT_TO_OUTPUTLOG
	type;
	%A=abf.FileName$;
	type -v "File name = %A";
	type -v "Number of Channels = $(abf.MaxChannel)";
	type -v "Number of Episodes = $(abf.MaxEpisode)";
	type -v "Episode Size = $(abf.EpisodeSize)";
	type -v "Interval = $(abf.p120 * abf.p122 / 1000)";
	type;
	type.Redirection=SaveRedirection; // restore previous redirection.
	delete -v SaveRedirection;
	return 0;

[Plot]
	%a=%h;
	if (wks.sel==0)
	{
		work -s 0 0 0 0;		//If nothing is selected selected the whole worksheet
		work -p ? pClamp;		//Plot into the pClamp graph template
		doc -cp %aGraph;		//Count how many graphs are named %aGraph
		win -r %h %aGraph$(count+1);	//Rename the graph
	}
	else
	{
		work -p ? pClamp;
		doc -cp %aGraph;
		win -r %h %aGraph$(count+1);
	};

	mscale.show=0;
	queue
	{
		i=legend.show;
		if(i!=0/0) 
		{				//temp method to test existance
			legend;
		}
		run.section(,resize, 0);		//mouse click is 1 
		mscale.show=1;
		show.xvalue=yline.x;
	};
	win -o %a {worksheet -s;}; 

[Resize]
//%1==0 means the plot was just created
//%1==1 means the user clicked the resize button
	mscale.yrange=y3;
	mscale.xrange=x3;

	if(%1==1) 
	{
		getn
		($Template.PClampYScale) mscale.yrange	
		($Template.PClampXScale) mscale.xrange
		($Template.PClampScaleRange);
		clr;
	}

	mscale.l1$=$(mscale.xrange);
	mscale.l2$=$(mscale.yrange);
