\RequirePackage{jbw-list}
\RequirePackage{jbw-prog-util}
\RequirePackage{pstricks}

\newcommand{\graphbarstyles}
  {%
   {fillstyle=none}%
   {fillstyle=solid,fillcolor=lightgray}%
   {fillstyle=solid,fillcolor=gray}%
   {fillstyle=vlines}%
   {fillstyle=hlines}%
   {fillstyle=crosshatch}%
   }

% ----------------------------------------------------------------------

\newcommand{\vertbargraph}[2]
  % #1 is NUM_VERT_UNITS
  % #2 is a list of type small_graph
  {\hbox
     {% Prevent array struts from being used.
      \renewcommand{\arraystretch}{0}%
      \dolist{#2}
        {}% no args
        {% ##1 is {KEY}{TICK_LIST}{RECT_LIST}
         \usetemplate{##1}
           {% ####1 is KEY
            % ####2 is TICK_LIST
            % ####3 is RECT_LIST
            \begin{tabular}[t]{@{\hspace{0.1em}}c@{\hspace{0.1em}}}%
              \vertgraphbarwithticks{####2}{####3}%
            \\%
              \normalsize
              \vrule height 2ex width 0pt depth 0.5ex\relax
              ####1%
            \end{tabular}}}
        {}% no continuation
     }}

\newcommand{\horizbargraphtabularlines}[2]
  {% #1 is LABEL
   % #2 is a list of LABELLED_TICKED_BAR
   % LABELLED_TICKED_BAR is {LABEL}{TICK_LIST}{RECT_LIST}
   \relax
   #1%
   \dolist{#2}
     {}% no initial values for loop variables
     {% ##1 is {LABEL}{TICK_LIST}{RECT_LIST}
      % ##2 is LINE_PREFIX (empty or \\
      ##2\relax
      \usetemplate{##1}
        {% ####1 is LABEL
         % ####2 is TICK_LIST
         % ####3 is RECT_LIST
         &####1&%
         \horizgraphbarwithticks{####2}{####3}}%
      \DLNext{{\\}}}
     {}% no continuation
  }

% ----------------------------------------------------------------------

\newcommand{\vertgraphbar}[2][]{\vertgraphbarwithticks{#1}{#2}}
\newcommand{\vertgraphbarwithticks}[2]
  % #1 is a list of tick positions
  % #2 is a list of rectangle top border positions
  {\jbwbg@graphbar
     {\jbwbg@vertgraphbarsteprect}
     {\jbwbg@vertgraphbarsteptick}
     {\jbwbg@vertgraphbarfinalize}
     {#1}
     {#2}}
\newcommand{\jbwbg@vertgraphbarsteprect}[3]
  % #1 is OLD_POS
  % #2 is NEW_POS
  % #3 is RECT_STYLE
  {\psframe[dimen=middle,#3](0.2,#1)(0.8,#2)}
\newcommand{\jbwbg@vertgraphbarsteptick}[1]
  % #1 is TICK_POS
  % ***TODO: augment this with TICK_STYLE and TICK_HEADS
  {\psline(0,#1)(1,#1)}
\newcommand{\jbwbg@vertgraphbarfinalize}[2]
  % #1 is COMMANDS
  % #2 is LAST_POS
  {\begin{pspicture}(0,0)(1,#2)#1\end{pspicture}}

% ----------------------------------------------------------------------

\newcommand{\splithorizbargraph}[1]
  {% #1 is a list of LINE
   % LINE is {LABEL}{BAR_1}{BAR_2}
   % BAR is a list of numbers in increasing order
   \hbox
     {\dolist{#1}
        {}% no initial values for loop variables
        {% ##1 is LINE is {LABEL}{BAR_1}{BAR_2}
         % ##2 is ACCUM (the commands accumulated for previous lines)
         % ##3 is SEP
         \usetemplate{##1}
           {% ####1 is LABEL
            % ####2 is BAR_1
            % ####3 is BAR_2
            \DLNext
              {% append commands for line to ##2 for next iteration
               {##2##3\relax####1&\revhorizgraphbar{####2}&\horizgraphbar{####3}}
               % Set SEP to \\ for all iterations but first
               {\\}}}}
        {% ##1 is ACCUM (the commands accumulated for the lines)
         \begin{tabular}
           % the complications on the next line around the \vrule are
           % necessary because the "array" package makes | in a
           % preamble cause a vrule that takes horizontal space.
           {@{}lr@{\hskip-.5\arrayrulewidth\vrule width\arrayrulewidth\hskip-.5\arrayrulewidth}l@{}}%
           ##1%
         \end{tabular}}}}

% ----------------------------------------------------------------------

\newcommand{\horizgraphbar}[2][]{\horizgraphbarwithticks{#1}{#2}}
\newcommand{\horizgraphbarwithticks}[2]
  % #1 is a list of tick positions
  % #2 is a list of rectangle right border positions
  {\jbwbg@graphbar
     {\jbwbg@horizgraphbarsteprect{}}
     {\jbwbg@horizgraphbarsteptick{}}
     {\jbwbg@horizgraphbarfinalize}
     {#1}
     {#2}}

\newcommand{\revhorizgraphbar}[2][]{\revhorizgraphbarwithticks{#1}{#2}}
\newcommand{\revhorizgraphbarwithticks}[2]
  % #1 is a list of tick positions
  % #2 is a list of rectangle right border positions
  {\jbwbg@graphbar
     {\jbwbg@horizgraphbarsteprect{-}}
     {\jbwbg@horizgraphbarsteptick{-}}
     {\jbwbg@revhorizgraphbarfinalize}
     {#1}
     {#2}}

\newcommand{\jbwbg@horizgraphbarsteprect}[4]
  % #1 is REV (either blank or -)
  % #2 is OLD_POS
  % #3 is NEW_POS
  % #4 is RECT_STYLE
  {\psframe[dimen=middle,#4](#1#2,0)(#1#3,0.6)}
\newcommand{\jbwbg@horizgraphbarsteptick}[2]
  % #1 is REV (either blank or -)
  % #2 is TICK_POS
  % ***TODO: augment this with TICK_STYLE and TICK_HEADS
  {\psline(#1#2,-0.2)(#1#2,0.8)}
\newcommand{\jbwbg@horizgraphbarfinalize}[2]
  % #1 is COMMANDS
  % #2 is LAST_POS
  {% The [] optional argument means put baseline through (0,0).
   \begin{pspicture}[](0,-0.2)(#2,0.8)#1\end{pspicture}}
\newcommand{\jbwbg@revhorizgraphbarfinalize}[2]
  % #1 is COMMANDS
  % #2 is LAST_POS
  {% The [] optional argument means put baseline through (0,0).
   \begin{pspicture}[](-#2,-0.2)(0,0.8)#1\end{pspicture}}

% ----------------------------------------------------------------------

\newcommand{\jbwbg@graphbar}[5]
  % #1 is RECT_STEP, which will be applied to {OLD_POS}{NEW_POS}{RECT_STYLE}
  % #2 is TICK_STEP, which will be applied to TICK
  % #3 is FINALIZE
  % #4 is a list {TICK_1}...{TICK_n}, which must be between 0 and POS_n
  % #5 is a list {POS_1}...{POS_n}, which must be positive and increasing
  {\expandafter
   \usetemplate\expandafter{\expandafter{\graphbarstyles}}
     {% ##1 is now the list of graph bar styles
      \ziplist{##1}{#5}
        {% ####1 is {{RECT_STYLE_1}{POS_1}}...{{RECT_STYLE_n}{POS_n}}
         \dolist{####1}
           {% ########2 will always be the previous element of the list.
            % For the first element, we pretend its previous element
            % is {}{0}.
            {{}{0}}%
            % ########3 will be the accumulated commands, which starts
            % empty, so we don't have to write anything here.
           }
           {% ########1 is {RECT_STYLE_i}{POS_i}
            % ########2 is {RECT_STYLE_{i-1}}{POS_{i-1}}
            % ########3 is COMMANDS
            \DLNext
              {% Set ########2 to the current element for next iteration.
               {########1}%
               % Accumulate another command in ########3.
               {########3\jbwbg@graphbarsteprect{#1}{########2}{########1}}}}
           {% ########2 is {RECT_STYLE_n}{POS_n}
            % ########3 is COMMANDS
            \jbwbg@graphbarC{#2}{#3}{#4}{########3}{########2}}%
         %\looplist
         %  {\jbwbg@graphbarC{#2}{#3}{#4}}
         %  {\jbwbg@graphbarsteprect{#1}}
         %  {{}{0}}
         %  {####1}%
        }}}

\newcommand{\jbwbg@graphbarsteprect}[3]
  % #1 is RECT_STEP, which will be applied to {OLD_POS}{NEW_POS}{RECT_STYLE}
  % #2 is {JUNK}{OLD_POS}
  % #3 is {RECT_STYLE}{NEW_POS}
  {\usetemplate{#2#3}
     {% ##2 is OLD_POS
      % ##3 is RECT_STYLE
      % ##4 is NEW_POS
      \ifdim##4pt<##2pt\relax\DieUglyDeathDueToPositionsNotInOrder\fi
      #1{##2}{##4}{##3}}}

\newcommand{\jbwbg@graphbarC}[5]
  % #1 is TICK_STEP, which will be applied to TICK
  % #2 is FINALIZE, to be applied to {COMMANDS_1 COMMANDS_2}{LAST_POS}
  % #3 is a list {TICK_1}...{TICK_n}, which must be between 0 and POS_n
  % #4 is COMMANDS_1
  % #5 is {JUNK_1}{LAST_POS}
  {\usetemplate{#5}
     {% ##2 is LAST_POS
      \dolist{#3}
        {}% accumulator initially empty
        {% ####1 is TICK (current list element)
         % ####2 is COMMANDS (commands accumulated so far in this loop)
         % We extend COMMANDS with TICK_STEP{TICK}.
         \DLNext{{####2#1{####1}}}}
        {% ####2 is COMMANDS_2 (the tick commands built up in this loop)
         % We invoke FINALIZE{COMMANDS_1 COMMANDS_2}{LAST_POS}.
         #2{#4####2}{##2}}%
      %\looplisttemplate
      %  {% ####1 is COMMANDS_2 (accumulated commands from this loop)
      %   % ####2 is JUNK_2
      %   #2{#4####1}{##2}}
      %  {% ####1 is JUNK_3
      %   % ####2 is TICK (current list element)
      %   #1{####2}}
      %  {}
      %  {#3}%
     }}

% ----------------------------------------------------------------------

% \newcommand{\jbwbg@graphbarOld}[5]
%   % #1 is RECT_STEP, which will be applied to {OLD_POS}{NEW_POS}{RECT_STYLE}
%   % #2 is TICK_STEP, which will be applied to TICK
%   % #3 is FINALIZE
%   % #4 is a list {TICK_1}...{TICK_n}, which must be between 0 and POS_n
%   % #5 is a list {POS_1}...{POS_n}, which must be positive and increasing
%   {\expandafter
%    \usetemplate\expandafter{\expandafter{\graphbarstyles}}
%      {% ##1 is now the list of graph bar styles
%       \ziplist{##1}{#5}{\jbwbg@graphbarB{#1}{#2}{#3}{#4}{####1}}}}
% \newcommand{\jbwbg@graphbarB}[5]
%   % #1 is RECT_STEP, which will be applied to {OLD_POS}{NEW_POS}{RECT_STYLE}
%   % #2 is TICK_STEP, which will be applied to TICK
%   % #3 is FINALIZE
%   % #4 is a list {TICK_1}...{TICK_n}, which must be between 0 and POS_n
%   % #5 is a list {{RECT_STYLE_1}{POS_1}}...{{RECT_STYLE_n}{POS_n}}
%   {\looplist
%      {\jbwbg@graphbarC{#2}{#3}{#4}}
%      {\jbwbg@graphbarsteprect{#1}}
%      {{}{0}}
%      {#5}}

% \newcommand{\jbwbg@graphbarCold}[5]
%   % #1 is TICK_STEP, which will be applied to TICK
%   % #2 is FINALIZE
%   % #3 is a list {TICK_1}...{TICK_n}, which must be between 0 and POS_n
%   % #4 is COMMANDS
%   % #5 is {JUNK}{LAST_POS}
%   {\usetemplate{#5}
%      {% ##2 is LAST_POS
%       \looplist{\jbwbg@graphbarfinalize{#2}{##2}{#4}}
%                {\jbwbg@graphbarsteptick{#1}}
%                {}
%                {#3}}}
% \newcommand{\jbwbg@graphbarsteptick}[3]
%   % #1 is TICK_STEP, which will be applied to TICK
%   % #2 is junk (the previous tick)
%   % #3 is TICK
%   {#1{#3}}
% \newcommand{\jbwbg@graphbarfinalize}[5]
%   % #1 is FINALIZE, to be applied to {COMMANDS_1 COMMANDS_2}{LAST_POS}
%   % #2 is LAST_POS
%   % #3 is COMMANDS_1
%   % #4 is COMMANDS_2
%   % #5 is junk (the last tick)
%   {#1{#3#4}{#2}}

%\newcommand{\vertgraphbar}[1]
%  % #1 is a list of positions
%  {\expandafter\ziplist\expandafter{\graphbarstyles}{#1}{\jbwbg@graphbaraux}{0}{}}
%\newcommand{\jbwbg@graphbaraux}[3]
%  % #1 is a list of elements {{RECT_STYLE}{POSITION}}
%  % #2 is the last position
%  % #3 is the accumulated picture commands
%  {\listcase{#1}
%     {\jbwbg@graphbarfinalize{#2}{#3}}
%     {\jbwbg@graphbarstep{#2}{#3}}}
%\newcommand{\jbwbg@graphbarstep}[4]
%  % #1 is the last position
%  % #2 is the accumulated picture commands
%  % #3 is a pair {{RECT_STYLE}{POSITION}}
%  % #4 is a list of elements {{RECT_STYLE}{POSITION}}
%  {\jbwbg@graphbarstepaux{#1}{#2}#3{#4}}
%\newcommand{\jbwbg@graphbarstepaux}[5]
%  % #1 is the last position
%  % #2 is the accumulated picture commands
%  % #3 is the fill style
%  % #4 is the position
%  % #5 is a list of elements {{RECT_STYLE}{POSITION}}
%  {\jbwbg@graphbaraux{#5}{#4}
%     {#2%
%      \psframe[#3](0,#1)(1,#4)}}
%\newcommand{\jbwbg@graphbarfinalize}[2]
%  % #1 is the last position
%  % #2 is the accumulated picture commands
%  {\begin{pspicture}(0,0)(1,#1)%
%     #2%
%   \end{pspicture}}

% Local variables:
% tex-main-file: "jbw-bar-graph-test.tex"
% end:
