		/// ML 11/8/2005 QA70-7659 FORTEBIO_ALLOW_MORE_LAYERS_LAYER_TOOL
// Purpose:     Modularized Code for adding and arranging layers

[Tab.Add.OnInit]
	LayerTool!Tab.Add.XScale.choice=1;
	LayerTool!Tab.Add.YScale.choice=1;

[Tab.Arrange.OnInit]
	LayerTool!Tab.Arrange.VertGap.v1=5;
	LayerTool!Tab.Arrange.HorzGap.v1=5;
	LayerTool!Tab.Arrange.Columns.v1=2;
	LayerTool!Tab.Arrange.Rows.v1=2;

	LayerTool!Tab.Arrange.Top.v1=10;
	LayerTool!Tab.Arrange.Bottom.v1=10;
	LayerTool!Tab.Arrange.Left.v1=10;
	LayerTool!Tab.Arrange.Right.v1=10;
	
	LayerTool!Tab.Arrange.VertPanel.Choice=3;


[Tab.Arrange.Active]
run.section(,ArrangeTab);
	
[Tab.Arrange.VertPanel.OnClick]
run.section(,ArrangeTab);

	
[ArrangeTab]	
	
	switch (LAYERTOOL!TAB.ARRANGE.VERTPANEL.CHOICE)
	{
		case 1:
			LayerTool!Tab.Arrange.Columns.v1=1;
			LayerTool!Tab.Arrange.Rows.v1=page.nlayers;
			break;
			
		case 2:
			LayerTool!Tab.Arrange.Columns.v1=page.nlayers;
			LayerTool!Tab.Arrange.Rows.v1=1;
			break;
		default:
			LayerTool!Tab.Arrange.Columns.v1=page.nlayers;
			LayerTool!Tab.Arrange.Rows.v1=1;
			break;
	}
	
	
[Tab.Move.Active]
	
	LayerTool!Tab.Move.LayerA.numitems=page.nlayers;
	LayerTool!Tab.Move.LayerB.numitems=page.nlayers;
	
	loop(ii,1,page.nlayers)
	{
		LayerTool!Tab.Move.LayerA.v$(ii)=ii;
		LayerTool!Tab.Move.LayerB.v$(ii)=ii;			
	}
	
	LayerTool!Tab.Move.LayerA.choice=1;
	if(page.nlayers==1)
		LayerTool!Tab.Move.LayerB.choice=1;	
	else
		LayerTool!Tab.Move.LayerB.choice=2;



////////////////////////////////////////////////////////////////////////////////
// Main code which is called from Arrange Page
////////////////////////////////////////////////////////////////////////////////

[Tab.Arrange.Arrange.OnClick]
	switch (LAYERTOOL!TAB.ARRANGE.VERTPANEL.CHOICE)
	{
		case 1:
			run.section(LayerTool,VertPanel);
				break;
			
		case 2:
			run.section(LayerTool,HorzPanel);
			
				break;
		case 3:
			run.section(LayerTool,MxNPanel);
			break;
		
		default:
				break;
	}
	


//[Tab.Arrange.Toolbar1.Vert.OnClick]
[VertPanel]
	if(run.section(,CheckWindow)==0)
	{
		LayerTool!StoreRows.v1= page.nlayers-page.nlinks;
		LayerTool!StoreColumns.v1= 1;
		if(page.nlayers-page.nlinks==1)
		{
			LayerTool!StoreRows.v1=2;
			page -L layer;
			plot -L;
		}
	}
	run.section(,ArrangeLayers);
	return 0;

//[Tab.Arrange.Toolbar1.Horz.OnClick]
[HorzPanel]
	if(run.section(,CheckWindow)==0)
	{
		LayerTool!StoreRows.v1= 1;
		LayerTool!StoreColumns.v1= page.nlayers-page.nlinks;
		if(page.nlayers-page.nlinks==1)
		{
			LayerTool!StoreColumns.v1=2;
			page -L layer;
			plot -L;
		}
	}
	run.section(,ArrangeLayers);
	return 0;

//[Tab.Arrange.Toolbar1.MxN.OnClick]
[MxNPanel]
	if(run.section(,CheckWindow)==0)
	{
		LayerTool!StoreColumns.v1= LayerTool!Tab.Arrange.Columns.v1;
		LayerTool!Storerows.v1= LayerTool!Tab.Arrange.Rows.v1;
		/// ML 11/8/2005 QA70-7659 FORTEBIO_ALLOW_MORE_LAYERS_LAYER_TOOL
		//if (LayerTool!Storerows.v1*LayerTool!StoreColumns.v1>50) 
		if (LayerTool!Storerows.v1*LayerTool!StoreColumns.v1>121) 
		/// end FORTEBIO_ALLOW_MORE_LAYERS_LAYER_TOOL
		{
			type -b $layer.TooMany;
			break 1;
		}  
		LayerTool!StoreTotal.v1=LayerTool!Storerows.v1 * LayerTool!StoreColumns.v1 - (page.nlayers-page.nLinks);

		if(LayerTool!StoreTotal.v1 > 0 && LayerTool!Tab.Arrange.NewLayer) 
		{
					
			repeat LayerTool!StoreTotal.v1 
			{
				/*layer -n; create a new layer */
				/* CPy v3.61, change to load from template */
				page -L layer;
	
				plot -L;
				/* plot the layer, this forces the layer
				 * to finish making itself before
				 * the next one is created.
				 */
			};
		} 
	}
	run.section(,ArrangeLayers);
	return 0;
	
	

