%D \module
%D   [       file=page-mak, % copied from main-001,
%D        version=1997.03.31,
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Simple MakeUp,
%D         author=Hans Hagen,
%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.

\writestatus{loading}{Context Page Macros / MakeUp}

\unprotect

%D \macros
%D   {definemakeup, setupmakeup, startmakeup}
%D
%D A makeup is a separate page, like a title page or colofon.
%D There is one standard makeup page, but you can define more
%D if needed.
%D
%D \starttyping
%D \startstandardmakeup
%D   My Fancy Title
%D \stopstandardmakeup
%D \stoptyping
%D
%D The associated commands are:
%D
%D \showsetup{definemakeup}
%D \showsetup{setupmakeup}
%D \showsetup{startmakeup}

\def\definemakeup
  {\dodoubleargument\dodefinemakeup}

\def\dodefinemakeup[#1][#2]%
  {\getparameters
     [\??do#1]%
     [\c!width=\makeupwidth,  % example in manual
      \c!height=\textheight,  % example in manual
      \c!voffset=\!!zeropoint, % example in manual
      \c!hoffset=\!!zeropoint, % example in manual
      \c!commands=,
      \c!setups=,
      \c!page=\v!right,
      \c!doublesided=\v!empty,
      \c!before=,
      \c!top=\vss,
      \c!bottom=\vss,
      \c!after=,
      \c!bottomstate=\v!normal,
      \c!topstate=\v!normal,
      \c!textstate=\v!normal,
      \c!headerstate=\v!stop,
      \c!footerstate=\v!stop,
      \c!pagestate=\v!stop, % in manual ! ! !
      \c!color=,
      \c!align=,
      #2]%
   \setvalue{\e!start#1\e!makeup}{\startmakeup[#1]}%
   \setvalue{\e!stop #1\e!makeup}{\stopmakeup}}

\def\setupmakeup
  {\dodoubleargument\dosetupmakeup}

\def\dosetupmakeup[#1]%
  {\getparameters[\??do#1]}

%D This will save us some 375 bytes in the format file.

\def\makeupparameter#1{\getvalue{\??do\currentmakeup#1}}

%D The \type{\start}||\type{\stop} macros are used for both
%D the direct and indirect way. The parameterless call will
%D build a simple box.

\newtoks\everymakeup

\appendtoks \postponemarks \to \everymakeup

\let\currentmakeup\empty

\def\startmakeup
  {\dodoubleempty\dostartmakeup}

\def\dostartmakeup[#1][#2]%
  {\bgroup
   \edef\currentmakeup{#1}%
   \the\everymakeup
   \iffirstargument
     \setupmakeup[\currentmakeup][#2]%
     \let\stopmakeup\dodostopmakeup
     \expandafter   \dodostartmakeup
   \else
     \let\stopmakeup\donostopmakeup
     \expandafter   \donostartmakeup
   \fi}

%D The simple case:

\def\donostartmakeup
  {\page
   \setupheader[\c!state=\v!empty]%
   \setupfooter[\c!state=\v!empty]%
   \setsystemmode\v!makeup
   \vbox to \textheight\bgroup\hsize\textwidth}

\def\donostopmakeup
  {\egroup
   \flushmarks % new, here, else empty pages
   \page
   \egroup}

%D The normal one:

\newbox\makeupbox

\def\dodostartmakeup
  {\doifvaluesomething{\??do\currentmakeup\c!page}
     {\ExpandFirstAfter\page[\makeupparameter\c!page]}%
   \pagetype[\currentmakeup]%
   \setsystemmode\v!makeup
   \setupmakeuplayout
   \makeupparameter\c!commands % hm, what is this one doing here ?
   \startregistercolor[\makeupparameter\c!color]%
   \global\setbox\makeupbox\vbox to \makeupparameter\c!height
     \bgroup
       \forgetall
       \setups[\makeupparameter\c!setups]%
       \hsize\makeupparameter\c!width
       \doifsomething{\makeupparameter\c!align}
         {\setupalign[\makeupparameter\c!align]}%
       \makeupparameter\c!top}

\def\dodostopmakeup
  {\endgraf
   \makeupparameter\c!bottom
   \egroup
   \flushmarks % new, here, else empty pages
\stopregistercolor
   \doflushmakeup
   \egroup
   \calculatehsizes
   \calculatevsizes}

%D Selective page processing is handled here. (Why?)

\def\doflushmakeup
  {\ifverwerken
     \ifgeselecteerd
       \doshipoutmakeup
     \fi
   \else
     \ifgeselecteerd \else
       \doshipoutmakeup
     \fi
   \fi
   \ifselecteren
     \global\geselecteerdfalse
   \fi}

\def\doshipoutmakeup
  {\globalpushmacro\@@pnstate % new
   \makeupparameter\c!before
   \vbox{\hbox{\color[\makeupparameter\c!color]{\box\makeupbox}}}%
   \setuppagenumber[\c!state=\makeupparameter\c!pagestate]%
   \setupmakeuplayout
   \page
   \makeupparameter\c!after
   \relax % voor fi
   \ifdubbelzijdig \ifodd\realpageno\else
     \processaction
       [\makeupparameter\c!doublesided]
       [  \v!yes=>\null
                  \page,
                % \verlaagpaginanummer, % new
        \v!empty=>{\setupmakeuplayout
                  \page[\v!blank]%
                  \null
                  \page}]%
                % \verlaagpaginanummer]% % new
   \fi \fi
 % \verlaagpaginanummer % new
   \globalpopmacro\@@pnstate} % new

%D The text surrounding the main body text can be influenced
%D by setting their associated status variables. The
%D connection between them is made by the following macro

\def\setupmakeuplayout
  {\setupfooter[\c!state=\makeupparameter\c!footerstate]%
   \setupheader[\c!state=\makeupparameter\c!headerstate]%
   \setuptext  [\c!state=\makeupparameter\c!textstate]%
   \setupbottom[\c!state=\makeupparameter\c!bottomstate]%
   \setuptop   [\c!state=\makeupparameter\c!topstate]%
   % this is needed, but no \setuplayout here; fails in texexec --fig=c
   \recalculatelayout}

%D The standard page template is defined as follows:

\definemakeup
  [\v!standard]
  [\c!width=\makeupwidth,
   \c!height=\textheight,
   \c!voffset=\!!zeropoint,
   \c!hoffset=\!!zeropoint,
   \c!page=\v!right,
   \c!doublesided=\v!empty]

\definemakeup
  [\v!text]
  [\c!topstate=\v!start,
   \c!headerstate=\v!start,
   \c!textstate=\v!start,
   \c!footerstate=\v!start,
   \c!bottomstate=\v!start,
   \c!doublesided=\v!no,
   \c!page=\v!yes,
   \c!top=\pseudostrut\ignorespaces,
   \c!bottom=\obeydepth\vss]

\protect \endinput
