% \iffalse
% --------------------------------------------------------------------
% <*utils>
% \fi
% 
% \subsubsection{Other Tikz utilities}
% 
% \begin{TikzKey}{tikz/reverseclip}
%   
%   A reverse clipping path.  This is used to cut out stuff outside of
%   path defined.
%   
%    \begin{macrocode}
\tikzstyle{reverseclip}=[insert path={(current bounding box.north east) --
  (current bounding box.south east) --
  (current bounding box.south west) --
  (current bounding box.north west) --
  (current bounding box.north east)}]
%    \end{macrocode}
% \end{TikzKey}
% 
% \begin{TikzKey}{tikz/clip even odd rule}
%   A reverse clipping path
%
%    \begin{macrocode}
\tikzset{
  clip even odd rule/.code={\pgfseteorule}, % Credit to Andrew Stacey
}
%    \end{macrocode}
% \end{TikzKey}
% 
%
% \begin{TikzKey}{tikz/invclip}
%
% Inverse clipping.  This should be an option \emph{after} the path to
% do the inverse clipping by.  This works by adding a \emph{large}
% (page) path to the current path, and then use that as clipping.
% 
%    \begin{macrocode}
\tikzset{
  invclip/.style={
    clip,insert path=
    [clip even odd rule]{
      [reset cm](-\maxdimen,-\maxdimen)rectangle(\maxdimen,\maxdimen)
    }
  },
}
%    \end{macrocode}
% \end{TikzKey}
%
% \begin{TikzKey}{save clip}
%
%   An option for use with sub-elements of NATO App 6(c) or chit
%   nodes.  This will save the current path as a clipping path for the
%   next paths to be drawn in the sub-element
%
%    \begin{macrocode}
\newif\ifwg@s@ve\wg@s@vefalse
\tikzset{
  save clip/.is choice,
  save clip/true/.code={\global\wg@s@vetrue},
  save clip/false/.code={\global\wg@s@vefalse},
  save clip/.default={true},
  save clip/.initial={false},
}
%    \end{macrocode}
% \end{TikzKey}
%
% \begin{TikzKey}{scale line widths}
%
%   Scales any line width specified in the node options.
%
%   Use like
%
% \begin{verbatim}
%    \tikzset{
%      some/.style={
%        scale line widths,
%        line width=1pt}
%    }
% \end{verbatim}
%
% Note that the order is important.
%   
%    \begin{macrocode}
%      Save pgf rounded corners macro
%      \let\wg@pgfsetcornersarced\pgfsetcornersarced
\def\wg@setcornersarched#1{%
  \def\arg{#1}%
  \let\isarched\relax%   Cannot set \ifpgf@arccorners directly inside
                     %   other \if
  \ifx\arg\@empty\else%
    \edef\pgf@corner@arc{{#1}{#1}}%
    \let\isarched\pgf@arccornerstrue%
    \ifdim#1=0pt%
      \let\isarched\pgf@arccornersfalse%
    \fi%
  \fi%
  \isarched}
\newdimen\wg@lw@scaled\wg@lw@scaled=1pt
\def\wg@getscale{%
  \pgfgettransformentries{%
    \wg@jaca}{%
    \wg@jacb}{%
    \wg@jacc}{%
    \wg@jacd}{%
    \wg@tmp}{%
    \wg@tmp}%
  \pgfmathsetmacro{\wg@jac}{sqrt(abs(\wg@jaca*\wg@jacd-\wg@jacb*\wg@jacc))}%
  \wg@dbg{4}{Scale is \wg@jac}
  \xdef\wg@scale{\wg@jac}}
\def\wg@scaled#1{%
  \wg@getscale%
  \wg@dbg{4}{Scaling #1 by \wg@scale}
  \pgfmathsetmacro{\wg@tmp}{\wg@scale*#1}%
  \xdef\wg@tmp{\wg@tmp}%
  \xdef\wg@lw@scale{\wg@tmp}%
  \wg@dbg{4}{Scaled #1 -> \wg@tmp}}
%% \message{^^JRounded corners: \meaning\pgfsetcornersarced}
\tikzset{
  %% Get current scale and store in \wg@scale
  get scale/.code={\wg@getscale},
  scale line widths/.style={%
    /utils/exec=\def\tikz@semiaddlinewidth##1{%
      \wg@scaled{##1}
      \wg@lw@scaled=\wg@tmp pt
      \tikz@addoption{\pgfsetlinewidth{\wg@lw@scaled}}%
      \wg@dbg{4}{Added scaled option \wg@tmp}
      \pgfmathsetlength\pgflinewidth{\wg@tmp pt}
      \wg@dbg{4}{Did set line width \wg@tmp pt}
    }
  },
  scale rounded corners/.style={%
    /utils/exec=\def\pgfsetcornersarced##1{%
      \pgf@process{##1}%
      \pgf@xa=\pgf@x%
      \wg@scaled{\the\pgf@xa}%
      % \tikz@addoption{\wg@setcornersarched{\wg@tmp pt}}%
      \wg@dbg{4}{Scaled rounded corners: \the\pgf@xa -> \wg@tmp}%
      \wg@setcornersarched{\wg@tmp pt}%
    }
  },
  relative line width/.style={%
    /utils/exec=\def\tikz@semiaddlinewidth##1{%
      \wg@dbg{4}{Relative line width #1 times ##1}%
      \pgfmathsetmacro{\wg@lv}{#1*##1}%
      \tikz@addoption{\pgfsetlinewidth{\wg@lw pt}}%
      \pgfmathsetlength\pgflinewidth{\wg@lw pt}}}
}
%    \end{macrocode}
% \end{TikzKey}
%
% \begin{TikzKey}{sub pic actions}
%
%   This is key that propagates actions to sub pictures of pictures.
%   The normal \texttt{pic actions} cannot be used as it causes an
%   infinite loop.
%
%    \begin{macrocode}
\tikzset{
  sub pic actions/.code={%
    \tikz@picmode%
    \edef\opts{%
      \iftikz@mode@draw draw,\else draw=none,\fi 
      \iftikz@mode@fill fill\else fill=none\fi}
    \wg@dbg{5}{^^JSub Mode: \meaning\tikz@picmode  \meaning\opts}
    \pgfset{/tikz/.cd}
    \pgfkeysalsofrom{\opts}
  }}
%    \end{macrocode}
% \end{TikzKey}
% 
% \begin{TikzKey}{wg/debug show}
%
%   Show debugging information
%   
%    \begin{macrocode}
\tikzset{
  wg/debug show/.code={%
    \extractcolorspec{pgfstrokecolor}{\wg@tmp@fg}
    \def\wg@tmp@bg{none}
    \@ifundefinedcolor{pgffillcolor}{}{
      \extractcolorspec{pgffillcolor}{\wg@tmp@bg}}
    \begingroup
    \tikz@mode
    \wargamedbglvl=#1
    \wg@dbg{3}{Drawing with w/stroke `\wg@tmp@fg'
      (\tikz@strokecolor,\iftikz@mode@draw\else not\space\fi drawing)
      and fill `\wg@tmp@bg' (\tikz@fillcolor,\iftikz@mode@fill\else
      not\space\fi  filling)}
    \endgroup
  }
}
%    \end{macrocode}
% \end{TikzKey}
%
% 
% \iffalse
% --------------------------------------------------------------------
% </utils>
% \fi
