%D \module
%D   [       file=page-one,
%D        version=2000.10.20,
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Default Routine,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%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 / Default Routine}

%D This is just the good old \CONTEXT\ output routine, which
%D has been there right from the start.

\unprotect

% OTRONE: basic single column

\activateotr{ONE}{} % the default one

\newtoks\OTRONEoutput

\def\OTRONEgotonextpage
  {\ejectpage}

\def\OTRONEgotonextpageX % will become obsolete
  {\superejectpage}

\def\OTRONEsethsize
  {\global\hsize\textwidth}

% keep (original one)
%
% \def\OTRONEsetvsize
%    {\ifdim\vsize=\teksthoogte \else
%       \bgroup
%       \scratchdimen-\vsize
%       \advance\scratchdimen \teksthoogte
%       \global\advance\vsize \scratchdimen
%       \relax \ifdim\pagegoal<\maxdimen
%         \advance\scratchdimen \pagegoal
%         \global\pagegoal\scratchdimen
%       \fi
%       \egroup
%    \fi}
%
% no (keep)
%
% \def\OTRONEsetvsize
%   {\ifdim\vsize=\teksthoogte \else
%      \bgroup
%      \scratchdimen-\vsize
%      \advance\scratchdimen \teksthoogte
%      \ifgridsnapping
%        \getrawnoflines\scratchdimen
%        \scratchdimen\noflines\openlineheight
%        \ifdim\scratchdimen>\noflinesheight % available afterwards
%          \advance\scratchdimen-\openlineheight
%        \fi
%        \ifdim\scratchdimen<\zeropoint
%          \scratchdimen\zeropoint
%        \fi
%      \fi
%      \global\advance\vsize \scratchdimen
%      \relax \ifdim\pagegoal<\maxdimen
%        \advance\scratchdimen \pagegoal
%        \global\pagegoal\scratchdimen
%      \fi
%      \egroup
%   \fi}

% \def\OTRONEsetvsize
%   {\ifgridsnapping
%      \ifcase\layoutlines
%        \getrawnoflines\teksthoogte
%      \else
%        \noflines\layoutlines
%      \fi
%      \global\vsize\noflines\openlineheight
%    \else
%      \global\vsize\teksthoogte
%    \fi
%    \ifdim\pagegoal<\maxdimen
%      \global\pagegoal\vsize
%    \fi}

\newdimen\oldvsize

\def\OTRONEsetvsize
  {\ifgridsnapping
     \ifcase\layoutlines
       \getrawnoflines\textheight
     \else
       \noflines\layoutlines
     \fi
     \global\vsize\noflines\openlineheight
   \else
     \global\vsize\textheight
   \fi
   \ifdim\pagegoal<\maxdimen
     \ifdim\oldvsize=\vsize
       % let's assume that the layout didn't change
     \else
       \bgroup
         \global\oldvsize\vsize
         \advance\vsize-\topinserted
         \advance\vsize-\botinserted
         \global\pagegoal\vsize
       \egroup
     \fi
   \fi}

\chardef\kindofpagetextareas=2 % whole page (public variable! never change)

