% Define node group background
% The first 4 arguments are nodes that define the group borders in (left,
% top, right, bottom) order, the last argument is a text label
\usetikzlibrary{shapes,arrows}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}  % Layer order
\newcommand{\grouppadding}{0.5}
\newcommand{\group}[5]{
    \begin{pgfonlayer}{background}
        % Left top corner
        \path (#1.west |- #2.north)+(-0.7, 0.7)
            node (lefttop) {};
        % Right bottom corner
        \path (#3.east |- #4.south)+(0.7, -0.4)
            node (rightbottom) {};
        % Draw rectangle
        \path[draw, rounded corners, dashed] (lefttop) rectangle (rightbottom);
        % Text label
        \path (rightbottom.west |- lefttop.south)+(-1.2, -0.3) node {\emph{#5}};
    \end{pgfonlayer}
}
\tikzstyle{block} = [
    rectangle,
    draw=black,
    fill=white,
    thick,
    minimum height=2em,
    text centered,
    rounded corners,
    text width=6em
]
\tikzstyle{lineto} = [
    ->,
    thick,
    shorten <= 2pt,
    shorten >= 2pt
]
\tikzstyle{linefrom} = [
    <-,
    thick,
    shorten <= 2pt,
    shorten >= 2pt
]
\newcommand{\architecture}[1]{
    \begin{tikzpicture}[node distance=6em, auto]
        \node[block] (driver) {Driver};
        #1
    \end{tikzpicture}
}

\newcommand{\simplediagram}{
    \begin{figure}[H]
        \center
        \architecture{
            \node[block, dashed, below of=driver] (arch) {Architecture}
                edge[linefrom] node[right] {driver-specific messages} (driver);
            \node[block, below of=arch] {Application}
                edge[linefrom] node[right] {gestures} (arch);
        }
        \caption{Translation of driver-specific messages to gestures.}
    \end{figure}
}

\newcommand{\completediagrams}{
    \begin{figure}[H]
        \hspace{-2.3em}
        \subfigure[Architecture using a single widget. Dotted arrows represent
                   gestures, normal arrows represent events (unless labeled
                   otherwise).]{
            \architecture{
                \node[block, below of=driver] (eventserver) {Event server}
                    edge[linefrom] node[right, near end] {driver-specific messages} (driver);
                \node[block, below of=eventserver] (widget) {Widget}
                    edge[linefrom] (eventserver);
                \node[block, right of=widget, xshift=5em] (tracker) {Gesture tracker}
                    edge[lineto, dotted, bend left=10] (widget)
                    edge[linefrom, bend right=10] (widget);
                \node[block, below of=widget] {Application}
                    edge[linefrom, dotted] (widget);

                \group{eventserver}{eventserver}{tracker}{tracker}{Architecture}
            }
        }
        \hspace{1.5em}
        \subfigure[Architecture using a sub widget. Dotted arrows represent
                   gestures, normal arrows represent events (unless labeled
                   otherwise).]{
            \architecture{
                \node[block, below of=driver] (eventserver) {Event server}
                    edge[linefrom] node[right, near end] {driver-specific messages} (driver);

                \node[block, below of=eventserver] (rootwidget) {Root widget}
                    edge[linefrom] (eventserver);

                \node[block, below of=rootwidget] (subwidget) {Sub widget}
                    edge[linefrom] (rootwidget)
                    edge[lineto, bend right=45, dotted] node[right=3]
                    {propagation} (rootwidget);
                \node[block, right of=rootwidget, xshift=5em] {Gesture tracker}
                    edge[lineto, dotted, bend left=10] (rootwidget)
                    edge[linefrom, bend right=10] (rootwidget);

                \node[block, right of=subwidget, xshift=5em] (tracker) {Gesture tracker}
                    edge[lineto, dotted, bend left=10] (subwidget)
                    edge[linefrom, bend right=10] (subwidget);
                \node[block, below of=subwidget, yshift=-.5em] {Application}
                    edge[linefrom, dotted, bend left=60] (rootwidget)
                    edge[linefrom, dotted] (subwidget);

                \group{subwidget}{eventserver}{tracker}{subwidget}{Architecture}
            }
        }
    \end{figure}
}
