%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% This file is a copy of some part of PGF/Tikz.
%%% It has been copied here to provide :
%%%  - compatibility with older PGF versions
%%%  - availability of PGF contributions by Christian Feuersaenger
%%%    which are necessary or helpful for pgfplots.
%%%
%%% For reasons of simplicity, I have copied the whole file, including own contributions AND
%%% PGF parts. The copyrights are as they appear in PGF.
%%%
%%% Note that pgfplots has compatible licenses.
%%% 
%%% This copy has been modified in the following ways:
%%%  - nested \input commands have been updated
%%%  
%
% Support for the contents of this file will NOT be done by the PGF/TikZ team. 
% Please contact the author and/or maintainer of pgfplots (Christian Feuersaenger) if you need assistance in conjunction
% with the deployment of this patch or partial content of PGF. Note that the author and/or maintainer of pgfplots has no obligation to fix anything:
% This file comes without any warranty as the rest of pgfplots; there is no obligation for help.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Date of this copy: Di 25. Dez 18:14:20 CET 2012 %%%



% Copyright 2006 by Till Tantau
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\ProvidesFileRCS $Header: /cvsroot/pgf/pgf/generic/pgf/basiclayer/pgfcorelayers.code.tex,v 1.5 2012/11/07 19:23:16 ludewich Exp $


% Creates a new pgf layer
%
% #1 = layer name
%
% Declares a new layer for pgf.
%
% Example:
%
% \pgfdeclarelayer{background}

\def\pgfdeclarelayer#1{%
  \pgfutil@ifundefined{pgf@layerbox@#1}{%
	  \expandafter\expandafter\csname pgf@newbox\endcsname\csname pgf@layerbox@#1\endcsname%
	  \expandafter\expandafter\csname pgf@newbox\endcsname\csname pgf@layerboxsaved@#1\endcsname%
  }{}%
}
\let\pgf@newbox=\newbox % avoid plain TeX outer problem

% Sets the layers that compose the picture
%
% #1 = List of layers
%
% Description:
%
% Sets the list of layers that make up the picture. The layers will be
% put on top of each other in the order given. 
%
% This command can also be given inside of a picture in which case it
% applies only to that very picture.
%
% Example:
%
% \pgfsetlayers{background,main}

\def\pgfsetlayers#1{\edef\pgf@layerlist{#1}}
\pgfsetlayers{main}

% Adds code to a layer
%
% #1 = layer name
%
% Note:
%
% You cannot add anything to the ``main'' layer using this command.
%
% Example:
%
% \begin{pgfonlayer}{background}
%   \fill[red] (0,0) -- (1,1);
% \end{pgfonlayer}

\def\pgfonlayer@name{main}

\def\pgfonlayer#1{%
  \pgfutil@ifundefined{pgf@layerbox@#1}{%
     \PackageError{pgf}{Sorry, the requested layer '#1' could not be found. Maybe you misspelled it?}{}%
	 \bgroup
	 	\begingroup
  }{%
      \begingroup
      \edef\pgf@temp{#1}%
	  \ifx\pgf@temp\pgfonlayer@name
	    % we are already on this layer.
		\def\pgf@temp{%
			\bgroup
			   \begingroup
		}%
      \else
		\let\pgfonlayer@name=\pgf@temp
		\pgfonlayer@assert@is@active
	  	\def\pgf@temp{%
			 \expandafter\global\expandafter%
			 \setbox\csname pgf@layerbox@#1\endcsname=\hbox to 0pt%
				\bgroup%
				  \expandafter\box\csname pgf@layerbox@#1\endcsname%
				  \begingroup%
		}%
	 \fi
	 \pgf@temp
  }%
}
\def\endpgfonlayer{%
		  \endgroup%
		  \hss
		\egroup%
	\endgroup
}

\let\startpgfonlayer=\pgfonlayer
\let\stoppgfonlayer=\endpgfonlayer

\def\pgfdiscardlayername{discard}

\def\pgfonlayer@assert@is@active{%
  \ifx\pgfonlayer@name\pgfdiscardlayername
     % this special layer name can be used as /dev/null without
	 % warning.
  \else
	  \begingroup
	  \def\pgfonlayer@isactive{0}%
	  \expandafter\pgf@assert@layer@is@active@loop\pgf@layerlist,,\relax%
	  \if0\pgfonlayer@isactive
	     \pgfonlayer@assert@fail
	  \fi
	  \endgroup
  \fi
}%
\def\pgfonlayer@assert@fail{%
	\PackageError{pgf}{Sorry, the requested layer '\pgfonlayer@name' is not 
	 part of the layer list. Please verify that you provided 
	 \string\pgfsetlayers\space and that '\pgfonlayer@name' is part of this list}{}%
}%
\def\pgf@assert@layer@is@active@loop#1,#2,\relax{%
  \edef\pgf@test{#1}%
  \ifx\pgf@test\pgfonlayer@name
 	  \def\pgfonlayer@isactive{1}%
  \else
	  \def\pgf@test{#2}%
	  \ifx\pgf@test\pgfutil@empty%
	  \else%
		\pgf@assert@layer@is@active@loop#2,\relax%
	  \fi%
  \fi
}

% Hooks into the scoping:

\def\pgf@insertlayers{%
  \expandafter\pgf@dolayer\pgf@layerlist,,\relax%
}
\def\pgf@maintext{main}%
\def\pgf@dolayer#1,#2,\relax{%
  \def\pgf@test{#1}%
  \ifx\pgf@test\pgf@maintext%
    \box\pgf@layerbox@main%
  \else%
    \pgfsys@beginscope%
      \expandafter\box\csname pgf@layerbox@#1\endcsname%
    \pgfsys@endscope%
  \fi%
  \def\pgf@test{#2}%
  \ifx\pgf@test\pgfutil@empty%
  \else%
    \pgf@dolayer#2,\relax%
  \fi%
}

\def\pgf@savelayers{%
  \expandafter\pgf@dosavelayer\pgf@layerlist,,\relax%
}
\def\pgf@dosavelayer#1,#2,\relax{%
  \def\pgf@test{#1}%
  \ifx\pgf@test\pgf@maintext%
  \else%
    \setbox\csname pgf@layerboxsaved@#1\endcsname=\box\csname pgf@layerbox@#1\endcsname%
  \fi%
  \def\pgf@test{#2}%
  \ifx\pgf@test\pgfutil@empty%
  \else%
    \pgf@dosavelayer#2,\relax%
  \fi%
}

\def\pgf@restorelayers{%
  \expandafter\pgf@dorestorelayer\pgf@layerlist,,\relax%
}
\def\pgf@dorestorelayer#1,#2,\relax{%
  \def\pgf@test{#1}%
  \ifx\pgf@test\pgf@maintext%
  \else%
    \global\setbox\csname pgf@layerbox@#1\endcsname=\box\csname pgf@layerboxsaved@#1\endcsname%
  \fi%
  \def\pgf@test{#2}%
  \ifx\pgf@test\pgfutil@empty%
  \else%
    \pgf@dorestorelayer#2,\relax%
  \fi%
}

\endinput
