% A simple library for signal flow diagrams
% based on the pgf/tikz package of Till Tantau
%
% Author: Dr. Karlheinz Ochs, Ruhr-University of Bochum, Germany
% Version: 0.1
% Date: 2007/01/05
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{tikz}
\ProvidesPackage{signalflowdiagram}
              [2006/12/30 v0.1 Signal flow diagrams]

%
% Libraries for signal flow diagrams.
%
\usetikzlibrary{signalflowdiagram,signalflowarrows,signalflowoperators,signalflowblocks}


%
% Colors
%
\newcommand{\pathdrawcolor}{\blockdrawcolor}     % draw color of signal paths
\newcommand{\pathfillcolor}{\blockfillcolor}     % fill color of signal paths
\newcommand{\operatordrawcolor}{\blockdrawcolor} % draw color of operators
\newcommand{\operatorfillcolor}{\blockfillcolor} % fill color of operators
\newcommand{\blockdrawcolor}{black}              % draw color of blocks
\newcommand{\blockfillcolor}{white}              % fill color of blocks

%
% line widths of
%
\newcommand{\diagramlinewidth}{0.5mm}  % signal flow diagram frames
\newcommand{\pathlinewidth}{0.3mm}     % signal paths
\newcommand{\operatorlinewidth}{0.3mm} % operator frames
\newcommand{\blocklinewidth}{0.5mm}    % building block styles

%
% line widths, sizes, etc.
%
\newcommand{\nodesize}{1.5mm}         % size of nodes
\newcommand{\terminalsize}{\nodesize} % size of terminals
\newcommand{\operatorsize}{3mm}       % size of circular shaped operator symbols
\newcommand{\delaysize}{6mm}          % minimum size of delay elements
\newcommand{\blockwidth}{24mm}        % minimum width of blocks
\newcommand{\blockheight}{12mm}       % minimum height of blocks
\newcommand{\blocktextwidth}{22mm}    % minimum text width of blocks
\newcommand{\filterwidth}{12mm}       % minimum width of filters
\newcommand{\filterheight}{8mm}       % minimum height of filters
\newcommand{\filtertextwidth}{6mm}    % minimum text width of filters

%
% misc.
%
\newcommand{\pathlineextend}{0.025mm} % shortens the end of the signal paths
\newcommand{\vbullets}[1]{% vertical bullets
\node[rectangle,anchor=center,at=(#1),text width=2ex]{%
$\bullet$\\[-1ex] $\bullet$\\[-1ex] $\bullet$}
}%


%
% Signal flow diagram.
%
% #1 = optional parameter(s)
% #2 = title text
\tikzstyle{signal flow diagram} =
  [rectangle,
   sharp corners,
   line width=\diagramlinewidth,
   draw=black,
   inner sep=3mm]
\newenvironment{signalflow}[2][]{%
\begin{center}
  \begin{tikzpicture}
    \node[% title
      signal flow diagram,
      fill=blue!90!black,
      text=white] (title) {#2};% title text
    \node at (title.south west) [%
      signal flow diagram,
      fill=blue!20!white,
      anchor=north west,
      yshift=\pgflinewidth]
    \bgroup
      \begin{minipage}{0.9\textwidth}
        \centering
        \begin{tikzpicture}[%
          node distance=8mm,
          pin distance=8mm,
          label distance=-0.5mm,
          inner sep=1mm,
          anchor=center,
          text centered,
          #1]% optional parameter(s)
}{%
        \end{tikzpicture}
      \end{minipage}
    \egroup;
  \end{tikzpicture}
\end{center}
}%


%
% Positioning of node text.
%
% #1 = node label
% #2 = label text
\newcommand{\textaboveof}[2]{\pgftext[bottom,at=\pgfpointanchor{#1}{north},y=+1mm]{#2}}%
\newcommand{\textrightof}[2]{\pgftext[left,  at=\pgfpointanchor{#1}{east}, x=+1mm]{#2}}%
\newcommand{\textbelowof}[2]{\pgftext[top   ,at=\pgfpointanchor{#1}{south},y=-1mm]{#2}}%
\newcommand{\textleftof} [2]{\pgftext[right, at=\pgfpointanchor{#1}{west}, x=-1mm]{#2}}%

\makeatletter
\newcounter{r}
\newcommand{\tikzgrid}{%
  \pgfsetxvec{\pgfpoint{\tikz@node@distance}{0mm}}%
  \pgfsetyvec{\pgfpoint{0mm}{\tikz@node@distance}}%
  \tikz@matrix%
}
\newcommand{\tikz@matrix}[1]{\tikz@@matrix#1@}%
\def\tikz@@matrix#1@{\do@rows#1\\@\\}%
\def\do@rows#1\\{%
  \ifx#1@%
  \else%
    \setcounter{r}{0}%
    \do@columns#1&@&%
    \pgftransformshift{\pgfpointxy{-\ther}{-1}}%
    \expandafter\do@rows%
  \fi}%
\def\do@columns#1&{%
  \if#1@%
  \else%
    \stepcounter{r}%
    \pgftransformshift{\pgfpointxy{1}{0}}%
    #1;%
    \expandafter\do@columns%
  \fi}%
\makeatother