%D \module
%D   [       file=page-pls,
%D        version=2003.03.16,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Page Setup,
%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.

\ifx\pageareabox\undefined \else \endinput \fi

\writestatus{loading}{Context Core Macros / Extra Page Building}

%D This feature has been present for a while but has never been
%D exploited: pluggable pagebuilders. The next example code
%D demonstrates the application of one such a plug-in. This variant
%D support \type {page}, \type {leftpage} and \type {rightpage}
%D definitions where specific areas are placed with the \type
%D {\pagearea} command.
%D
%D \starttyping
%D \setupheadertexts[the header text]
%D \setupfootertexts[a pretty long left footer text][something right]
%D \setupbottomtexts[a not so long bottom text][another right thing]
%D \setuptexttexts  [margin][something marginal][indeed]
%D
%D \startpagelayout[leftpage]
%D   \setupTABLE[offset=overlay]
%D   \setupTABLE[c][1][width=\leftmarginwidth]
%D   \bTABLE
%D     \bTR
%D       \bTD[nx=3,background=color,backgroundcolor=green]
%D         \pagearea[header][text][middle]
%D       \eTD
%D     \eTR
%D     \bTR
%D       \bTD \pagearea[text][margin][left] \eTD
%D       \bTD[nx=2] \pagearea[text] \eTD
%D     \eTR
%D     \bTR
%D       \bTD[nx=3,offset=overlay]
%D         {\bTABLE[width=.5\hsize]
%D            \bTR
%D              \bTD \pagearea[footer][text][left] \eTD
%D              \bTD \pagearea[bottom][text][left] \eTD
%D            \eTR
%D          \eTABLE}
%D        \eTD
%D     \eTR
%D   \eTABLE
%D \stoppagelayout
%D
%D \startpagelayout[rightpage]
%D   \setupTABLE[offset=overlay]
%D   \setupTABLE[c][1][width=\rightmarginwidth]
%D   \bTABLE
%D     \bTR
%D       \bTD[nx=3] \pagearea[header][text][middle] \eTD
%D     \eTR
%D     \bTR
%D       \bTD \pagearea[text][margin][left] \eTD
%D       \bTD[nx=2] \pagearea[text] \eTD
%D     \eTR
%D     \bTR
%D       \bTD[nx=3,offset=overlay]
%D         {\bTABLE[width=.5\hsize]
%D            \bTR
%D              \bTD \pagearea[bottom][text][right] \eTD
%D              \bTD \pagearea[footer][text][right] \eTD
%D            \eTR
%D          \eTABLE}
%D       \eTD
%D     \eTR
%D   \eTABLE
%D \stoppagelayout
%D
%D \setupcolors[state=start]
%D
%D \setupbackgrounds[text][background=color,backgroundcolor=blue]
%D \setupbackgrounds[header][text][background=color,backgroundcolor=red]
%D
%D \setuppagenumbering[alternative=doublesided,location=]
%D
%D \setuplayout[method=makeup]
%D
%D \definetextbackground
%D   [test]
%D   [state=start,
%D    background=color,
%D    backgroundcolor=yellow]
%D
%D \starttext
%D
%D \dorecurse{10}{\input tufte \par}
%D
%D \input tufte \starttest \input tufte \stoptest \input tufte
%D
%D \starttabulate
%D \NC test \NC \starttest \input tufte  \stoptest \NC \NR
%D \stoptabulate
%D
%D \dorecurse{10}{\input tufte \par}
%D
%D \stoptext
%D \stoptyping

\unprotect

\setvalue{\??ly\c!method\v!makeup}#1#2%
  {\setbox\pagebox\hbox
     {\vbox to \textheight
        {\offinterlineskip
         % optie
         \vskip-1\topskip
         \vskip\strutheight
         %
         \textwidth\makeupwidth
         \hsize\textwidth
         \boxmaxdepth\maxdepth
         \noindent
         \dopagecontents#1#2}}%
   \wd\pagebox\makeupwidth
   \ht\pagebox\textheight
   \dp\pagebox\zeropoint
   \hsize\paperwidth
   \vsize\paperheight
   \setbox\pagebox\vbox
     {\doifbothsidesoverruled
        {\let\!!stringa\v!page}
        {\let\!!stringa\v!rightpage}
        {\let\!!stringa\v!leftpage}%
      \getvalue{\??ly\c!method:\!!stringa}}%
   \wd\pagebox\paperwidth
   \ht\pagebox\paperheight
   \dp\pagebox\zeropoint}

\newbox\pageareabox

\def\pagearea
  {\dotripleempty\dopagearea}

\def\dopagearea[#1][#2][#3]%
  {\ifthirdargument
     \doifelse{#3}\v!left
       {\dodopagearea{#1}{#2}\c!lefttext}
       {\doifelse{#3}\v!right
          {\dodopagearea{#1}{#2}\c!righttext}
          {\dodopagearea{#1}{#2}\c!middletext}}%
   \else\ifsecondargument
     \doifbothsidesoverruled
       {\dodopagearea{#1}{#2}\c!righttext}
       {\dodopagearea{#1}{#2}\c!righttext}
       {\dodopagearea{#1}{#2}\c!lefttext }%
   \else
     \doif{#1}\v!text % copy due to trial runs in TABLE
       {\iftrialtypesetting
          \copy\pagebox
        \else
          \setbox\pagebox\vbox
            {\localstarttextcolor
               \box\pagebox
             \localstoptextcolor}%
          \localpositioningfalse
          \addtextbackground\pagebox
          \addtextgridlayer\pagebox
          \box\pagebox
        \fi}%
   \fi\fi}

\def\dodopagearea#1#2#3%
  {\setbox\pageareabox\vbox{\getvalue{\??tk#1#2#3}}%
   \ifsomebackgroundfound{#1#2}%
     \iftrialtypesetting
       \box\pageareabox
     \else
       \localframed
         [\??ma#1#2]
         [\c!width=\wd\pageareabox,
          \c!height=\ht\pageareabox,
          \c!offset=\v!overlay]
         {\box\pageareabox}%
     \fi
   \else
     \box\pageareabox
   \fi}

\setvalue{\??ly\c!method:\v!leftpage }{\getvalue{\??ly\c!method:\v!page}}
\setvalue{\??ly\c!method:\v!rightpage}{\getvalue{\??ly\c!method:\v!page}}

% \long\def\startpagelayout[#1]#2\stoppagelayout
%   {\long\setvalue{\??ly\c!method:#1}{#2}}

\long\def\startpagelayout
  {\bgroup\catcode`\^^M=\@@ignore\dostartpagelayout}

\long\def\dostartpagelayout[#1]#2\stoppagelayout
  {\egroup\long\setvalue{\??ly\c!method:#1}{#2}}

\protect \endinput
