// Filename:    Layer.ogs
// Creation:    by Laurie Shea, 8/28/95
// Purpose:     Modularized Code for adding and arranging layers

////////////////////////////////////////////////////////////////////////////////
// Main code which is called from Arrange Page
////////////////////////////////////////////////////////////////////////////////
[main]
	if (exist(%H,11)==11) 
	{
		type -b;//BEEP!
		type -b $layer.layout;
		type -q "";//Clear Status Bar
		break 1;
	}

	switch (index)
	{
	case 1:
		store.rows= page.nlayers-page.nlinks;
		store.columns= 1;
		break;
	case 2:
		store.rows= 1;
		store.columns= page.nlayers-page.nlinks;
		break;
	case 3:
		store.columns= mn.v1;
		store.rows= mn.v2;
		//if (store.rows*store.columns>50)  ///Iris 5/25/05 QA70-7661 INCREASE_LAYER_NUMBER
		if (store.rows*store.columns>121) 
		{
			type -b $layer.TooMany;
			break 1;
		}  
		store.total=store.rows * store.columns - (page.nlayers-page.nLinks);

		if(store.total > 0 && addlayer.v1) 
		{
					
			repeat store.total 
			{
				/*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.
				 */
			};
		} 
		
		break;
	}; 
	
	
	run.section(,ArrangeLayers);
	
	

////////////////////////////////////////////////////////////////////////////////
// Arrange all the layers (just like ArrangeAll macro) but without GetNum input
////////////////////////////////////////////////////////////////////////////////
[ArrangeLayers]
	 	
	store.width = 100 - gap.left - gap.right - (store.columns-1)*horizontal.horizontalgap;
	store.width/=store.columns;
	store.height = 100 - gap.top - gap.bottom - (store.rows-1)*vertical.verticalgap;
	store.height/=store.rows;
	
	if(store.width < 5 || store.height < 5) 
	{
		type -b $layer.TooSmall;
		break 1;
	} 

	store.kk = 0;
	for(store.ii = 1; store.ii <= store.rows; store.ii+=1)
	{
		for(store.jj = 1; store.jj <= store.columns; store.jj+=1) 
		{

				/* skip all the child layers */
				for(store.kk+=1;store.kk <=page.nlayers;store.kk+=1) 
				{
					#!store.kk=;
					layer -s store.kk;
					if(layer.link == 0 || layer.unit != 7) break;
				} 
			
			layer -u 1;/* make sure unit is 1 which is % of page */
			if(store.columns==1)
			  /* one column, put layer 1 at bottom */
				layer store.width store.height [gap.left+(store.jj-1)*(store.width + horizontal.horizontalgap)] [gap.top+(store.rows - store.ii)*(store.height + vertical.verticalgap)];
			else 
				layer store.width store.height [gap.left+(store.jj-1)*(store.width + horizontal.horizontalgap)] [gap.top+(store.ii-1)*(store.height + vertical.verticalgap)];
		}
	}


////////////////////////////////////////////////////////////////////////////////
// The following two sections are called from Add Page
////////////////////////////////////////////////////////////////////////////////

[Construct]
	if (exist(%H,11)==11) 
	{
		type -b;//BEEP!
		type -b $layer.layout;
		type -q "";//Clear Status Bar
		break 1;
	}
	switch (index)
	{
	case 1:
		#Adds a normal layer to the active graph window
		layer -new;
		layer.x.type=xscale.choice;
		layer.y.type=yscale.choice;
		break;
	case 2:
		#Adds a linked gap.top x axis to the active layer
		layer -new x;
		layer.x.type=xscale.choice;
		break;
	case 3:
		#Adds a linked gap.right y axis to the active layer
		layer -new y;
		layer.y.type=yscale.choice;
		break;
	case 4:
		#Adds a linked layer to the active layer
		layer -new Both;        
		layer.x.type=xscale.choice;
		layer.y.type=yscale.choice;
		break;
	}; 
	
		

[inset]
	
	if (exist(%H,11)==11) 
	{
		type -b;//BEEP!
		type -b $layer.layout;
		type -q "";//Clear Status Bar
		break 1;
	}
	switch (index)
	{
	case 1:
		layer -s page.active; /*select the active layer*/
		layer -new both; /*adds a linked layer to the active layer*/
		layer.x.showaxes=1;
		layer.y.showaxes=1;
		layer.x.showlabels=1;
		layer.y.showlabels=1;
		layer.x.type=xscale.choice;
		layer.y.type=yscale.choice;
		layer.left=75;
		layer.width=25;
		layer.height=25;
		break;

	case 2:

		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.x.showaxes=1;
		layer.y.showaxes=1;
		layer.x.showlabels=1;
		layer.y.showlabels=1;
		layer.x.type=xscale.choice;
		layer.y.type=yscale.choice;
		layer.left=75;
		layer.width=25;
		layer.height=25;
		layer -i %Z; /*include datasets in layer*/
		%Z="";
		layer -all;
		break;
	}; 	
	
	
[SwapMain]
	//Need to use v1 and v2 instead of A and B, since object.A is property
	if(swap.v1==swap.v2 || swap.v1<1 || swap.v1>page.nlayers || swap.v2<1 || swap.v2>page.nlayers )
	{
		type -b $Layer.OutofBounds;
		return 1;
	}
	loop(ii,1,page.nlayers)
	{
		if((layer$(ii).link==swap.v1 || layer$(ii).link==swap.v2) && layer$(ii).unit==7)
		{
			layer$(ii).unit=1;
		}
	}	
	
	store.v17=page.active;		//Get the current active layer
	if(%1==1)
	{
		page.active=swap.v1;	//Make Layer A active
		layer -x;		//Get the dimensions
		store.v9=v1;store.v10=v2;
		store.v11=v3;store.v12=v4;
	}		
	
	page.active=swap.v2;		//Make Layer B active
	layer -x;			//Get the dimensions
	store.v13=v1;store.v14=v2;
	store.v15=v3;store.v16=v4;

	if(%1==1)
	{
		layer store.v9 store.v10 store.v11 store.v12;	//Move layer B to A
	}
	page.active=swap.v1;		//Make Layer A active
	layer store.v13 store.v14 store.v15 store.v16;	//Move layer A to B

	page.active=store.v17;		//Restore the original active layer