////////////////////////////////////////////////////////////////////////////////
// Arrange all the layers (just like ArrangeAll macro) but without GetNum input
////////////////////////////////////////////////////////////////////////////////
[ArrangeLayers]
	 	
	LayerTool!StoreWidth.v1 = 100 - LayerTool!Tab.Arrange.Left.v1 - LayerTool!Tab.Arrange.Right.v1 - (LayerTool!StoreColumns.v1-1)*LayerTool!Tab.Arrange.HorzGap.v1;
	LayerTool!StoreWidth.v1/=LayerTool!StoreColumns.v1;
	LayerTool!StoreHeight.v1 = 100 - LayerTool!Tab.Arrange.Top.v1 - LayerTool!Tab.Arrange.Bottom.v1 - (LayerTool!StoreRows.v1-1)*LayerTool!Tab.Arrange.VertGap.v1;
	LayerTool!StoreHeight.v1/=LayerTool!storerows.v1;
	if(LayerTool!StoreWidth.v1 < 5 || LayerTool!StoreHeight.v1 < 5) 
	{
		type -b $layer.TooSmall;
		break 1;
	} 

	LayerTool!Storekk.v1 = 0;
	for(LayerTool!Storeii.v1 = 1; LayerTool!Storeii.v1 <= LayerTool!Storerows.v1; LayerTool!Storeii.v1+=1)
	{
		for(LayerTool!Storejj.v1 = 1; LayerTool!Storejj.v1 <= LayerTool!StoreColumns.v1; LayerTool!Storejj.v1+=1) 
		{

				/* skip all the child layers */
				for(LayerTool!Storekk.v1+=1;LayerTool!Storekk.v1 <=page.nlayers;LayerTool!Storekk.v1+=1) 
				{
					#!LayerTool!Storekk.v1=;
					layer -s LayerTool!Storekk.v1;
					if(layer.link == 0 || layer.unit != 7) break;
				} 
			
			layer -u 1;/* make sure unit is 1 which is % of page */

			  /* one column, put layer 1 at bottom */
			if(LayerTool!StoreColumns.v1==1)
				layer LayerTool!StoreWidth.v1 LayerTool!StoreHeight.v1 [LayerTool!Tab.Arrange.Left.v1+(LayerTool!Storejj.v1-1)*(LayerTool!StoreWidth.v1 + LayerTool!Tab.Arrange.HorzGap.v1)] [LayerTool!Tab.Arrange.Top.v1+(LayerTool!StoreRows.v1 - LayerTool!Storeii.v1)*(LayerTool!StoreHeight.v1 + LayerTool!Tab.Arrange.VertGap.v1)];
			else 
				layer LayerTool!StoreWidth.v1 LayerTool!StoreHeight.v1 [LayerTool!Tab.Arrange.Left.v1+(LayerTool!Storejj.v1-1)*(LayerTool!StoreWidth.v1 + LayerTool!Tab.Arrange.HorzGap.v1)] [LayerTool!Tab.Arrange.Top.v1+(LayerTool!Storeii.v1-1)*(LayerTool!StoreHeight.v1 + LayerTool!Tab.Arrange.VertGap.v1)];
		}
	}


////////////////////////////////////////////////////////////////////////////////
// The following sections are called from Add Page
////////////////////////////////////////////////////////////////////////////////
[CheckWindow]
	if (exist(%H,11)==11) 
	{
		type -b;//BEEP!
		type -b $layer.layout;
		type -q "";//Clear Status Bar
		return 1;
	}


	
	
