%D \module
%D   [       file=page-flw,
%D        version=2003.04.19, % from test-002 (1997) profile experiment 
%D          title=\CONTEXT\ OTR Macros,
%D       subtitle=Text Flows,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=PRAGMA ADE]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{Context OTR Macros / Text Flows}

\unprotect 

\def\definetextflow
  {\dodoubleempty\dodefinetextflow}

\def\dodefinetextflow[#1][#2]% flow settings
  {\iffirstargument       
     \doiftextflowcollectorelse{#1}
       {\setbox\textflowcollector{#1}\emptybox}
       {\@EA\newbox\csname\??tx:c:#1\endcsname}%
     \getparameters[\??tx:p:#1]
       [\c!breedte=\hsize,\c!letter=,#2]%
   \fi}

\def\textflowparameter#1#2{\csname\??tx:p:#1#2\endcsname}
\def\textflowcollector  #1{\csname\??tx:c:#1\endcsname}

\def\doiftextflowcollectorelse#1{\doifdefinedelse{\??tx:c:#1}}

\def\doiftextflowelse#1%
  {\doiftextflowcollectorelse{#1}
     {\ifvoid\textflowcollector{#1}%
        \expandafter\secondoftwoarguments
      \else
        \expandafter\firstoftwoarguments
      \fi}
     {\secondoftwoarguments}}

\def\doiftextflow#1%
  {\doiftextflowelse{#1}\firstofoneargument\gobbleoneargument}

\def\starttextflow[#1]%
  {\doiftextflowcollectorelse{#1}
     {\global\setbox\textflowcollector{#1}\vbox
        \bgroup
        \unvbox\textflowcollector{#1}%
        \hsize\textflowparameter{#1}\c!breedte
        \doifsomething{\textflowparameter{#1}\c!letter}%
          {\doconvertfont{\textflowparameter{#1}\c!letter}}%
        \def\stoptextflow{\endgraf\egroup}}
     {\let\stoptextflow\relax}}

\def\flushtextflow#1%
  {\doiftextflow{#1}
     {\ifdim\ht\textflowcollector{#1}>\vsize
        \setbox\scratchbox\vsplit\textflowcollector{#1} to \vsize
        \unvbox\scratchbox
      \else
        \unvbox\textflowcollector{#1}%
      \fi}}

\protect \endinput 

% Example (dutch) 
% 
% \stelpapierformaatin [S6]
% \steltolerantiein    [soepel,rek]
% \stelkleurenin       [status=start]
% \stelvoetin          [strut=nee]
% \stelwitruimtein     [groot]
% 
% \stellayoutin
%   [rechterrand=5cm,breedte=passend,marge=0pt,randafstand=1cm,
%    voet=4cm,voetafstand=1cm,hoofd=0cm]
% 
% \stelteksttekstenin[rand][][\flushtextflow{first}]
% \stelvoettekstenin [rand][][\flushtextflow{second}]
% \stelvoettekstenin         [\flushtextflow{third}][]
% 
% \definetextflow [first]  [alfa]  [breedte=\rechterrandbreedte]
% \definetextflow [second] [beta]  [breedte=\rechterrandbreedte]
% \definetextflow [third]  [gamma] [breedte=\voethoogte]
% 
% \starttekst
% 
% \dorecurse{50}
%   {\getrandomnumber{\funny}{0}{8}
%    \ifcase\funny \starttextflow[alfa]  \input tufte.tex   \stoptextflow
%    \or           \starttextflow[beta]  \input knuth.tex   \stoptextflow
%    \or           \starttextflow[gamma] \input materie.tex \stoptextflow
%    \or          {\bf   TUFTE}\quad \input tufte   \par
%    \or          {\bf   TUFTE}\quad \input tufte   \par
%    \or          {\bf   KNUTH}\quad \input knuth   \par
%    \or          {\bf   KNUTH}\quad \input knuth   \par
%    \or          {\bf MATERIE}\quad \input materie \par
%    \else        {\bf MATERIE}\quad \input materie \par
%    \fi}
% 
% \stoptekst
