%D \module
%D   [       file=page-log, % moved here from main-001
%D        version=1997.03.31,
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Logos,
%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 / Logos}

\unprotect

\startmessages  dutch  library: layouts
      7: beeldmerken berekenen
\stopmessages

\startmessages  english  library: layouts
      7: calculating logospace
\stopmessages

\startmessages  german  library: layouts
      7: berechne Platz des Logo
\stopmessages

\startmessages  czech  library: layouts
      7: pocita se misto pro logo
\stopmessages

\startmessages  italian  library: layouts
      7: calcolo dello spazio per logo
\stopmessages

\startmessages  norwegian  library: layouts
      7: beregner plass for logo
\stopmessages

\startmessages  romanian  library: layouts
      7: se calculeaza spatiul pentru logo
\stopmessages

\startmessages  french  library: layouts
      7: calcul de l'espace pour le logo
\stopmessages

%D Although logos can conveniently be implemented on top of
%D background and text areas, we provide a dedicated mechanism
%D here. One reason is that such a separate mechanism cannot
%D interfere with the other ones, but an even more important
%D reason is that logos are kind of special in the sense that
%D they have a short life span and may change after the first
%D page.

%D \macros
%D  {recalculatelogos,addlogobackground}
%D
%D The interface to the other low level page building routines
%D is provided by a macro that signals changes in layout
%D specifications:
%D
%D \starttyping
%D \recalculatelogos
%D \stoptyping
%D
%D as well as a simple placement macro:
%D
%D \starttyping
%D \addlogobackground <box>
%D \stoptyping
%D
%D In no way the following boolean switch should be used
%D directly.

\newif\ifnewlogos

\def\recalculatelogos
  {\global\newlogostrue}

%D The current state of logos is registered in a status
%D variable \type {\logostatus}.
%D
%D \starttabulate[|l|l|l|]
%D \NC 0 \NC don't place              \NC remains 0 \NC \NR
%D \NC 1 \NC place now                \NC remains 1 \NC \NR
%D \NC 2 \NC calulate and place       \NC becomes 1 \NC \NR
%D \NC 3 \NC calculate and place once \NC becomes 2 \NC \NR
%D \stoptabulate

\chardef\logostatus=0

\def\addlogobackground#1% todo: dimension spec
  {\ifcase\logostatus \else
     \ifcase\logostatus
       % no logos to take care of
     \or % 1
       \ifnewlogos
         \chardef\logostatus2
         \setlogoboxes
         \chardef\logostatus1
         \global\newlogosfalse
       \fi
     \or % 2
       \setlogoboxes
       \chardef\logostatus1
     \or % 3
       \setlogoboxes
       \global\chardef\logostatus2
     \fi
     \setbox#1\vbox
       {\offinterlineskip
        \doifmarginswapelse
          {\copy\leftlogos}
          {\copy\rightlogos}
        \box#1}%
   \fi}

%D For efficiency reasons (and since logos seldom change inside
%D a document) we can save the left and right hand (or first
%D and following page) logos in boxes. The areas are slightly
%D different from the ones used in backgrounds and text
%D placement, but still related to the page layout. The {\em
%D left} and {\em right edge}, as well as {\em top} and {\em
%D bottom} touch the bounding box of the paper and are
%D therefore not the sams as their background adn text
%D counterparts. In addition there are {\em left}, {\em right}
%D and {\em middle} areas as well as a {\em page} one.

\newbox\leftlogos
\newbox\rightlogos

\def\setlogoboxes
  {\showmessage\m!layouts7\empty
   \dosetlogobox\leftlogos\relax
   \ifdoublesided
     \dosetlogobox\rightlogos\doswapmargins
   \fi}

\def\dosetlogobox#1#2%
  {\global\setbox#1\vbox to \paperheight
     {\dontcomplain           % needed here ?
      \calculatereducedvsizes % needed here ?
      \offinterlineskip
      #2\relax
      \vskip-\topspace
      \dodosetlogobox\v!top\blap
      \vskip\topspace
      \dodosetlogobox\v!header\blap
      \vskip\headerheight
      \vskip\headerdistance
      \dodosetlogobox\v!text\blap
      \vskip\textheight
      \vskip\footerdistance
      \vskip\footerheight
      \dodosetlogobox\v!footer \tlap
      \vfilll
      \dodosetlogobox\v!bottom\tlap
      \vskip\topspace}
  \smashbox#1}

\def\dodosetlogobox#1#2%
  {\hbox % width equals \makeupwidth
     {\def\docommand##1%
        {\donefalse
         \ifnum\logostatus=3 \ExpandBothAfter
           \doifinset{\getvalue{\??lo#1##1}}\requestedlogos\donetrue
         \else
           \doifvalue{\??lo#1##1\c!state}\v!start\donetrue
         \fi
         \ifdone
           #2{\hbox{\getvalue{\??lo#1##1\c!command}}}%
         \fi}%
      \def\dodocommand##1##2##3##4##5##6%
        {\hsmash
           {\hskip-\texthoffset
            \hbox to \paperwidth
              {\rlap{\docommand##1}\hss\llap{\docommand##6}}%
            \hskip-\paperwidth
            \hbox to \paperwidth
              {\hskip\texthoffset
               \hskip-\leftmarginwidth
               \hskip-\leftmargindistance
               \hbox to \leftmarginwidth{\docommand##2\hss}%
               \hskip\leftmargindistance
               \hbox to \makeupwidth{\docommand##3\hss\docommand##4}%
               \hskip\rightmargindistance
               \hbox to \rightmarginwidth{\hss\docommand##5}%
               \hfill}}}%
      \normalbaselines
      \settexthoffset
      \hsmash
        {\hbox to \makeupwidth{\hss\docommand\c!middle\hss}}%
      \hsmash
        {\hskip-\texthoffset
         \hbox to \paperwidth{\docommand\v!page\hss}}%
      \swapmargins
      \doifbothsidesoverruled
        {\dodocommand
          \v!leftedge \v!leftmargin  \v!left
          \v!right     \v!rightmargin \v!rightedge}
        {\dodocommand
          \v!leftedge \v!leftmargin  \v!left
          \v!right     \v!rightmargin \v!rightedge}
        {\dodocommand
          \v!rightedge \v!rightmargin \v!right
          \v!left       \v!leftmargin  \v!leftedge}}}

%D The user interface is relatively simple and provides
%D macros for assigning logos to logo areas as well as
%D forcing placement.
%D
%D \showsetup{definelogo}
%D \showsetup{placelogos}

\let\definedlogos  \empty
\let\requestedlogos\empty

\long\def\dodefinelogo[#1][#2][#3][#4]%
  {\addtocommalist{#1}\definedlogos
   \long\setvalue{\??lo#2#3}{#1}%
   \getparameters[\??lo#2#3][#4]%
   \global\chardef\logostatus2 }

\def\definelogo
  {\doquadrupleargument\dodefinelogo}

\def\placelogos
  {\dosingleempty\doplacelogos}

\def\doplacelogos[#1]%
  {\xdef\requestedlogos{\iffirstargument#1\else\definedlogos\fi}%
   \global\chardef\logostatus3 }

\protect \endinput
