////////////////////////////////////////////////////////////
// Filename:	GRAPH.OGS
// Purpose:	General Graph Operations from Context Menus or Toolbars
// Modifications:	RKM added [BotXLeftY] through [ColorPalette]
//			for Origin 5.0
//			Took out many one line sections and put the script right
//			in the string associated with the ID
//			RKM add [ArrangeAllLayers]; we did have Graph.ogs in Origin4.1
////////////////////////////////////////////////////////////


[SetDisplayRange]
	if (MkS1 >= 0) 
	{
		set %C -b MkS1;
		set %C -e MkS2;
		MkS1=-1
	} 
	else 
	{
		ty -c $EXEmessage.DataSelectTool
	};

[MoveData]
	get %C -m v1;
	dotool 0;
	if (v1 == 0) 
	{
		ty -N $EXEmessage.MoveDataPoints;
		set %C -m 1;
		mvpts %C;
	} 
	else 
	{
		mvpts %C
	};

[RemoveBadData]
	Mark -x %C;
	def EndToolbox {
		;i1=_indx[1];
		undo -dr %C i1 i1;
		Mark -d %C -b i1 -e i1;
	};
	GetPts 1 $EXEmessage.DataPtRemove;

[MergeAllPages]
	if (run.section(,Merge)==0)
	{
		run.section(,ArrangeAllLayers)
	}
[Merge]
	Window -MA; // CPY v3.6 changed from -M
	return 0;

[ArrangeAllLayers]
	nColumn=((int(sqrt(page.nlayers-page.nlinks)))^2==(page.nlayers-page.nlinks)?sqrt(page.nlayers-page.nlinks):(int(sqrt(page.nlayers-page.nlinks))*(int(sqrt(page.nlayers-page.nlinks))+1)>=(page.nlayers-page.nlinks)?int(sqrt(page.nlayers-page.nlinks)):int(sqrt(page.nlayers-page.nlinks))+1));
	nRow=((int(sqrt(page.nlayers-page.nlinks)))^2==(page.nlayers-page.nlinks)?sqrt(page.nlayers-page.nlinks):int(sqrt(page.nlayers-page.nlinks))+1);
	CheckVar nRow 2;
	CheckVar nColumn 1;
	GetNum 
	$graph.ArrangeAllLayersnRow nRow
	$graph.ArrangeAllLayersnColumn nColumn
	$graph.ArrangeAllLayersLayers;

	nTotal=page.nlayers-page.nLinks;
	/* v3.48 CPY added */
	n = nRow * nColumn - nTotal;
	
	//JMP & LAS 9/29/1995
	//Modify such that if user doesn't want new layers, existing layers are arranged
	//according to specified gridding array
	//ADD new section: ASKMORELAYERS
	
	if (n > 0 && run.section(,AskMoreLayers)==0) 
	{
		repeat n 
		{
			//CPY v3.61
			// layer -n; creates a new layer, but
			// change to load from template: LAYER.OTP */
			page -L layer;

			plot -L;
			/* plot the layer, this force the layer
			 * to finish making itself before
			 * the next one is created.
			 */
		};
	}
	
	run.section(,CheckMargins);
	run.section(,Arrange);			//Need this to be in a separate section so that it can be recalled


////////////////////////////////////////////////////////////////////////////////
[Arrange]

	if (run.section(,GetGaps)==1)
		return 1;
	
	if (run.section(,ArrangeLayers,nRow nColumn)==1)
	{
		run.section(,Arrange);
	}
	

////////////////////////////////////////////////////////////////////////////////
[AskMoreLayers]
	type -N $general.arrangeLayer;	//Ask permission for adding layers
	return 0;


////////////////////////////////////////////////////////////////////////////////
[CheckMargins]                                                       
	CheckVar Top 10;
	CheckVar Left 15;
	CheckVar Right 10;
	CheckVar Bottom 15;
	CheckVar xGap 5;
	CheckVar yGap 5;
	return 0;
////////////////////////////////////////////////////////////////////////////////
[GetGaps]
	GetNum
	$graph.ArrangeAllLayersHorizontal  xGap
	$graph.ArrangeAllLayersVertical  yGap
	$graph.ArrangeAllLayersLeft  Left
	$graph.ArrangeAllLayersRight  Right
	$graph.ArrangeAllLayersTop  Top
	$graph.ArrangeAllLayersBottom  Bottom
	$graph.ArrangeAllLayersSpacings;

////////////////////////////////////////////////////////////////////////////////
[ArrangeLayers]
	x = 100 - Left - Right - (%2-1)*xGap;
	x/=%2;
	y = 100 - Top - Bottom - (%1-1)*yGap;
	y/=%1;
	if(x < 5 || y < 5) 
	{
		type -b $plot.layError;	//Error! Resulting layers are too small.  The operation must be aborted.
		return 1;
	} 
	cc=1;rr=1;laynum=1;
	loop(ii,1,page.nlayers)
	{
		page.active=ii;
		if(layer.unit == 7) continue;
		laynum+=1;
		layer -u 1;
		if (%2==1) 
			layer x y [Left+(cc-1)*(x + xGap)] [Top+(%1 - rr)*(y + yGap)];
		else 
			layer x y [Left+(cc-1)*(x + xGap)] [Top+(rr-1)*(y + yGap)];

		cc=mod(laynum,%2);
		cc=cc==0?%2:cc;
		rr=cc==1?(rr+1):rr;
		rr=rr>%1?1:rr;
	} 
	delete -v rr;
	delete -v cc;
	delete -v laynum;
	return 0;

[RotatePage]
	page -O Rotate; // P = portrait, L = landscape, R = rotate


[FitLayerToPage]
	type -N $Graph.FitLayerToPage;
	layer 0;

[FitPageToLayers]
	page -FL;



