//This code is called from the Digitize.otp
//Assumes the image object with the graph on it is named "Image"
//%B is used

[OpenTemplate]
	win -t plot Digitize;
	return 0;

////////////////////////////////////////////////////////////////////////////////
// Called from the Digitize button on Digitize.otp
////////////////////////////////////////////////////////////////////////////////
[Main]

	if (image1.show!=1 || image1.attach!=1)
	{
		type -b "Error!  You must paste an image onto this graph window and attach it to the page.\r\nThen name the object 'Image1'.";
		return 1;
	}
	else
	{
		%W=%H;
		run.section(,PickTwoPts, X 2 1)
	}
	return 0;

////////////////////////////////////////////////////////////////////////////////
// Get the numeric To and From axis values from the User (Getn)
// Also get the location of the axes on the graph window (dotool 2) 
////////////////////////////////////////////////////////////////////////////////
[PickTwoPts]
	%A="%1";
	if ( %2==2 )
		%B = "";
	else
		%B = (Then click Digitize button to trace the data. Press Esc to finish tracing) $:@HL;
	Getn 
	(Specify the %A axis scale and then double click on the image to locate the 2 points) $:@HL
	%B
	(From %A) %AVal1
	(To %A) %AVal2
	(%A Axis Scale);

	if((%AVal1==0/0) || (%AVal2==0/0)) 
	{
		type -b "Error!  Please enter two values on the axis.";
		if(%A=="X")
			run.section(,PickTwoPts, X 2);
		else
			run.section(,PickTwoPts, Y 4);
		return 1;
	}

	def pointproc {
		if(%3==1)
		{
			SCR1X=x;
		}
		if(%3==3)
		{
			SCR1Y=y;
		}
		if(%2==2)
		{
			dotool 2;
			def pointproc {
				SCR2X=x;
				dotool 0;
				run.section(digitize,PickTwoPts, Y 4 3);
			}
		}
		else
		{
			dotool 2;
			def pointproc {
				SCR2Y=y;
				dotool 0;
				//run.section(digitize,Section4);
			}
		}
	};
	dotool 2;
	return 0;

////////////////////////////////////////////////////////////////////////////////
// Set the invisible layer's position and size
////////////////////////////////////////////////////////////////////////////////
[Section4]
	if ( exist(SCR1x) == 0 || exist(SCR2y) == 0 )
	{
		type -b "Please Set Axes before Digitize";
		return 1;
	}
	
	if(xscale.choice==1)
	{
		Ax=(XVal1-XVal2)/(SCR1x-SCR2x);
		Bx=XVal1-Ax*SCR1x;
	}
	else
	{
		Ax=(log(Xval1)-log(Xval2))/(SCR1x-SCR2x);
		Bx=log(Xval1)-Ax*SCR1x;
	}
	if(yscale.choice==1)
	{
		Ay=(YVal1-YVal2)/(SCR1y-SCR2y);
		By=YVal1-Ay*SCR1y;
	}
	else
	{
		Ay=(log(YVal1)-log(YVal2))/(SCR1y-SCR2y);
		By=log(YVal1)-Ay*SCR1y;
	}
	//type -b "Trace your data by double clicking on the data points in the image.\r\nClick the Pointer tool or ESC to when you are finished.";
	run.section(digitize,TraceData);
	return 0;

////////////////////////////////////////////////////////////////////////////////
// Allow the user to trace over the data in their image
////////////////////////////////////////////////////////////////////////////////
[TraceData]

	doTool 0;	// return to the PointerTool first 
	doc -cs DRAW;	//count number of datasets begins with Draw
	count=nint(count/2+0.1);
	%B=Draw$(Count+1);
	create %B_B -c 100;
	getpts -new %B_B 100
	(Double-click to add new pt, click any tool to quit or hit Esc);
	def QuitToolBox
	{
		%A=xof(%B);
		if(xscale.choice==1)
		{
			"%A"=Ax*%A+Bx;
		}
		else
		{
			"%A"=10^(Ax*%A+Bx);
		}
		if(yscale.choice==1)
		{
			"%B"=Ay*%B+By;
		}
		else
		{
			"%B"=10^(Ay*%B+By);
		}
	
		if(getpts.count == 0)
		{
			del %B_B;	//If no points are traced, delete the dataset
			//run.section(digitize,Cleanup);
			def QuitToolBox {};
			return 1;
		}
		run.section(digitize,CreateNewGraph);
	};
	return 0;



////////////////////////////////////////////////////////////////////////////////
// Open a new graph window and plot the traced data into it
////////////////////////////////////////////////////////////////////////////////
[CreateNewGraph]

	//loop(ii,2,4) {label -r label$(ii);}	//remove temporary labels from original graph window
	//Need to null the QuitToolBox macro because it is called with the win -t command
	def QuitToolBox {};	
	win -t plot origin;	//Open a new graph window
	queue {layer -all ask;}
	layer -i202 %B;		//Include the newly created data in the layer
	layer.x.type=%W!xscale.choice;
	layer.y.type=%W!yscale.choice;
	//run.section(,Cleanup);
	return 0;

////////////////////////////////////////////////////////////////////////////////
//Delete global variables
////////////////////////////////////////////////////////////////////////////////
[Cleanup]
	delete -v xval1;delete -v xval2;
	delete -v yval1;delete -v yval2;
	delete -v SCR1x;delete -v SCR2x;
	delete -v SCR1y;delete -v SCR2y;
	return 0;

