% \iffalse
%%
%% (C) Copyright 1999-2000 Frank Mittelbach, David Carlisle, Chris Rowley
%% All rights reserved.
%%
%% Not for general distribution. In its present form it is not allowed
%% to put this package onto CD or an archive without consulting the
%% the authors.
%% 
% \fi
%
%    \begin{macrocode}
\def\@tempa#1: #2.dtx,v #3 #4 #5 #6 #7${
  \ProvidesPackage{#2}[#4 #3 #5 #6]}
\@tempa$Id: xo-new.dtx,v 1.25 2000/08/11 07:14:23 latex3 Exp $
%    \end{macrocode}
%
% This package is available for test overwrites that need integration
% into the main packages at some point
%
% Ignore white space in this package.
%    \begin{macrocode}
\IgnoreWhiteSpace
%    \end{macrocode}
%    

%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% STUFF THAT CHANGES CHRIS' xo-final
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
%
%    \begin{macrocode}

\flushbottom


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%changes to xo-final
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\iftrue %  using Frank's changes built on 1.15

%<*obsolete>
\def\typeset@this@float@and@caption{
  \append@caption@to@float
  \box\this@captioned@float
}
%</obsolete>



\def\final@column {
%<*trace>
  \@tracepush{produce@final@column}
%</trace>
   \setbox\@outputbox \box\@cclv

   \ifvoid\footins
   \else
     \setbox\@outputbox \vbox {
       \boxmaxdepth \@maxdepth
%    \end{macrocode}
% \TeX\ calculates page breaks expecting the depth of the main text
% to extend below the page baseline, not contributing to the page height,
% and it assumes the entire height plus depth of footnotes will contribute
% to the page height.  When the page is assembled, the footnote depth 
% is below the baseline and the main text depth appears mid-page.  To
% make the contents fit the page precisely, we (locally) adjust the 
% footnote spacing \cs{skip}\cs{footins} by the \emph{difference} in
% the two depths.\footnote{This part of the code was suggested by
%    Donald Arseneau in bug report pr3189.}
%    \begin{macrocode}
       \advance\skip\footins -\dp\@outputbox % already limited to \@maxdepth
       \unvbox \@outputbox
       \advance\skip\footins 
          \ifdim\dp\footins>\@maxdepth \@maxdepth \else \dp\footins \fi
       \vskip \skip\footins
%    \end{macrocode}
%    If we are doing grid typesetting we put all excess space between
%    the text and the footnote(s).
%    \begin{macrocode}
       \ifdim \pagesetup@grid@point@sep > \z@
         \vfil
       \fi
       \color@begingroup
         \normalcolor
         \footnoterule
         \unvbox \footins
       \color@endgroup
      }
   \fi
%    \end{macrocode}
%
%   I am not sure if texttop/bottom should survive for long: something
%   more spohisictaed is needed these days.  This may mean that
%   footnotes need better handling too: what makes a complex column
%   look good?
%
%    \begin{macrocode}
   \global\setbox\csname col@box@\the\curr@col@count\endcsname
          \vbox to \csname col@ht@ \the\curr@col@count\endcsname %<-----
          { % this should one day become \@colht
                                % or else
           \@texttop
           \dimen@ \dp\@outputbox
           \unvbox \@outputbox
           \vskip -\dimen@
           \@textbottom
           }%
   \global \maxdepth \@maxdepth
%<*trace>
  \@tracepop{produce@final@column}
%</trace>
}
\let \produce@final@column \final@column



\def\make@area@floats@box #1#2 {
%<*trace>
  \@tracepush{make@area@floats@box}
  \tr@ce{Area:~ #1}
%</trace>
  \@ifundefined{area@#1}   % test only needed because of missing integration
     {
%<*trace>
      \tr@ce{Area:~ not~ set~ up}
%</trace>
     }
     {
  \setup@this@area{#1} % needed below when typesetting floats

  \setbox #2 \box\voidb@x                % <-------- FMi
%  \let \@elt \@comflelt  % Big aaah!
  \def \@elt { \typeset@float@in@box {#2} }
    \csname area@#1\endcsname
%    \end{macrocode}
%    Not very pretty yet!  But better when decoration
%    stuff is used.
%    \begin{macrocode}
  \let \@elt \relax

  \expandafter
  \release@floats
    \csname area@#1\endcsname
     }
%<*trace>
  \@tracepop{make@area@floats@box}
%</trace>
}


\def\typeset@float@in@box #1 #2 {
%<*trace>
  \@tracepush{typeset@float@in@box}
%</trace>
  \gdef\this@float@box {#2}         % <------------- FMi
  \expandafter
  \extract@this@float@actual@structure \the\toks #2

  \update@this@area@columns
     {
      \expandafter
      \gdecrement@num
        \csname col@ \this@area@type
            @floats@ \the\count@ @number\endcsname
%<*trace>
       \tr@ce{col@ \this@area@type
            @floats@ \the\count@ @number~ <-~
            \csname col@ \this@area@type
            @floats@ \the\count@ @number\endcsname}
%</trace>
     }

  \setbox #1 \vbox {
    \ifvoid #1 
%<*trace>
\tr@ce{add~to~#1:~float~ #2}
%</trace>
    \else       % experimental FMi
%<*trace>
\tr@ce{add~to~#1:~float~ #2~ plus~ \pagesetup@float@float@sep}
%</trace>
      \unvbox #1                            % experimental FMi
      \vskip \pagesetup@float@float@sep     % experimental FMi
    \fi                                     % experimental FMi
    \typeset@this@float@and@caption
    }
%<*trace>
  \@tracepop{typeset@float@in@box}
%</trace>
  }


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% my new xo-final stuff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\def \myfinalpage {
    \mbox@addtopage (\pagebodylefthpos, -\headsep)
       {page:~\thepage}
%    \end{macrocode}
%    
%    \begin{macrocode}
%    Init |\dimen|1,2,3,\ldots as vertical current point per column
%    (don't forget in Chris's (sorry, Don's) universum zero is at the
%    top).
%    \end{macrocode}
%    
%    \begin{macrocode}
  \forall@columns{
     \dimen \the\curr@col@count
            \z@
  }
%    \end{macrocode}
%    Now mount all top areas looping through an ordered list of top
%    areas.
%    \begin{macrocode}
  \expandafter\mount@top@areas@loop
      \top@areas
      \relax\relax\relax
%    \end{macrocode}
%    Next mount the text columns.
%    \begin{macrocode}
  \forall@columns{

%<*trace>
     \tr@ce{GRID (delta):~ column~\the\curr@col@count t:~
        \csname col@t@delta@ \the\curr@col@count 
        \endcsname\space used}
%</trace>
     \advance \dimen \the\curr@col@count 
              by %NEEDED!!!!!
              \csname col@t@delta@ \the\curr@col@count \endcsname
     
     \advance \dimen \the\curr@col@count
              \csname col@ht@ \the\curr@col@count \endcsname

     \setlength \@tempdima
        {\pagebodylefthpos - \columndisplacement
                           + \columndisplacement * \curr@col@count }

     \mbox@addtopage ( \the\@tempdima ,\the\dimen\curr@col@count  )
         { \box \csname col@box@ \the\curr@col@count \endcsname } 

%<*trace>
     \tr@ce{GRID (delta):~ column~\the\curr@col@count b:~
        \csname col@b@delta@ \the\curr@col@count 
        \endcsname\space used}
%</trace>
     \advance \dimen \the\curr@col@count 
              by %NEEDED!!!!!
              \csname col@b@delta@ \the\curr@col@count \endcsname
  }
%    \end{macrocode}
%    Then mount the bottom floats.
%    \begin{macrocode}
  \def\col@of@focus{0}               % temp solution see below
  \expandafter\mount@bot@areas@loop
      \bot@areas
      \relax\relax\relax
%    \end{macrocode}
%    
%    Next thing is absolutely temp: mount a marginal area on the right
%    (bottom) to allow footnotes there (this is just for testing).
%    \begin{macrocode}
  \setlength \@tempdima
     {\pagebodylefthpos + \columndisplacement * \col@count }
   \mbox@addtopage ( \the\@tempdima , \textheight )
      { \box \saved@footins } % TEMP FMi
%    
%    Next thing is absolutely temp: mount a marginal area on the right
%    (bottom) to allow marginal floats there (this is just for testing).
%    \begin{macrocode}
  \expandafter
  \ifx\csname area@m1\the\curr@col@count\endcsname\relax
  \else
    \curr@col@count\col@count
    \advance\curr@col@count\@ne
    \make@area@floats@box {m\the\curr@col@count1}\@tempboxa
    \setlength \@tempdima
       {\pagebodylefthpos + \columndisplacement * \col@count }
     \mbox@addtopage ( \the\@tempdima , \textheight )
        { \vbox to\textheight{\vfil \unvbox \@tempboxa \vfil }}
  \fi
%
  \ifShowGrid
    \ifdim \pagesetup@grid@point@sep > \z@
      \@tempcnta\textheight
      \@tempcntb\topskip
      \advance\@tempcnta -\@tempcntb
      \@tempdimb\pagesetup@grid@point@sep
      \@tempcntb\@tempdimb
      \divide\@tempcnta\@tempcntb
      \advance\@tempcnta\@ne

      \setlength \@tempdimb
       {\pagebodylefthpos + \columndisplacement * \col@count
                          - \columnsep }
      \count@\@tempdimb

      \sbox\grid@box{
               \setlength\unitlength{1sp}
               \begin{picture}(0,0)
                 \multiput(0,0)(0,\@tempcntb){\@tempcnta}
                   {\line(1,0){\count@}}
               \end{picture}
               }

      \mbox@addtopage ( 0pt ,\textheight  )
             {  \box\grid@box }

    \fi    
  \fi    
}

\newbox\grid@box

\newif\ifShowGrid
\ShowGridtrue

% for tracing only (unchanged)


\def\mbox@addtopage (#1,#2)#3{%
%  \tr@ce{box~being~added~to~page:~at~(#1,~#2) }
  \global \setbox\page@box
    \hbox {
      \unhbox \page@box
      \mbox@put  (#1,#2) {#3}
    }
}




\def\mount@top@areas@loop#1#2#3{
  \ifx#1\relax
  \else
    \mount@top@area#1#2#3
    \expandafter\mount@top@areas@loop
  \fi}


\def\mount@top@area#1#2#3 {
  \make@area@floats@box {#1#2#3}\@tempboxa

  \ifvoid \@tempboxa
  \else

    \setlength \@tempdimb
        { \dimen#2 + \ht\@tempboxa + \dp\@tempboxa }

    \setlength \@tempdima
        {\pagebodylefthpos - \columndisplacement + \columndisplacement * #2 }

    \mbox@addtopage ( \the\@tempdima ,\the\@tempdimb  )
         { \box \@tempboxa } 

    \count@ #2\relax
    \advance\count@ #3\relax
    \loop
      \advance\count@\m@ne
      \dimen \count@ =  \@tempdimb
      \advance\dimen \count@
          \ifnum \csname col@t@floats@ \the\count@ @number\endcsname > \z@
            \pagesetup@float@area@sep
          \else
            \pagesetup@float@text@sep
          \fi
    \ifnum #2 < \count@ 
    \repeat

  \fi
}

\def\mount@bot@areas@loop#1#2#3{
  \ifx#1\relax
  \else
    \mount@bot@area#1#2#3
    \expandafter\mount@bot@areas@loop
  \fi}

\def\mount@bot@area#1#2#3 {
  \make@area@floats@box {#1#2#3}\@tempboxa

  \ifvoid \@tempboxa
  \else

    \setlength \@tempdimb
        { \dimen#2 + \ht\@tempboxa + \dp\@tempboxa }

%    \end{macrocode}
%    There are probably much better ways to get the spacing right, the
%    one below is really only a temporary fix: in |\col@of@focus| we
%    remember the last column in which already applied
%    |\pagesetup@float@text@sep|, any additional float area will
%    contribute another |\pagesetup@float@area@sep|
%    instead.\footnote{To make this work it is absolutely necessary
%    that the floats in bot@areas are ordered by column!!! No-good. FIX!!}
%    \begin{macrocode}
    \ifnum \col@of@focus < #2 \relax
      \def\col@of@focus{#2}
      \advance\@tempdimb 
            \pagesetup@float@text@sep
    \else
      \advance\@tempdimb 
            \pagesetup@float@area@sep
    \fi

    \setlength \@tempdima
        {\pagebodylefthpos - \columndisplacement + \columndisplacement * #2 }

    \mbox@addtopage ( \the\@tempdima ,\the\@tempdimb  )
         { \box \@tempboxa } 

%    \end{macrocode}
%    Next loop is actually not necessary as long we keep the
%    restriction that we don't allow partial overlapping float areas, ie we
%    will never mount another float area in the columns that are
%    spanned by the current float area (other than potentially the
%    first and we set this one explicitly).
%    \begin{macrocode}
    \dimen #2 = \@tempdimb
%    \count@ #2\relax
%    \advance\count@ #3\relax
%    \loop
%      \advance\count@\m@ne
%      \dimen \count@ =  \@tempdimb
%    \ifnum #2 < \count@ 
%    \repeat

  \fi
}


% of course \top@areas and \bot@areas should be constructed
% automatically when preparing the \use@areas or from the \known@areas
% but for now i simply define them here:

\def\top@areas{t13 t12 t22 t32 t11 t21 t31}

\def\bot@areas{b11 b12 b13 b21 b22 b31 b32}


%%%%%%%%%%%%%%%%%%%%%%%%%%%
% next line activates my version (well some of it is activated already
% above) 
%%%%%%%%%%%%%%%%%%%%%%%%%%%
\let\make@page@box\myfinalpage

\fi % end of stuff modifying xo-final 1.15

%    \end{macrocode}
%

\endinput



% \begin{macro}{\savecolumnsizes}
% Declaration to turn on writing of CSL (column size list) files.
%    \begin{macrocode}
\def\savecolumnsizes{
  \newwrite\csl@file
  \immediate\openout\csl@file\jobname.csl
  \def\perhaps@write@to@csl@file{
     \immediate\write\csl@file
     }
  \def\perhaps@write@placements@to@csl@file{
    \def\@elt{\expandafter
              \perhaps@write@placements@to@csl@file@aux\the\toks}
    \perhaps@write@to@csl@file{
     ^^JPage:~\the\absolute@page@number\space (\the\c@page)^^J
%    \end{macrocode}
%    
%    \begin{macrocode}
       \expandafter\@write@areas\used@areas\relax\relax\relax
    }
    \let\@elt\relax}}
%    \end{macrocode}
% \end{macro}
%






\endinput


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% an attempt to use column 0 and n+1 as marginal areas
% not active
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% \begin{macro}{\forall@columns@and@margins}
%    \begin{macrocode}
\def\forall@columns@and@margins#1{
  \edef\saved@curr@col@count{\the\curr@col@count}
  \global\curr@col@count\col@count
  \global\advance\curr@col@count\@ne
  \@whilenum \curr@col@count>\m@ne \do
     {
      #1
      \global\advance\curr@col@count\m@ne
     }
  \global\curr@col@count\saved@curr@col@count\relax
}
%    \end{macrocode}
% \end{macro}
%
%
%
%    \begin{macrocode}
% original macro no longer used
\def\initialise@best@trial{
%<*trace>
  \@tracepush{initialise@best@trial}
%</trace>
%    \end{macrocode}
%    
%    \begin{macrocode}
  \global\let\best@trial\@empty
  \forall@columns@and@margins{
     \expandafter
     \g@addto@macro\expandafter\best@trial\expandafter
        {
         \expandafter
         \global
           \csname col@ht@ \the\curr@col@count\endcsname
           \textheight
        }
  }
%<*trace>
  \tr@ce{\meaning\best@trial}
  \@tracepop{initialise@best@trial}
%</trace>
}
%    \end{macrocode}
%

%    \begin{macrocode}
\expandafter\newdimen\csname col@ht@ 0\endcsname
%    \end{macrocode}
%    


% \begin{macro}{\initialise@next@page}
%    Initialises the column heights to text height. Clear the float
%    area lists, zero holdinginserts to allow footnote texts to arrive
%    in the insertion box.
%    \begin{macrocode}
\def\initialise@next@page{
%<*trace>
  \@tracepush{initialise@next@page}
%</trace>
%
  \forall@columns@and@margins
     {\global\csname col@ht@ \the\curr@col@count\endcsname\textheight}
%
  \initialise@areas
%
  \global\page@float@count\z@
%
  \initialise@best@trial
%    \end{macrocode}
%    
%    \begin{macrocode}
  \initialise@flush@data
%    \end{macrocode}
%    
%    \begin{macrocode}
  \initialise@here@data
%    \end{macrocode}
%    
%    \begin{macrocode}
  \initialise@footins@action
%    \end{macrocode}
%    
%    Nothing is closed when we are at the start of a page.
%    \begin{macrocode}
  \global\let\this@page@closed\@empty
%    \end{macrocode}
%    
%    \begin{macrocode}
  \let\@elt\initialise@float@class
    \float@classes@list
  \let\@elt\relax
%
%\showfloatlists
%\show\area@ddd\show\@activelist
% for safety
 \global\let\this@float@box\@undefined
%<*trace>
  \@tracepop{initialise@next@page}
%</trace>
  }
%    \end{macrocode}
%  \end{macro}
%
%
%

%
% \endinput
\endinput
%
% $Log: xo-new.dtx,v $
% Revision 1.25  2000/08/11 07:14:23  latex3
% added header
%
% Revision 1.24  2000/08/11 06:49:09  latex3
% untabify
%
% Revision 1.23  2000/08/05 10:01:26  latex3
% ensure that \make@area@floats@box doesn't do any harm to areas not set
% up via \DeclareFloatArea
%
% Revision 1.22  2000/08/04 15:50:31  latex3
% \flushbottom default again
%
% Revision 1.21  2000/08/04 13:58:58  latex3
% \bot@areas need to be ordered by column at the moment!!!!
%
% Revision 1.20  2000/08/04 10:20:09  latex3
% removed old experimental code for grid layout
%
% Revision 1.19  2000/07/22 06:30:50  latex3
% fixed bug in positioning bottom floats
%
% Revision 1.18  2000/07/19 17:12:53  latex3
% introduced float sequence list
%
% Revision 1.17  2000/07/10 19:22:12  latex3
% more grid support
%
% Revision 1.16  2000/07/04 19:48:55  latex3
% experimental stuff for GRIDs
% start writing out columnsizes (unused)
% use \update@this@area@columns
%
% Revision 1.15  2000/06/29 17:16:59  latex3
% introduced \setup@this@area
%
% Revision 1.14  2000/06/26 15:17:26  latex3
% prototype support for \pagesetup@float@area@sep
%
% Revision 1.13  2000/06/16 11:21:07  latex3
% rename \construct@and@test@col@height to \construct@and@test@col@ht
% rename \construct@and@test@col@heights to \construct@and@test@col@hts
% rename \cl@height1 to \@col@ht@1 (etc)
%
% Revision 1.12  2000/06/15 15:22:36  latex3
% implemented new semantics for area names
%
% Revision 1.11  2000/06/13 21:23:03  latex3
% *** empty log message ***
%
% Revision 1.10  2000/05/04 09:21:27  latex3
% urg: \this@area undefined inside the output routine, so captions got
% typeset with whatever was the last \this@area.
%
% Revision 1.9  2000/05/03 18:41:52  latex3
% still overwriting code for xo-final 1.15 ! (not 1.16)
% enabling support for caption templates
%
% Revision 1.8  2000/04/21 19:16:32  latex3
% activate my version of xo-final
%
% Revision 1.7  2000/04/21 19:05:21  latex3
% my version of xo-final mainly
%
% Revision 1.6  2000/04/09 20:02:42  latex3
% first draft of marginpar support
%
% Revision 1.5  2000/03/24 15:34:27  latex3
% version that starts supporting spans (still a hack yet)
%
% Revision 1.4  2000/02/26 18:26:08  david
% code moved to xo-*
%
% Revision 1.3  2000/02/13  21:37:20  latex3
% ooops, my ultrix rcs is too old to guess the comment char correctly
% so we better add explicit \endinput
%
% Revision 1.2  2000/02/13  21:35:25  latex3
% wording
%
% Revision 1.1  2000/02/13  21:34:51  latex3
% Initial revision
%