[SwapMain]
	//Need to use v1 and v2 instead of A and B, since object.A is property
	if(LayerTool!Tab.Move.LayerA.s1==LayerTool!Tab.Move.LayerB.s1 || LayerTool!Tab.Move.LayerA.s1<1 || LayerTool!Tab.Move.LayerA.s1>page.nlayers || LayerTool!Tab.Move.LayerB.s1<1 || LayerTool!Tab.Move.LayerB.s1>page.nlayers )
	{
		type -b $Layer.OutofBounds;
		return 1;
	}
	loop(ii,1,page.nlayers)
	{
		if((layer$(ii).link==LayerTool!Tab.Move.LayerA.s1 || layer$(ii).link==LayerTool!Tab.Move.LayerB.s1) && layer$(ii).unit==7)
		{
			layer$(ii).unit=1;
		}
	}	
	
	LayerTool!StoreActivePage.s1=page.active;		//Get the current active layer
	if(%1==1)
	{
		page.active=LayerTool!Tab.Move.LayerA.s1;	//Make Layer A active
		layer -x;		//Get the dimensions
		LayerTool!StoreDim1.v1=v1;LayerTool!StoreDim2.v1=v2;
		LayerTool!StoreDim3.v1=v3;LayerTool!StoreDim4.v1=v4;
	}		
	
	page.active=LayerTool!Tab.Move.LayerB.s1;		//Make Layer B active
	layer -x;			//Get the dimensions
	LayerTool!StoreDim5.v1=v1;LayerTool!StoreDim6.v1=v2;
	LayerTool!StoreDim7.v1=v3;LayerTool!StoreDim8.v1=v4;

	if(%1==1)
	{
		layer LayerTool!StoreDim1.v1 LayerTool!StoreDim2.v1 LayerTool!StoreDim3.v1 LayerTool!StoreDim4.v1;	//Move layer B to A
	}
	page.active=LayerTool!Tab.Move.LayerA.s1;		//Make Layer A active
	layer LayerTool!StoreDim5.v1 LayerTool!StoreDim6.v1 LayerTool!StoreDim7.v1 LayerTool!StoreDim8.v1;	//Move layer A to B

	page.active=LayerTool!StoreActivePage.v1;		//Restore the original active layer

[Tab.Add.Toolbar.Normal.OnClick]
	#Adds a normal layer to the active graph window
	if(run.section(,CheckWindow)==0)
	{
		layer -new;
		layer -b a 0;
		layer -b c 1;
		layer.x.type=LayerTool!Tab.Add.XScale.choice;
		layer.y.type=LayerTool!Tab.Add.YScale.choice;
		return 0;
	}

[Tab.Add.Toolbar.TopX.OnClick]
	#Adds a linked LayerTool!Tab.Arrange.Top.v1 x axis to the active layer
	if(run.section(,CheckWindow)==0)
	{
		layer -new x;
		layer -b a 0;
		layer -b c 1;
		layer.x.type=LayerTool!Tab.Add.XScale.choice;
		return 0;
	}

[Tab.Add.Toolbar.RightY.OnClick]
	#Adds a linked LayerTool!Tab.Arrange.Right.v1 y axis to the active layer
	if(run.section(,CheckWindow)==0)
	{
		layer -new y;
		layer -b a 0;
		layer -b c 1;
		layer.y.type=LayerTool!Tab.Add.YScale.choice;
		return 0;
	}

[Tab.Add.Toolbar.TopXRightY.OnClick]
	#Adds a linked layer to the active layer
	if(run.section(,CheckWindow)==0)
	{
		layer -new Both;
		layer -b a 0;
		layer -b c 1;
		layer.x.type=LayerTool!Tab.Add.XScale.choice;
		layer.y.type=LayerTool!Tab.Add.YScale.choice;
		return 0;
	}

[Tab.Add.Toolbar.Inset.OnClick]
	if(run.section(,CheckWindow)==0)
	{
		layer -s page.active; /*select the active layer*/
		layer -new both; /*adds a linked layer to the active layer*/
		layer -b a 0;
		layer -b c 1;
		layer.x.showaxes=1;
		layer.y.showaxes=1;
		layer.x.showlabels=1;
		layer.y.showlabels=1;
		layer.x.type=LayerTool!Tab.Add.XScale.choice;
		layer.y.type=LayerTool!Tab.Add.YScale.choice;
		layer.left=75;
		layer.width=25;
		layer.height=25;
		return 0;
	}

[Tab.Add.Toolbar.InsetData.OnClick]
	if(run.section(,CheckWindow)==0)
	{
		layer -s page.active; /*select the active layer*/
		layer -c; /*concatenate the names of all datasets into %Z*/
		layer -new both; /*adds a linked layer to the active layer*/
		layer -b a 0;
		layer -b c 1;
		layer.x.showaxes=1;
		layer.y.showaxes=1;
		layer.x.showlabels=1;
		layer.y.showlabels=1;
		layer.x.type=LayerTool!Tab.Add.XScale.choice;
		layer.y.type=LayerTool!Tab.Add.YScale.choice;
		layer.left=75;
		layer.width=25;
		layer.height=25;
		layer -i %Z; /*include datasets in layer*/
		%Z="";
		layer -all;
		return 0;
	}

[Tab.Move.Swap.OnClick]
	run.section(,SwapMain,1);

[Tab.Move.Move.OnClick]
	run.section(,SwapMain,2);