\def\OTRONEregisteredtextarea#1%
  {\ifregistertextareas
     \setbox0\vbox{#1}%
     \wd0\makeupwidth % somehow a space creeps in
     \vbox{\registeredtextarea000}%
   \else
     #1%
   \fi}

% \chardef\kindofpagetextareas\plusone

\def\doOTRONEregisteredtextareaA#1%
  {\ifregistertextareas
     \xypos{pbd:\realfolio:b}% we could save bytes by only saving the y
     \endgraf
     \begingroup
     \scratchdimen\MPy{pbd:\realfolio:b}%
     \advance\scratchdimen-\MPy{pbd:\realfolio:e}%
     \setbox\scratchbox\null
     \wd\scratchbox\makeupwidth
     \ht\scratchbox\scratchdimen
     \vsmash{\registeredtextarea00\scratchbox}%
     \endgroup
     #1%
     \endgraf
     \xypos{pbd:\realfolio:e}%
   \else
     #1%
   \fi}

\def\doOTRONEregisteredtextareaB#1%
  {\ifregistertextareas
     \setbox0\vbox{#1}%
     \wd0\makeupwidth % somehow a space creeps in
     \vbox{\registeredtextarea000}%
   \else
     #1%
   \fi}

\let\OTRONEregisteredtextareaA\firstofoneargument
\let\OTRONEregisteredtextareaB\firstofoneargument

\def\OTRONEdopagecontents#1#2% \box<n> \unvbox<n>
  {\bgroup             % niet breedte zetten, kan fractie zijn!
   \ifcase\kindofpagetextareas
   \or % partial page (experimental)
     \let\OTRONEregisteredtextareaA\doOTRONEregisteredtextareaA
   \or % whole page   (default)
     \let\OTRONEregisteredtextareaB\doOTRONEregisteredtextareaB
   \or % partial page (only works well with no stretch!)
     \let\OTRONEregisteredtextareaA\doOTRONEregisteredtextareaB
   \fi
   \setbox0\vbox \ifbottomnotes to \textheight \fi
     {\edef\currentpagedepth{\the\dp#2}% still to be derived from #1
      \dotopinsertions
      \ifgridsnapping
        \OTRONEregisteredtextareaA{#1#2}%
        \vskip-\currentpagedepth\vskip\openstrutdepth
        \pushproperties % moved from just after #1#2
        \prevdepth\openstrutdepth
        \dobotinsertions
        \vfil
      \else\ifr@ggedbottom
        \OTRONEregisteredtextareaA{#1#2}%
        \vskip-\currentpagedepth\vskip\openstrutdepth
        \pushproperties % moved from just after #1#2
        \prevdepth\openstrutdepth
        \dobotinsertions
        \vfil
      \else\ifb@selinebottom
        \OTRONEregisteredtextareaA{#1#2}%
        \kern-\currentpagedepth\kern\maxdepth
        \pushproperties % moved from just after #1#2
        \dobotinsertions
      \else
        \OTRONEregisteredtextareaA{#1#2}%
        \pushproperties % moved from just after #1#2
        \dobotinsertions % added
      \fi\fi\fi
      \fakepagenotes}% was \fakenotes, but wrong! (check with \setupalign[height])
   \ifbottomnotes
     \ifgridsnapping
\ifcase\layoutlines % todo: make macro of this
    \getrawnoflines\textheight
\else
    \noflines\layoutlines
\fi
%        \getnoflines\textheight
       \advance\noflines \minusone
       \scratchdimen\noflines\lineheight
       \advance\scratchdimen \topskip
     \else
       \scratchdimen\ht0
     \fi
   \else
     \scratchdimen\zeropoint
   \fi
   \setbox2\hbox
     {\checksinglecolumnfootnotes
      \lower\scratchdimen\vbox{\placebottomnotes}}%
   \smashbox2% % needed here
   \ifbottomnotes
     \ht0\zeropoint
   \fi
   \OTRONEregisteredtextareaB
     {\vbox to \textheight
        {\box0\box2\ifbottomnotes\else\vfill\fi}}%
   \egroup}

\def\OTRONEfinalsidefloatoutput
  {\finaloutput\unvbox\normalpagebox}

\OTRONEoutput
  {\sidefloatoutput}

%D Insertions

\newif\iftopofinsert

% \def\OTRONEdosettopinserts
%   {\bgroup
%    \ifsomefloatwaiting
%      \noffloatinserts\zerocount
%      \let\totaltopinserted\!!zeropoint
%      \OTRONEdodosettopinserts
%      \ifnum\@@bknbottom=\zerocount
%        \ifnum\@@bknlines>\zerocount
%          \ifdim\totaltopinserted>\zeropoint\relax
%            \dimen0=\lineheight
%            \dimen0=\@@bknlines\dimen0
%            \advance\dimen0 \totaltopinserted\relax
%            \ifdim\dimen0>\textheight
%              \showmessage\m!floatblocks8\@@bknlines
%              \vfilll\eject
%            \fi
%          \fi
%        \fi
%      \fi
%    \fi
%    \egroup}

\def\OTRONEdosettopinserts
  {\bgroup
   \ifsomefloatwaiting
     \noffloatinserts\zerocount
     \let\totaltopinserted\!!zeropoint
     \OTRONEdodosettopinserts
     \ifnum\@@bknbottom=\zerocount
       \ifnum\@@bknlines>\zerocount
         \ifdim\totaltopinserted>\zeropoint\relax
           \ifdim\dimexpr\@@bknlines\lineheight+\totaltopinserted\relax>\textheight
             \showmessage\m!floatblocks8\@@bknlines
             \vfilll\eject
           \fi
         \fi
       \fi
     \fi
   \fi
   \egroup}

\def\OTRONEdodosettopinserts
  {\ifnum\noffloatinserts<\noftopfloats
     \dogetfloat
     \ifdim\topinserted=\zeropoint
       \topofinserttrue
     \else
       \topofinsertfalse
     \fi
     \global\advance\topinserted\dimexpr\ht\floatbox+\dp\floatbox+\floatbottomskip\relax
     \ifdim\topinserted<\textheight\relax
       \xdef\totaltopinserted{\the\topinserted}%
       \insert\topins
         {\forgetall
          \iftopofinsert
            \topskipcorrection % [xx] new: see icare topbleed
            \kern-\lineskip\par
            \prevdepth\maxdimen
          \else
            %\blank[-\@@bkspaceafter,\@@bkspacebefore]% inserts can't look back
            \betweenfloatblanko
          \fi
          \flushfloatbox
          \blank[\@@bkspaceafter]}%
       \ifsomefloatwaiting
         \advance\noffloatinserts \plusone
       \else
         \noffloatinserts\noftopfloats\relax
       \fi
       \dofloatflushedinfo
     \else
       \doresavefloat
       \noffloatinserts\noftopfloats\relax
     \fi
   \else
     \ifsomefloatwaiting
       \showmessage\m!floatblocks6{\the\noftopfloats}%
     \fi
     \let\OTRONEdodosettopinserts\relax
   \fi
   \OTRONEdodosettopinserts}

\def\OTRONEdosetbotinserts
  {\bgroup
   \ifsomefloatwaiting
     \noffloatinserts\zerocount
     \OTRONEdodosetbotinserts
   \fi
   \egroup}

\def\OTRONEdodosetbotinserts
  {\ifnum\noffloatinserts<\nofbotfloats\relax
     \dogetfloat
     \global\advance\botinserted \ht\floatbox\relax
     \global\advance\botinserted \dp\floatbox\relax
     \global\advance\botinserted \floattopskip\relax
     \ifdim\botinserted<\pagegoal\relax
       \insert\botins
         {\forgetall
          \blank[\@@bkspacebefore]%
          \flushfloatbox}%
       \ifsomefloatwaiting
         \advance\noffloatinserts \plusone
       \else
         \noffloatinserts\nofbotfloats
       \fi
       \dofloatflushedinfo
     \else
       \doresavefloat
       \noffloatinserts\nofbotfloats\relax
     \fi
     \global\nofloatpermittedtrue % vgl topfloats s!
   \else
     \ifsomefloatwaiting
       \showmessage\m!floatblocks7{\the\nofbotfloats}%
     \fi
     \let\OTRONEdodosetbotinserts\relax
   \fi
   \OTRONEdodosetbotinserts}

\def\OTRONEdosetbothinserts
  {\global\topinserted\zeropoint
   \global\botinserted\zeropoint
   \ifflushingfloats \else
     \OTRONEdosettopinserts
     \OTRONEdosetbotinserts
     \ifsomefloatwaiting
        \doif\@@bkcache\v!no\doflushfloats
     \fi
   \fi}

% \def\OTRONEdotopinsertions
%   {\ifvoid\topins\else
%      \ifgridsnapping
%        \box\topins
%        \vskip-\topskip \vskip\strutheight % [xx] new: see icare topbleed
%      \else
%        \unvbox\topins
%      \fi
%    \fi
%    \global\topinserted\zeropoint}

\chardef\topinserttopskipmode=0 % 1 no topskip

\def\OTRONEdotopinsertions
  {\ifvoid\topins\else
     \ifgridsnapping
       \box\topins
       \vskip-\topskip
       \vskip\strutheight % [xx] new: see icare topbleed
     \else
       \ifcase\topinserttopskipmode
         % 0: default, do nothing
       \or
         % 1: no topskip (crossed fingers)
         \vskip-\topskip
         \vskip\strutheight
       \fi
       \unvbox\topins
     \fi
   \fi
   \global\topinserted\zeropoint}

\def\OTRONEdobotinsertions
  {\ifvoid\botins\else
     \ifgridsnapping
       \snaptogrid\hbox{\box\botins}%
     \else
       \unvbox\botins
     \fi
   \fi
   \global\botinserted\zeropoint
   \global\nofloatpermittedfalse}

\def\OTRONEdoflushfloats
  {\global\flushingfloatstrue
   \ifsomefloatwaiting
     \par
     % if kept, then option and definitely off in gridmode ! ! ! !
     % \ifvmode \prevdepth\maxdimen \fi % prevents whitespace; problematic in icare tests
     \OTRONEdodoflushfloats
   \fi
   \global\savednoffloats\zerocount
   \global\somefloatwaitingfalse
   \global\flushingfloatsfalse}

\def\OTRONEflushfloatbox % nog verder doorvoeren en meer info in marge
  {\ifcenterfloatbox \ifdim\wd\floatbox<\hsize
     \setbox\floatbox\hbox to \hsize{\hss\box\floatbox\hss}%
   \fi \fi
   \snaptogrid\hbox{\iftestfloatbox\ruledhbox\fi{\copy\floatbox}}}

\def\OTRONEdodoflushfloats % much in common with OTRSET
  {\ifsomefloatwaiting
     \ifpackflushedfloats
       \centerfloatboxfalse
       \dogetfloat
       \ifdim\wd\floatbox>\makeupwidth
         \global\setbox\floatbox\hbox to \makeupwidth{\hss\box\floatbox\hss}%
       \fi
       \OTRONEsetvsize
       \!!widtha\wd\floatbox
       \dofloatflushedinfo
       \doloop
         {\ifsomefloatwaiting
            \dosavefloatstatus
            \dogetfloat
            \advance\!!widtha 1em % variable
            \advance\!!widtha \wd\floatbox\relax
            \ifdim\!!widtha>\hsize
              \dorestorefloatstatus
              \global\somefloatwaitingtrue
              \exitloop
            \else
              \global\setbox\floatbox\hbox
                {\ifcase\columndirection % nog document wide
                   \ifvoid\savedfloatbox\else
                     \ifhbox\savedfloatbox\unhbox\else\box\fi\savedfloatbox\hfil
                   \fi
                   \ifhbox\floatbox\unhbox\else\box\fi\floatbox
                 \else
                   \ifhbox\floatbox\unhbox\else\box\fi\floatbox
                   \ifvoid\savedfloatbox\else
                     \hfil\ifhbox\savedfloatbox\unhbox\else\box\fi\savedfloatbox
                   \fi
                 \fi}%
              \dofloatflushedinfo
            \fi
          \else
            \exitloop
          \fi}%
       \global\setbox\floatbox\hbox to \hsize
         {\hfil\ifhbox\floatbox\unhbox\else\box\fi\floatbox\hfil}%
     \else
      %\bgroup % \box\floatbox can be in use!? messy
       \dogetfloat
      %\doplacefloatbox
      %\egroup
       \dofloatflushedinfo
     \fi
     % there is a chance that due to rounding errors, the float
     % fits on a page where it was first rejected, in which case
     % the prevdepth is -maxdimen and we cannot obey the grid
     \doplacefloatbox
     \expandafter\OTRONEdodoflushfloats
   \fi}

\def\OTRONEdocheckiffloatfits % vervangen ivm downward comp
  {\ifnofloatpermitted
     \global\roomforfloatfalse
   \else
     % new per 31/5/2004, should be an option, only one column mode
     \begingroup
     \scratchdimen\dimexpr\pagetotal+\lineheight\relax
     \ifdim\scratchdimen>\pagegoal
       \goodbreak % hack ? needed in icare-az
     \fi
     % should be an option
     \endgroup
     \dimen0\dimexpr\pagetotal+\ht\floatbox+\dp\floatbox+\floattopskip-\pageshrink\relax
    %\message{c:\the\mofcolumns,t:\the\pagetotal,g:\the\pagegoal}%\wait
     \dimen2\pagegoal
     \relax % needed
     \ifcase\textfloatmethod
       % method 0 : raw
     \or
       % method 1 : safe
       \dimen2 .99\pagegoal
     \or
       % method 2 : tight
       \advance\dimen0 -\onepoint
     \fi
     \relax % really needed ! ! ! !
     \ifdim\dimen0>\dimen2
       \global\roomforfloatfalse
     \else
       \global\roomforfloattrue
     \fi
   \fi}

\def\OTRONEflushsavedfloats
  {\dosetbothinserts}

% TODO: TEST FIRST, NO CORRECTION NEEDED IN GRID MODE, EVT OPTION

\def\OTRONEsomeherefloat[#1]% spacing between two successive must be better
  {\baselinecorrection                           % not really needed in grid mode:
  %\ifgridsnapping \else \baselinecorrection \fi % ! ! ! test test test ! ! ! !
   \doplacefloatbox
   \doinsertfloatinfo
   \dochecknextindentation\??bk
   \dorechecknextindentation}

% \def\OTRONEsomefixdfloat % [#1]
%   {\docheckiffloatfits
%    \ifroomforfloat\else
%      \goodbreak
%    \fi
%    \showmessage\m!floatblocks9\empty
%    \someherefloat} % [#1]
%
% better:
%
% \dorecurse{50}
%   {[before normal] \input thuan
%    \placefigure{normal}{\framed[height=1cm,width=8cm]{}}
%    \placefigure{normal}{\framed[height=2cm,width=8cm]{}}
%    [before force] \input thuan
%    \placefigure[force]{force}{\framed[height=8cm,width=8cm]{}}}

\chardef\fixedfloatmethod=3

% \def\OTRONEsomefixdfloat % [#1]
%   {\docheckiffloatfits
%    \ifroomforfloat\else
%      \ifzeropt\pagetotal
%        % let's assume that there is room
%      \else
%        \ifcase\fixedfloatmethod
%          % disabled
%        \or % 1 (old method)
%          \goodbreak
%        \or % 2 (safe method)
%          \page
%        \or % 3 (keeps in stream)
%          \vskip\textheight
%          \vskip-\textheight
%        \or % 4 (also keeps in place)
%           \dosomebreak\nobreak
%        \fi
%      \fi
%    \fi
%    \showmessage\m!floatblocks9\empty
%    \someherefloat} % [#1]

\def\OTRONEsomefixdfloat % [#1]
  {% there is (in mkii) no good way to prevent a break
   % so better fail than mess, we can get loose from
   % heads, so be it
   \showmessage\m!floatblocks9\empty
   \OTRONEsomeherefloat} % [#1]

\def\OTRONEsomesidefloat[#1]%  links, rechts     NOG TESTEN EN AANPASSEN
  {\ifinsidecolumns
     \someelsefloat[\v!here]%
   \else
    %\checkwaitingfloats{#1}%
     \def\logsidefloat
       {\doinsertfloatinfo}%
     \setbox\floatbox\vbox{\box\floatbox}%
     \wd\floatbox\floatwidth
     \processfirstactioninset
       [#1]
       [     \v!left=>\leftfloat       {\box\floatbox},
            \v!right=>\rightfloat      {\box\floatbox},
           \v!inleft=>\leftmarginfloat {\box\floatbox},
          \v!inright=>\rightmarginfloat{\box\floatbox},
       \v!leftmargin=>\leftmarginfloat {\box\floatbox},
      \v!rightmargin=>\rightmarginfloat{\box\floatbox},
         \v!leftedge=>\leftedgefloat   {\box\floatbox},
        \v!rightedge=>\rightedgefloat  {\box\floatbox},
        \v!backspace=>\backspacefloat  {\box\floatbox},
         \v!cutspace=>\cutspacefloat   {\box\floatbox},
         \v!inmargin=>\cutspacefloat   {\box\floatbox}]%
     \doifinset\v!tall{#1}\flushsidefloatsafterpar
   \fi}

\def\OTRONEdosomepagefloat#1[#2]%
  {%\checkwaitingfloats{#1}%
   \global\setbox#1\vbox
     {\unvbox#1%
      \vbox to \textheight
        {\doifnotinset\v!high{#2}\vfill
         \box\floatbox
         \doifnotinset\v!low{#2}\vfill}%
      \goodbreak}%
   \doinsertfloatinfo}

\def\OTRONEsomepagefloat     {\OTRONEdosomepagefloat\collectedpagefloats}
\def\OTRONEsomeleftpagefloat {\OTRONEdosomepagefloat\collectedleftpagefloats}
\def\OTRONEsomerightpagefloat{\OTRONEdosomepagefloat\collectedrightpagefloats}

\def\OTRONEsometopsfloat[#1]%
  {\ifdim\topinserted=\zeropoint
     \topofinserttrue
   \else
     \topofinsertfalse
   \fi
   \global\advance\topinserted \ht\floatbox
   \global\advance\topinserted \dp\floatbox
   \global\advance\topinserted \floatbottomskip
   \insert\topins
     {\forgetall
      \iftopofinsert
        \topskipcorrection % [xx] new: see icare topbleed
        \kern-\lineskip\par\prevdepth\maxdimen
      \else
        %\blank[-\@@bkspaceafter,\@@bkspacebefore]% inserts can't look back
        \betweenfloatblanko
      \fi
      \flushfloatbox
      \blank[\@@bkspaceafter]}%
   \doinsertfloatinfo}

\def\OTRONEsomebotsfloat[#1]%
  {\global\advance\botinserted \ht\floatbox
   \global\advance\botinserted \dp\floatbox
   \global\advance\botinserted \floattopskip
   \insert\botins
     {\forgetall
      \blank[\@@bkspacebefore]%
      \flushfloatbox}%
   %\global\nofloatpermittedtrue
   \doinsertfloatinfo}

\def\OTRONEsomefacefloat[#1]% untested
  {\startopposite\flushfloatbox\stopopposite}

\def\OTRONEnextcolumn[#1]%
  {}

\protect \endinput
