%D \module
%D   [       file=m-chart,
%D        version=1998.10.10,
%D          title=\CONTEXT\ Modules,
%D       subtitle=Flow Charts,
%D         author={Hans Hagen \& Ton Otten},
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for 
%C details. 

%D This is an experimental module. Pieces of code will be moved
%D to other modules. More features are possible but will be 
%D interfaces later. 
%D
%D When finished this module will be documented. The main macro 
%D is a rather big one. I'm not sure if splitting it up is wise.

% The 1pt offset is due to 'error' in pdftex form placement, 
% version 14a+ will be ok. This needs checking.

% arrow, dash
% crossing 
% \goto -> \normalgoto
% class -> class:name (ref prefix)
% c, automatisch geen overlap zoeken
% eind eerder chart connecties
% relateren aan korps
% check op bestaan naam, bestaan shape
% auto als extern figuur
% subchart
% pijlen
% focus
% ook nog \MPmessage
% areapath -> krappe vlak
% clippath -> gehele vlak
%
% offset  : clip offset
% breedte : breedte cel
% hoogte  : hoogte cel
% dx      : halve afstand in breedte (grid breedte = breedte + 2dx)
% dy      : halve afstand in hoogte (grid hoogte = hoogte + 2dy)
% x       : x offset (clipping)
% y       : y offset (clipping)
% nx      : minimaal aantal cellen horizontaal
% ny      : minimaal aantal cellen vertikaal

% kaderkleur achtergrondkleur
% lijnkleur lijndikte
% focus focuskaderkleur focusachtergrondkleur
% richting
%
% focus koppelen aan kleur

\unprotect

\def\setFLOWname#1#2%
  {\bgroup
   \lccode`0=`a\lccode`1=`b\lccode`2=`c\lccode`3=`d\lccode`4=`e%
   \lccode`5=`f\lccode`6=`g\lccode`7=`h\lccode`8=`i\lccode`9=`j%
   \lccode` =`\_\lccode`-=`\_\lccode`_=`\_%
   \lowercase{\gdef#1{#2}}%
   \egroup}

\def\resetFLOWcell%
  {\global\let\FLOWname       \empty
   \global\let\FLOWalign      \empty
   \global\let\FLOWshape      \empty
   \global\let\FLOWlocation   \empty
   \global\let\FLOWtext       \empty
   \global\let\FLOWhelp       \empty
   \global\let\FLOWdestination\empty
   \global\let\FLOWoverlay    \empty
   \global\let\FLOWfocus      \empty
   \global\let\tFLOWlabel     \empty
   \global\let\bFLOWlabel     \empty
   \global\let\bcFLOWlabel    \empty
   \global\let\lFLOWlabel     \empty
   \global\let\rFLOWlabel     \empty
   \def\name           ##1{\def\FLOWcell{##1}\setFLOWname\FLOWname{name_##1}\ignorespaces}%
   \def\shape          ##1{\gdef\FLOWshape{##1}\ignorespaces}%
   \def\destination    ##1{\gdef\FLOWdestination{##1}\ignorespaces}%
   \def\location       ##1{\setFLOWlocation##1\end\ignorespaces}%
   \def\focus          ##1{\gdef\FLOWfocus{##1}\ignorespaces}%
   \def\overlay        ##1{\gdef\FLOWoverlay{##1}\ignorespaces}%
   \def\figure         ##1{\defineoverlay
                             [dummy]
                             [{\externalfigure
                                 [##1]
                                 [\c!breedte=\overlaywidth,
                                  \c!hoogte=\overlayheight]}]%
                           \overlay{dummy}}%
   \def\dotext    [##1]##2{\gdef\FLOWalign{##1}\gdef\FLOWtext{##2}}%
   \def\text              {\dosingleempty\dotext}%
   \def\comment   [##1]##2{\ignorespaces\dogobblesingleempty}%
   \def\label     [##1]##2{\setgvalue{##1FLOWlabel}{##2}\ignorespaces}%
   \def\help           ##1{\gdef\FLOWhelp{##1}\ignorespaces}%
   \def\connection[##1]##2{\ignorespaces}%
   \def\connect           {\connection}%
   \def\locate            {\location}}

\def\startFLOWchart%
  {\bgroup
   \let\stopFLOWchart\egroup
   \obeylines              % lelijk, buffers nog eens fatsoeneren
   \dodoubleempty\dostartFLOWchart}

\def\dostartFLOWchart[#1][#2]%
  {\doglobal\increment\nofFLOWcharts
   \setxvalue{FLOW-#1}%
     {\noexpand\dosetFLOWchart[\nofFLOWcharts][#2]}%
   \dostartbuffer[flw-\nofFLOWcharts][startFLOWchart][stopFLOWchart]}

\def\setupFLOWcharts%
  {\dodoubleargument\getparameters[@@FLOW]}

\def\setupFLOWlines%
  {\dodoubleargument\getparameters[@@FLOL]}

\def\setupFLOWshapes%
  {\dodoubleargument\getparameters[@@FLOS]}

\def\setupFLOWfocus%
  {\dodoubleargument\getparameters[@@FLOF]}

\setupFLOWcharts
  [\c!optie=,
   \c!punt=,  % private option
   \c!breedte=12\bodyfontsize,
   \c!hoogte=7\bodyfontsize,
   \c!maxbreedte=,
   \c!maxhoogte=,
   \c!offset=0pt, % auto offset: .5\bodyfontsize,
   \c!dx=2\bodyfontsize,
   \c!dy=2\bodyfontsize,
   \c!nx=0, % 1,
   \c!ny=0, % 1,
   \c!x=1,
   \c!y=1,
   \c!autofocus=,
   \c!focus=,
   \c!achtergrond=,      % \v!kleur,
   \c!achtergrondkleur=white,
   \c!kader=\v!uit,
   \c!kaderkleur=]

\setupFLOWlines
  [\c!hoek=\v!rond,
   \c!pijl=\v!ja,
   \c!streep=\v!nee,
   \c!straal=.375\bodyfontsize,      % 2.5\c!lijndikte
   \c!kleur=FLOWlinecolor,
   \c!lijndikte=.15\bodyfontsize,  % 2pt,
   \c!offset=\v!geen]

\setupFLOWshapes
  [\c!default=action,
   \c!kaderkleur=FLOWframecolor,
   \c!achtergrond=\v!kleur,
   \c!achtergrondkleur=FLOWbackgroundcolor,
   \c!achtergrondraster=\@@rsraster,
   \c!lijndikte=.15\bodyfontsize,  % 2pt,
   \c!offset=.5\bodyfontsize]

\setupFLOWfocus
  [\c!kaderkleur=FLOWfocuscolor,
   \c!achtergrond=\@@FLOSachtergrond,
   \c!achtergrondkleur=\@@FLOSachtergrondkleur,
   \c!achtergrondraster=\@@FLOSachtergrondraster,
   \c!lijndikte=\@@FLOSlijndikte,
   \c!offset=\@@FLOSoffset]

\definecolor[FLOWfocuscolor]      [s=.2]
\definecolor[FLOWlinecolor]       [s=.5]
\definecolor[FLOWframecolor]      [s=.7]
\definecolor[FLOWbackgroundcolor] [s=.9]

\newcounter\includeFLOWx
\newcounter\includeFLOWy

\def\includeFLOWchart%
  {\dodoubleempty\doincludeFLOWchart}

\def\doincludeFLOWchart[#1][#2]%
  {\pushmacro\includeFLOWx
   \pushmacro\includeFLOWy
   \getparameters[FLOWi][x=1,y=1,#2]%
   \increment(\includeFLOWx,0\FLOWix)%
   \decrement(\includeFLOWx,1)%
   \increment(\includeFLOWy,0\FLOWiy)%
   \decrement(\includeFLOWy,1)%
   \def\dodoincludeFLOWchart##1%
     {\doifdefined{FLOW-##1}
        {\pushmacro\dosetFLOWchart
         \def\dosetFLOWchart[####1][####2]%
           {\popmacro\dosetFLOWchart
            \haalbuffer[flw-####1]}%
         \getvalue{FLOW-##1}}}%
   \processcommalist[#1]\dodoincludeFLOWchart
   \popmacro\includeFLOWx
   \popmacro\includeFLOWy}

\def\setFLOWlocation#1,#2\end%
 {\scratchcounter=0#1\advance\scratchcounter\includeFLOWx
  \xdef\FLOWlocation{\the\scratchcounter}%
  \scratchcounter=0#2\advance\scratchcounter\includeFLOWy
  \xdef\FLOWlocation{\FLOWlocation,\the\scratchcounter}}

\def\FLOWshapes%
  {node, action, procedure, product, decision, archive,
   loop, wait, subprocedure, singledocument, multidocument,
   sub procedure, single document, multi document, up, down,
   left, right}

\def\FLOWlines%
  {up, down, left, right} 

\def\FLOWsetconnect#1%
  {\donefalse
   \let\cFLOWfrom\empty
   \let\cFLOWto\empty
   \def\zFLOWfrom{0}%
   \def\zFLOWto{0}%
   \handletokens#1\with\doFLOWsetconnect
   \ifx\cFLOWto\empty\let\cFLOWfrom\empty\fi}

\def\doFLOWsetconnect#1%
  {\ifx     #1p%
     \ifdone\def\zFLOWto{+1}\else\def\zFLOWfrom{+1}\fi
   \else\ifx#1+%
     \ifdone\def\zFLOWto{+1}\else\def\zFLOWfrom{+1}\fi
   \else\ifx#1n%
     \ifdone\def\zFLOWto{-1}\else\def\zFLOWfrom{-1}\fi
   \else\ifx#1-%
     \ifdone\def\zFLOWto{-1}\else\def\zFLOWfrom{-1}\fi
   \else\ifdone
     \edef\cFLOWto{\FLOWconnector#1}%
   \else
     \edef\cFLOWfrom{\FLOWconnector#1}%
     \donetrue
   \fi\fi\fi\fi\fi}

\def\FLOWconnector#1%
  {\if#1bbottom\else\if#1ttop\else\if#1lleft\else\if#1rright\fi\fi\fi\fi}

\newif\ifFLOWscaling \FLOWscalingtrue

\def\getFLOWchart%
  {\dodoubleempty\dogetFLOWchart}

\def\dogetFLOWchart[#1][#2]%
  {\doifundefinedelse{FLOW-#1}
     {\writestatus{FLOW}{unknown chart #1}%
      \framed
        [\c!breedte=12\bodyfontsize,\c!hoogte=8\bodyfontsize]
        {\tttf [chart #1]}}
     {\dodogetFLOWchart[#1][#2]}}

\def\dodogetFLOWchart[#1][#2]%
  {\bgroup
\forgetall
\offinterlineskip
   \def\dosetFLOWchart[##1][##2]%
     {\def\currentFLOWnumber{##1}%
      \getparameters[@@FLOW][##2]}%
   \getvalue{FLOW-#1}%
   \getparameters[@@FLOW][#2]% dubbelop ?
   \doifsomething{\@@FLOWautofocus}
     {\def\@@FLOWminx{100}\let\@@FLOWminy\@@FLOWminx
      \def\@@FLOWmaxx  {0}\let\@@FLOWmaxy\@@FLOWmaxx
      \def\@@FLOWabsx  {0}\let\@@FLOWabsy\@@FLOWabsx
      \def\startFLOWcell%
        {\resetFLOWcell}%
      \def\dodolocation##1##2##3##4%
        {\ifnum##1##2##4\relax
           \!!counta=##1\advance\!!counta by ##31\relax
           \edef##4{\ifnum\!!counta<1 1\else\the\!!counta\fi}%
         \fi}%
      \def\dolocation##1,##2\end%
        {\ifnum##1>\@@FLOWabsx\def\@@FLOWabsx{##1}\fi
         \ifnum##2>\@@FLOWabsy\def\@@FLOWabsy{##2}\fi
         \ExpandBothAfter\doifinset{\FLOWcell}{\@@FLOWautofocus}
           {\dodolocation{##1}<-\@@FLOWminx
            \dodolocation{##1}>+\@@FLOWmaxx
            \dodolocation{##2}<-\@@FLOWminy
            \dodolocation{##2}>+\@@FLOWmaxy}}%
      \def\stopFLOWcell%
        {\expandafter\dolocation\FLOWlocation\end}%
      \haalbuffer[flw-\currentFLOWnumber]%
     %\message{AUTOSHAPE 1: (\@@FLOWminx,\@@FLOWminy)->(\@@FLOWmaxx,\@@FLOWmaxy)}%
      \ifnum\@@FLOWabsx<\@@FLOWmaxx\let\@@FLOWmaxx\@@FLOWabsx\fi
      \ifnum\@@FLOWabsy<\@@FLOWmaxy\let\@@FLOWmaxy\@@FLOWabsy\fi
     %\message{AUTOSHAPE 2: (\@@FLOWminx,\@@FLOWminy)->(\@@FLOWmaxx,\@@FLOWmaxy)}%
      \donetrue
      \ifnum\@@FLOWminx=100 \donefalse\fi
      \ifnum\@@FLOWminy=100 \donefalse\fi
      \ifnum\@@FLOWmaxx=0   \donefalse\fi
      \ifnum\@@FLOWmaxy=0   \donefalse\fi
      \def\do##1##2##3##4%
        {\ifdone
           \let##1=##2%
           \!!counta=##3%
           \advance\!!counta 1\advance\!!counta -##2\relax
           \ifnum\!!counta<1 \!!counta=1 \fi
           \edef##4{\the\!!counta}%
         \else
           \def##1{1}\def##4{0}%   {1}%
         \fi}%
      \do\@@FLOWx\@@FLOWminx\@@FLOWmaxx\@@FLOWnx
      \do\@@FLOWy\@@FLOWminy\@@FLOWmaxy\@@FLOWny}%
  %\message{AUTOSHAPE 3: (\@@FLOWx,\@@FLOWy)->(\@@FLOWnx,\@@FLOWny)}\wait
   \let\FLOWwidth \@@FLOWnx
   \let\FLOWheight\@@FLOWny
   \def\getFLOWlocation##1,##2\end%
     {\ifnum0##1>\FLOWwidth \edef\FLOWwidth {##1}\fi
      \ifnum0##2>\FLOWheight\edef\FLOWheight{##2}\fi}%
   \long\def\startFLOWcell##1\stopFLOWcell%
     {\resetFLOWcell
      \ignorespaces##1\unskip
      \expandafter\getFLOWlocation\FLOWlocation\end
      \ignorespaces}%
   \haalbuffer[flw-\currentFLOWnumber]%
   \ifcase\@@FLOWnx\relax
     \let\@@FLOWnx\FLOWwidth
   \fi
   \ifcase\@@FLOWny\relax
     \let\@@FLOWny\FLOWheight
   \fi
   \doifnothing{\@@FLOWmaxbreedte\@@FLOWmaxhoogte}{\FLOWscalingfalse}%
   \ifFLOWscaling 
     \doifnothing{\@@FLOWmaxbreedte}{\let\@@FLOWmaxbreedte\maxdimen}%
     \doifnothing{\@@FLOWmaxhoogte} {\let\@@FLOWmaxhoogte \maxdimen}%
     \scratchcounter=\bodyfontpoints
     \doloop  % NOG FONTSWITCH OM EX EN EM TE LATEN WERKEN 
       {\ifnum\scratchcounter>1 % NU DIMENSIONS IN TERMS OF BODYFONTSIZE
          \bodyfontsize=\the\scratchcounter pt
          \dimen0=\@@FLOWmaxbreedte
          \dimen2=\@@FLOWbreedte
          \dimen4=\@@FLOWdx
          \advance\dimen2 by 2\dimen4
          \dimen2=\@@FLOWnx\dimen2
          \advance\dimen2 by 2\dimen4
          \ifdim\dimen2>\dimen0
            \advance\scratchcounter by -1
          \else
            \dimen0=\@@FLOWmaxhoogte
            \dimen2=\@@FLOWhoogte
            \dimen4=\@@FLOWdy
            \advance\dimen2 by 2\dimen4
            \dimen2=\@@FLOWny\dimen2
            \advance\dimen2 by 2\dimen4
            \ifdim\dimen2>\dimen0
              \advance\scratchcounter by -1
            \else
              \exitloop
            \fi
          \fi
        \else
          \exitloop
        \fi}%
     \expanded{\switchtobodyfont[\the\scratchcounter pt]}%
     \forgetall
     \offinterlineskip
   \fi
   \global\let\FLOWcells\empty
   \dimen0=\@@FLOWbreedte
   \edef\FLOWshapewidth{\the\dimen0}%
   \dimen2=\@@FLOWdx
   \advance\dimen0 by 2\dimen2
   \edef\FLOWgridwidth{\the\dimen0}%
   \dimen0=\@@FLOWhoogte
   \edef\FLOWshapeheight{\the\dimen0}%
   \dimen2=\@@FLOWdy
   \advance\dimen0 by 2\dimen2
   \edef\FLOWgridheight{\the\dimen0}%
   \scratchdimen=\@@FLOSlijndikte
   \edef\@@FLOSlijndikte{\the\scratchdimen}%
   \scratchdimen=\@@FLOFlijndikte
   \edef\@@FLOFlijndikte{\the\scratchdimen}%
   \scratchdimen=\@@FLOLlijndikte
   \edef\@@FLOLlijndikte{\the\scratchdimen}%
   \ifdim\@@FLOLstraal<2.5\scratchdimen
     \scratchdimen=2.5\scratchdimen
     \edef\@@FLOLstraal{\the\scratchdimen}%
     \ifdim\@@FLOLstraal>\@@FLOWdx
       \scratchdimen=\@@FLOWdx
       \edef\@@FLOLstraal{\the\scratchdimen}%
     \fi
     \ifdim\@@FLOLstraal>\@@FLOWdy
       \scratchdimen=\@@FLOWdy
       \edef\@@FLOLstraal{\the\scratchdimen}%
     \fi
   \else
     \scratchdimen=\@@FLOLstraal
     \edef\@@FLOLstraal{\the\scratchdimen}%
   \fi
   \ifdim\@@FLOWoffset=\!!zeropoint
     \edef\@@FLOWoffset{\the\scratchdimen}%
   \else
     \scratchdimen=\@@FLOWoffset
     \edef\@@FLOWoffset{\the\scratchdimen}%
   \fi
   \forgetall
   \offinterlineskip
   \resetMPdrawing
   \doglobal\newcounter\FLOWcomment
   \startMPdrawing
     input mp-chart.mp ;
     grid_width             := \FLOWgridwidth ;
     grid_height            := \FLOWgridheight ;
     shape_width            := \FLOWshapewidth ;
     shape_height           := \FLOWshapeheight ;
     connection_line_width  := \@@FLOLlijndikte ;
     connection_smooth_size := \@@FLOLstraal ; % 2.5connection_line_width ;
     connection_arrow_size  := \@@FLOLstraal ; % 2.5connection_line_width ;
     connection_dash_size   := \@@FLOLstraal ; % 2.5connection_line_width ;
   \stopMPdrawing
%   \def\getFLOWlocation##1,##2\end%
%     {\ifnum0##1>\FLOWwidth \edef\FLOWwidth {##1}\fi
%      \ifnum0##2>\FLOWheight\edef\FLOWheight{##2}\fi}%
%  \long\def\startFLOWcell##1\stopFLOWcell%
%     {\resetFLOWcell
%      \ignorespaces##1\unskip
%      \expandafter\getFLOWlocation\FLOWlocation\end
%      \ignorespaces}%
%   \haalbuffer[flw-\currentFLOWnumber]%
%   \ifnum\@@FLOWnx\@@FLOWny=11 % listig
%     \let\@@FLOWnx\FLOWwidth
%     \let\@@FLOWny\FLOWheight
%   \fi
   \startMPdrawing
     begin_chart(0,\FLOWwidth,\FLOWheight);
     reverse_y := true ;
     chart_offset := \@@FLOWoffset ;
   \stopMPdrawing
   \doifelsenothing{\@@FLOWachtergrondkleur}
     {\startMPdrawing
      chart_background_color := white ;
      \stopMPdrawing}
     {\startMPdrawing
      chart_background_color := \MPcolor{\@@FLOWachtergrondkleur} ;
      \stopMPdrawing}%
   \doif{\@@FLOWoptie}{\v!test}
     {\startMPdrawing
        show_con_points := true ;
        show_mid_points := true ;
        show_all_points := true ;
      \stopMPdrawing}
   \processaction % private
     [\@@FLOWpunt]
     [     \v!ja=>\startMPdrawing
                    show_con_points := true ;
                    show_mid_points := true ;
                    show_all_points := true ;
                  \stopMPdrawing,
      \s!unknown=>\startMPdrawing
                    show_\@@FLOWpunt_points := true ;
                  \stopMPdrawing]%
   \long\def\startFLOWcell##1\stopFLOWcell%
     {\resetFLOWcell
      \ignorespaces##1\unskip
      \setxvalue{FLOW-loc-\FLOWname}{\FLOWlocation}%
      \ifx\FLOWshape\empty 
        \global\let\FLOWshape\@@FLOSdefault 
      \fi
      \doifnot{\FLOWshape}{\v!geen}
        {\ExpandBothAfter\doifinsetelse{\FLOWshape}{\FLOWshapes}
           {\edef\FLOWshapetag{shape_\FLOWshape}%
            \@EA\setFLOWname\@EA\FLOWshapetag\@EA{\FLOWshapetag}}
           {\doifnumberelse{\FLOWshape}
              {\let\FLOWshapetag\FLOWshape}
              {\let\FLOWshapetag\empty}}%
         \ifx\FLOWshapetag\empty \else
           \ExpandBothAfter\doifinsetelse{\FLOWshape}{\FLOWlines}
             {\chardef\FLOWstate=0 }
             {\ExpandBothAfter\doifcommonelse{\FLOWcell,\FLOWfocus}{\@@FLOWfocus}
                {\chardef\FLOWstate=1 }
                {\chardef\FLOWstate=2 }}%
           \startMPdrawing
             begin_sub_chart ;
             \ifcase\FLOWstate
               shape_line_color := \MPcolor{\@@FLOLkleur} ;
               shape_fill_color := \MPcolor{\@@FLOLkleur} ;
               shape_line_width := \@@FLOLlijndikte ;
             \or
               shape_line_color := \MPcolor{\@@FLOFkaderkleur} ;
               shape_fill_color := \MPcolor{\@@FLOFachtergrondkleur} ;
               shape_line_width := \@@FLOFlijndikte ;
             \or
               shape_line_color := \MPcolor{\@@FLOSkaderkleur} ;
               shape_fill_color := \MPcolor{\@@FLOSachtergrondkleur} ;
               shape_line_width := \@@FLOSlijndikte ;
             \fi
             \ifx\FLOWoverlay\empty
               peepshape := false ;
             \else
               peepshape := true ;
             \fi
             new_shape(\FLOWlocation,\FLOWshapetag) ;
             end_sub_chart ;
           \stopMPdrawing
         \fi}%
      \ignorespaces}%
   \haalbuffer[flw-\currentFLOWnumber]%
   \long\def\startFLOWcell##1\stopFLOWcell%
     {\resetFLOWcell
      \def\connection[####1]####2%
        {\doglobal\increment\FLOWcomment
         \setFLOWname\otherFLOWname{name_####2}%
         \doifdefinedelse{FLOW-loc-\FLOWname}
           {\edef\FLOWfrom{\getvalue{FLOW-loc-\FLOWname}}}
           {\edef\FLOWfrom{0,0}}%
         \doifdefinedelse{FLOW-loc-\otherFLOWname}
           {\edef\FLOWto  {\getvalue{FLOW-loc-\otherFLOWname}}}
           {\edef\FLOWto  {0,0}}%
         \FLOWsetconnect{####1}%
         \ifx\cFLOWfrom\empty\else
           \doifelse{\@@FLOLhoek}{\v!rond}
             {\startMPdrawing smooth     := true  ; \stopMPdrawing}
             {\startMPdrawing smooth     := false ; \stopMPdrawing}%
           \doifelse{\@@FLOLstreep}{\v!ja}
             {\startMPdrawing dashline   := true  ; \stopMPdrawing}
             {\startMPdrawing dashline   := false ; \stopMPdrawing}%
           \doifelse{\@@FLOLpijl}{\v!ja}
             {\startMPdrawing arrowtip   := true  ; \stopMPdrawing}
             {\startMPdrawing arrowtip   := false ; \stopMPdrawing}%
           \doifelse{\@@FLOLoffset}{\v!geen}
             {\startMPdrawing touchshape := true  ; \stopMPdrawing}
             {\startMPdrawing touchshape := false ; \stopMPdrawing}%
           \startMPdrawing
             connection_line_color := \MPcolor{\@@FLOLkleur} ;
             connection_line_width := \@@FLOLlijndikte ;
             connect_\cFLOWfrom_\cFLOWto (\FLOWfrom,\zFLOWfrom) (\FLOWto,\zFLOWto) ;
           \stopMPdrawing
         \fi
         \ignorespaces}%
      \ignorespaces##1\unskip}%
   \haalbuffer[flw-\currentFLOWnumber]%
   \startMPdrawing
     clip_chart(\@@FLOWx,\@@FLOWy,\@@FLOWnx,\@@FLOWny) ;
     end_chart ;
   \stopMPdrawing
   \MPdrawingdonetrue
   \setbox0=\hbox{\MPshiftdrawingfalse\getMPdrawing}%
   \def\MPmessage##1%
     {\writestatus{MP charts}{##1}}%
   \def\MPposition##1##2##3%
     {\setvalue{MPx##1}{##2}\setvalue{MPy##1}{##3}}%
   \def\MPclippath##1##2##3##4%
     {\def\clipMPllx{##1bp}\def\clipMPlly{##2bp}%
      \def\clipMPurx{##3bp}\def\clipMPury{##4bp}}%
   \def\MPareapath##1##2##3##4%
     {\def\areaMPllx{##1bp}\def\areaMPlly{##2bp}%
      \def\areaMPurx{##3bp}\def\areaMPury{##4bp}}%
   \readfile{\MPdatafile}{}{}%
   \doglobal\newcounter\FLOWcomment
   \long\def\startFLOWcell##1\stopFLOWcell%
     {\resetFLOWcell
      \ignorespaces##1\unskip
      \def\doprocessFLOWcell####1,####2\end  % kan ook met area
        {\!!counta=####1\relax
         \!!countb=####2\relax
         \!!countc=\@@FLOWx
         \!!countd=\@@FLOWy
         \advance\!!countc \@@FLOWnx
         \advance\!!countd \@@FLOWny
         \advance\!!countc -1
         \advance\!!countd -1
         \ifnum\!!counta<\@@FLOWx\relax
           \donefalse
         \else\ifnum\!!counta>\!!countc
           \donefalse
         \else\ifnum\!!countb<\@@FLOWy\relax
           \donefalse
         \else\ifnum\!!countb>\!!countd
           \donefalse
         \else
           \donetrue
           \doglobal\addtocommalist\FLOWcell\FLOWcells
           \advance\!!counta by -\@@FLOWx\advance\!!counta by 1
           \advance\!!countb by -\@@FLOWy\advance\!!countb by 1
           \dimen0=\FLOWgridwidth\dimen0=\!!counta\dimen0
           \advance\dimen0 by -\FLOWgridwidth
           \dimen4=\FLOWgridwidth\advance\dimen4 by -\FLOWshapewidth
           \advance\dimen0 by .5\dimen4
           \dimen2=\FLOWgridheight\dimen2=\!!countb\dimen2
           \dimen4=\FLOWgridheight\advance\dimen4 by -\FLOWshapeheight
           \advance\dimen2 by -.5\dimen4
           \setbox0=\hbox
             {\ifx\FLOWalign\empty\else
                \setupframed
                  [\c!uitlijnen=\v!normaal,\c!onder=\vfill,\c!boven=\vfill]%
                \@EA\processallactionsinset\@EA
                  [\FLOWalign]
                  [t=>{\setupframed[\c!onder=\vfill,\c!boven=]},
                   b=>{\setupframed[\c!onder=,\c!boven=\vfill]},
                   l=>{\setupframed[\c!uitlijnen=\v!rechts]},
                   r=>{\setupframed[\c!uitlijnen=\v!links]},
                   m=>{\setupframed[\c!uitlijnen=\v!midden]},
                   c=>{\setupframed[\c!uitlijnen=\v!midden]}]%
              \fi
              \doifelse{\FLOWshape}{\v!geen}
                {\setupframed[\c!offset=\v!overlay]}
                {\setupframed[\c!offset=.5\bodyfontsize]}%
              \framed
                [\c!kader=\v!uit,\c!breedte=\FLOWshapewidth,\c!hoogte=\FLOWshapeheight]
                {\FLOWtext}}%
           \showFLOWhelp0
           \ifx\FLOWdestination\empty\else
             \setbox0=\hbox
               {\setupinteraction[\c!kleur=,\c!contrastkleur=]%
                \naarbox{\box0}[\FLOWdestination]}%
           \fi
           \edef\FLOWdx{\the\dimen0}%
           \edef\FLOWdy{\the\dimen2}%
           \def\positionFLOWzero%
             {\setbox0=\hbox{\hskip\FLOWdx\lower\FLOWdy\box0}%
              \smashbox0
              \box0} %
           \positionFLOWzero
           \dimen0=\FLOWshapewidth \dimen2=.5\dimen0
           \dimen4=\FLOWshapeheight\dimen6=.5\dimen4
           \boxoffset=.5\bodyfontsize
           \setbox0=\hbox{\hskip\dimen2\raise\dimen4
             \hbox{\righttopbox{\strut\tFLOWlabel}}}%
           \positionFLOWzero
           \setbox0=\hbox{\hskip\dimen2
             \hbox{\rightbottombox{\strut\bFLOWlabel}}}%
           \positionFLOWzero
           \setbox0=\hbox{\raise\dimen6
             \hbox{\lefttopbox {\strut\lFLOWlabel}}}%
           \positionFLOWzero
           \setbox0=\hbox{\hskip\dimen0\raise\dimen6
             \hbox{\righttopbox{\strut\rFLOWlabel}}}%
           \positionFLOWzero
           \setbox0=\hbox{\hskip\dimen2 % for me only
             \hbox{\bottombox{\strut\bcFLOWlabel}}}%
           \positionFLOWzero
         \fi\fi\fi\fi}%
      \expandafter\doprocessFLOWcell\FLOWlocation\end
      \def\connection[####1]####2%
        {\doglobal\increment\FLOWcomment
         \ignorespaces}%
      \def\comment[####1]####2%
        {\bgroup
         \let\FLOW  \middlebox
         \let\FLOWb \bottombox
         \let\FLOWbl\bottomleftbox
         \let\FLOWbr\bottomrightbox
         \let\FLOWt \topbox
         \let\FLOWtl\topleftbox
         \let\FLOWtr\toprightbox
         \let\FLOWl \leftbox
         \let\FLOWlt\lefttopbox
         \let\FLOWlb\leftbottombox
         \let\FLOWr \rightbox
         \let\FLOWrt\righttopbox
         \let\FLOWrb\rightbottombox
         \let\FLOWc \middlebox
         \ifcase0\getvalue{MPx\FLOWcomment}\getvalue{MPy\FLOWcomment}\relax
         \else
           \ifdim\getvalue{MPx\FLOWcomment}bp<\areaMPllx\relax\else
             \ifdim\getvalue{MPx\FLOWcomment}bp>\areaMPurx\relax\else
               \ifdim\getvalue{MPy\FLOWcomment}bp<\areaMPlly\relax\else
                 \ifdim\getvalue{MPy\FLOWcomment}bp>\areaMPury\relax\else
                   \dimen0=\getvalue{MPx\FLOWcomment}bp
                   \advance\dimen0 by -\@@FLOWoffset
                   \advance\dimen0 by -\clipMPllx
                   \dimen2=\clipMPury
                   \advance\dimen2 by -\@@FLOWoffset
                   \advance\dimen2 by -\getvalue{MPy\FLOWcomment}bp
                   \setbox0=\hbox{\strut####2}%
                   \boxoffset=.5\bodyfontsize
                   \setbox0=\hbox
                     {\hskip\dimen0\lower\dimen2\getvalue{FLOW####1}{\box0}}%
                   \wd0=\!!zeropoint\ht0=\!!zeropoint\dp0=\!!zeropoint
                   \box0
                 \fi
               \fi
             \fi
           \fi
         \fi
         \egroup
         \ignorespaces}%
      \ignorespaces##1\unskip
      \ignorespaces}%
   \setbox2=\vbox to \ht0
     {\forgetall%%%\offinterlineskip
      \haalbuffer[flw-\currentFLOWnumber]\vss}%
   \setbox2=\hbox
     {\hskip\@@FLOWoffset\lower\@@FLOWoffset\box2}%
   \wd2=\wd0\ht2=\ht0\dp2=\dp0
   %%%%%%%%
   \long\def\startFLOWcell##1\stopFLOWcell%
     {\resetFLOWcell
      \ignorespaces##1\unskip
      \def\doprocessFLOWcell####1,####2\end  % redundant
        {\ifx\FLOWoverlay\empty \else
           \!!counta=####1\relax
           \!!countb=####2\relax
           \!!countc=\@@FLOWx
           \!!countd=\@@FLOWy
           \advance\!!countc \@@FLOWnx
           \advance\!!countd \@@FLOWny
           \advance\!!countc -1
           \advance\!!countd -1
           \ifnum\!!counta<\@@FLOWx\relax
             \donefalse
           \else\ifnum\!!counta>\!!countc
             \donefalse
           \else\ifnum\!!countb<\@@FLOWy\relax
             \donefalse
           \else\ifnum\!!countb>\!!countd
             \donefalse
           \else
             \donetrue
           \fi\fi\fi\fi
           \ifdone
             \advance\!!counta by -\@@FLOWx\advance\!!counta by 1
             \advance\!!countb by -\@@FLOWy\advance\!!countb by 1
             \dimen0=\FLOWgridwidth\dimen0=\!!counta\dimen0
             \advance\dimen0 by -\FLOWgridwidth
             \dimen4=\FLOWgridwidth\advance\dimen4 by -\FLOWshapewidth
             \advance\dimen0 by .5\dimen4
             \dimen2=\FLOWgridheight\dimen2=\!!countb\dimen2
             \dimen4=\FLOWgridheight\advance\dimen4 by -\FLOWshapeheight
             \advance\dimen2 by -.5\dimen4
             \edef\FLOWdx{\the\dimen0}%
             \edef\FLOWdy{\the\dimen2}%
             \setbox0=\hbox
               {\framed
                  [\c!kader=\v!uit,
                   \c!achtergrond={\@@FLOWachtergrond,\FLOWoverlay},
                   \c!achtergrondkleur=\@@FLOSachtergrondkleur,
                   \c!breedte=\FLOWshapewidth,\c!hoogte=\FLOWshapeheight]
                  {}}%
             \setbox0=\hbox{\hskip\FLOWdx\lower\FLOWdy\box0}%
             \smashbox0
             \box0
           \fi
         \fi}%
      \expandafter\doprocessFLOWcell\FLOWlocation\end}%
   \setbox4=\vbox to \ht0
     {\forgetall%%%\offinterlineskip
      \haalbuffer[flw-\currentFLOWnumber]\vss}%
   \setbox4=\hbox
     {\hskip\@@FLOWoffset\lower\@@FLOWoffset\box4}%
   \wd4=\wd0\ht4=\ht0\dp4=\dp0
   %%%%%%%%
   \doifelse{\@@FLOWoptie}{\v!test}
     {\setbox6=\vbox
        {\forgetall
         \vskip\@@FLOWoffset
         \hskip\@@FLOWoffset
         \rooster
           [\c!x=\@@FLOWx,\c!nx=\@@FLOWnx,\c!dx=\withoutpt\FLOWgridwidth,
            \c!y=\@@FLOWy,\c!ny=\@@FLOWny,\c!dy=\withoutpt\FLOWgridheight,
            \c!xstap=1,\c!ystap=1,
            \c!eenheid=pt,\c!plaats=\v!midden]}%
      \wd6=\wd0\ht6=\ht0\dp6=\dp0
      \setbox8=\vbox
        {\forgetall\offinterlineskip
         \vskip\@@FLOWoffset
         \dostepwiserecurse{\@@FLOWy}{\@@FLOWny}{1}
           {\vbox to \FLOWgridheight
              {\vfill
               \hskip\@@FLOWoffset
               \hbox
                 {\dostepwiserecurse{\@@FLOWx}{\@@FLOWnx}{1}
                    {\hbox to \FLOWgridwidth
                       {\hfill
                        \framed
                          [\c!kaderkleur=red,
                           \c!breedte=\FLOWshapewidth,
                           \c!hoogte=\FLOWshapeheight]
                          {}%
                        \hfill}}}
               \vfill}}}%
      \wd8=\wd0\ht8=\ht0\dp8=\dp0
      \framed 
        [\c!offset=\v!overlay,\c!kaderkleur=green]
        {\hbox{\box4\hskip-\wd0\box0\hskip-\wd2\box2\hskip-\wd6\box6\hskip-\wd8\box8}}}
     {\framed
        [\c!offset=\v!overlay,
         \c!kader=\@@FLOWkader,
         \c!kaderkleur=\@@FLOWkaderkleur,
         \c!achtergrond=\@@FLOWachtergrond,
         \c!achtergrondkleur=\@@FLOWachtergrondkleur]
        {\hbox{\box4\hskip-\wd0\box0\hskip-\wd2\box2}}}%
  %\message{[\FLOWcells]}\wait
   \egroup}

% \useFLOWchart[name][parent][setting,setting][additional settings]
% \useFLOWchart[name][parent][additional settings]

\def\useFLOWchart
  {\doquadrupleempty\douseFLOWchart}

\def\douseFLOWchart[#1][#2][#3][#4]% name parent sets mainsettings
  {\iffourthargument
     \setvalue{FLOW--#1}[##1]{\setgetFLOWchart[#2][#3][#4,##1]}%
   \else
     \checkparameters[#3]%
     \ifparameters
       \setvalue{FLOW--#1}[##1]{\setgetFLOWchart[#2][][#3,##1]}%
     \else
       \setvalue{FLOW--#1}[##1]{\setgetFLOWchart[#2][#3][##1]}%
     \fi
   \fi}

\def\setgetFLOWchart[#1][#2][#3]%
   {\def\docommando##1%
      {}% cell line focus 
    \processcommalist[#2]\docommando
    \getFLOWchart[#1][#3]}

\def\doFLOWchart[#1][#2]%
  {\hbox\bgroup\vbox\bgroup % vmode suppresses spaces
   \doifundefinedelse{FLOW--#1}
     {\getFLOWchart[#1][#2]}
     {\getvalue{FLOW--#1}[#2]}%
   \egroup\egroup}

\def\FLOWchart%
  {\dodoubleempty\doFLOWchart}

%D A hook into the help system. 

\def\showFLOWhelp#1%
  {\doifhelpinfo{\FLOWhelp}
     {\setbox#1=\hbox
        {\setbox\scratchbox=\hbox{\lower\@@FLOWdy\hbox
           {\helpbutton
              [\c!breedte=\wd0,\c!kleur=,\c!hoogte=\@@FLOWdy,\c!kader=\v!nee]
              [\FLOWhelp]}}%
         \smashbox\scratchbox
         \setbox#1=\vbox
           {\forgetall\offinterlineskip\box#1\box\scratchbox}%
         \box#1}}}

%D The next section is dedicated to splitting up charts. 

\def\getFLOWsize[#1]%
  {\bgroup\let\dodogetFLOWchart\dogetFLOWsize\FLOWchart[#1]\egroup}

\def\dogetFLOWsize[#1][#2]%
  {\setbox\scratchbox=\vbox
     {\xdef\FLOWmaxwidth {0}%
      \xdef\FLOWmaxheight{0}%
      \def\getFLOWlocation##1,##2\end%
        {\ifnum0##1>\FLOWmaxwidth \xdef\FLOWmaxwidth {##1}\fi
         \ifnum0##2>\FLOWmaxheight\xdef\FLOWmaxheight{##2}\fi}%
      \long\def\startFLOWcell##1\stopFLOWcell%
        {\resetFLOWcell##1\expandafter\getFLOWlocation\FLOWlocation\end}%
      \def\dosetFLOWchart[##1][##2]%
        {\haalbuffer[flw-##1]}%
      \getvalue{FLOW-#1}}}

\def\setupFLOWsplit%
  {\dodoubleargument\getparameters[@@FLOT]}

\setupFLOWsplit%
  [\c!nx=3,\c!ny=3,
   \c!dx=1,\c!dy=1,
   \c!commando=,
   \c!markering=\v!aan,
   \c!voor=,\c!na=]

\def\FLOWcharts%
  {\dodoubleempty\doFLOWcharts}

\def\doFLOWcharts[#1][#2]%
  {\bgroup
   \getFLOWsize[#1]%
   \def\@@FLOTx{1}%                  
   \doloop 
     {\def\@@FLOTy{1}%                  
      \doloop 
        {\bgroup
         \scratchcounter=\FLOWmaxwidth 
         \advance\scratchcounter by -\@@FLOTx
         \advance\scratchcounter by 1
         \ifnum\scratchcounter<\@@FLOTnx\edef\@@FLOTnx{\the\scratchcounter}\fi
         \scratchcounter=\FLOWmaxheight 
         \advance\scratchcounter by -\@@FLOTy
         \advance\scratchcounter by 1
         \ifnum\scratchcounter<\@@FLOTny\edef\@@FLOTny{\the\scratchcounter}\fi
         \doifnot{\@@FLOTmarkering}{\v!aan}{\let\cuthbox\relax}%
         \@@FLOTvoor
         \cuthbox
           {\@@FLOTcommando
              {\FLOWchart[#1][#2,
                 \c!x=\@@FLOTx,\c!nx=\@@FLOTnx,
                 \c!y=\@@FLOTy,\c!ny=\@@FLOTny]}}%
         \@@FLOTna
         \egroup
         \increment(\@@FLOTy,\@@FLOTny)%
         \ifnum\@@FLOTy>\FLOWmaxheight
           \exitloop
         \else  
           \decrement(\@@FLOTy,\@@FLOTdy)%
         \fi}%
      \increment(\@@FLOTx,\@@FLOTnx)%
      \ifnum\@@FLOTx>\FLOWmaxwidth 
        \exitloop
      \else  
        \decrement(\@@FLOTx,\@@FLOTdx)%
      \fi}%
   \egroup}

%D An example of splitting is given below: 
%D 
%D \starttypen 
%D \setupFLOWsplit
%D   [nx=5,ny=10,
%D    dx=0,dy=0,
%D    before=,
%D    after=\pagina]
%D 
%D \FLOWcharts[mybigflow]
%D \stoptypen 
%D
%D Or, one can say: 
%D
%D \starttypen 
%D \splitsplaatsblok
%D   {\plaatsfiguur{What a big flowchart this is!}}
%D   {\FLOWcharts[mybigflow]}
%D \stoptypen 

\protect

\endinput
