% \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-page.dtx,v 1.34 2000/08/11 07:14:27 latex3 Exp $
%    \end{macrocode}
%
%
% Ignore white space in this package.
%    \begin{macrocode}
\IgnoreWhiteSpace
%    \end{macrocode}
%
%
%
% \subsection{Footnote setup}
%
% \begin{macro}{\initialise@footins@action}
%    The |\initialise@footins@action| is a hook to add code to the
%    page initialisation phase related to the footnote insertions. For
%    example, when all footnotes are accumulated in the last column we
%    need to make footnote insertions invisible when gathering
%    material for the first columns.
%    \begin{macrocode}
\let\initialise@footins@action\relax
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\deal@with@footins@action}
%    The |\deal@with@footins@action| hook is called in the
%    |\grab@column@or| and the |best@column@or| output routine to do
%    something to the accumulated footnote insertions if
%    necessary. I.e., it is called for every column.
%    \begin{macrocode}
\let\deal@with@footins@action\relax
%    \end{macrocode}
% \end{macro}
%
%
%    
%    \begin{macrocode}
\DeclareTemplateType{footnotesetup}{0}
%    \end{macrocode}
%
%
%
%    
%    \begin{macrocode}
\DeclareTemplate{footnotesetup}{std}{0}{
   text-sep    =+l \skip\footins,
   max-height  =+l \dimen\footins,
 }
 {

  \global\skip\footins 10pt plus 5pt
  \global\dimen\footins.7\textheight

  \DoParameterAssignments

  \let\initialise@footins@action\relax
  \let\deal@with@footins@action\relax

  \global\count\footins\@m
  
 }
%    \end{macrocode}
%
%    \begin{macrocode}
\DeclareTemplate{footnotesetup}{ftnright}{0}{
   text-sep    =+l \rcol@footinsskip,
   max-height  =+l \dimen\footins,
 }
 {

  \global\rcol@footinsskip 10pt plus 5pt
  \global\dimen\footins\textheight

  \DoParameterAssignments

  \let\initialise@footins@action\initialise@footins@ftnright
  \let\deal@with@footins@action\deal@with@footins@ftnright

 }
%    \end{macrocode}
%
%
%    Setting |max-height| as done below has not the desired effect and
%    needs investigation.\footnote{FIX!}
%    
%    \begin{macrocode}
\DeclareTemplate{footnotesetup}{margin}{0}{
   max-height  =+l \dimen\footins,
 }
 {

  \global\dimen\footins.7\textheight

  \DoParameterAssignments

  \let\initialise@footins@action\initialise@footins@margin
  \let\deal@with@footins@action\deal@with@footins@margin

%    \end{macrocode}
%    If the footnote do not appear within the columns we have to
%    ensure that the |\insert| does not change the page
%    calculations. This needs changes in two places:
%    \begin{macrocode}
  \global\count\footins\z@
  \global\skip\footins \z@
  
 }
%    \end{macrocode}
%
%
%
%
% \subsubsection{Footnote setup ftnright}
%
% In this section we provide the code for running all footnotes in the
% last column.
%
% \begin{macro}{\saved@footins}
%
%    \begin{macrocode}
\newbox\saved@footins
%    \end{macrocode} 
% \end{macro}
%
%
%
% \begin{macro}{\rcol@footinsskip}
%    \begin{macrocode}
\newskip\rcol@footinsskip
%    \end{macrocode} 
% \end{macro}
%
%
%
%
%
% \begin{macro}{\initialise@footins@ftnright}
%    \begin{macrocode}
\def \initialise@footins@ftnright {
  \global\setbox\saved@footins\box\voidb@x
  \global\skip\footins\z@
  \global\count\footins\z@
  \global\dimen\footins\textheight
}
%    \end{macrocode} 
% \end{macro}
%
%
%
%
%
% \begin{macro}{\deal@with@footins@ftnright}
%    \begin{macrocode}
\def \deal@with@footins@ftnright {
%<*trace>
  \@tracepush{deal@with@footins@ftnright}
%</trace>
%    \end{macrocode}
%    If we are not in the last column we save any footnote accumulated
%    in |\footins| in a separate |\saved@footins| box. This way any
%    later code that checks |\footins| for constraints will not fire
%    incorrectly.
%    \begin{macrocode}
  \ifnum\curr@col@count<\col@count
%<*trace>
    \tr@ce{not~ last~ col}         
%</trace>
    \ifvoid\footins
    \else
      \global\setbox\saved@footins
        \vbox{\unvbox\saved@footins
              \unvbox\footins}
    \fi
%    \end{macrocode}
%    If we are in the second last column we have to change the
%    procedure: we now have to return all saved footnotes to the MVL
%    and modify the insertion register values so that from now on
%    footnotes need space in the column.\footnote{Not correctly
%    handled is the case that we have accumulated more than a full
%    column of footnotes before!}
%    \begin{macrocode}
    \count@\col@count
    \advance\count@\m@ne
    \ifnum\curr@col@count=\count@ % cur col = second last col  
%<*trace>
       \tr@ce{second~ last~ col}         
%</trace>
       \global\count\footins\@m
       \global\skip\footins\rcol@footinsskip
       \ifvoid\saved@footins
       \else
         \insert\footins{\unvbox\saved@footins}
       \fi
    \fi
  \fi
%<*trace>
  \@tracepop{deal@with@footins@ftnright}
%</trace>
}
%    \end{macrocode} 
% \end{macro}
%
%
%
%
% \begin{macro}{\deal@with@footins@margin}
%    \begin{macrocode}
\def \deal@with@footins@margin {
%<*trace>
  \@tracepush{deal@with@footins@margin}
%</trace>
  \ifvoid\footins
  \else
    \global\setbox\saved@footins
      \vbox{\unvbox\saved@footins
            \unvbox\footins}
  \fi
%<*trace>
  \@tracepop{deal@with@footins@margin}
%</trace>
}
%    \end{macrocode} 
% \end{macro}
%
%
%
%
% \begin{macro}{\deal@with@footins@margin}
%    \begin{macrocode}
\def \initialise@footins@margin {
  \global\setbox\saved@footins\box\voidb@x
}
%    \end{macrocode} 
% \end{macro}
%
%
%
%
% \subsection{A basic page setup template}
%
%
% \begin{macro}{\test@named@key}
%    This should perhaps be made more general
%    \begin{macrocode}
\def\test@named@key #1#2#3#4 {
    \global\expandafter\let \expandafter #1
       \csname #2 \endcsname
    \ifx #1 \relax
      \PackageError{template}{#3~ unknown}
        {#3~ can~ be~ either~ #4} 
    \fi
}
%    \end{macrocode}
% \end{macro}
%
%
%
%
% \begin{macro}{\DeclareFloatArea}
%    We declare new float areas and (for the moment) internally use the
%    template mechanism. But this is more or less overkill and should
%    probably be replaced in the end.
%    \begin{macrocode}
\def\DeclareFloatArea#1#2{
 \xdef\known@areas{\known@areas#1}
 \DeclareInstance{floatareasetup}{#1}{std}
   { name = #1,  #2 }
%    \end{macrocode}
%    We need to do some intialisation here in case the area isn't
%    really used. (Since it is added to |\known@areas| it will be
%    considered and that will die if the initialisation isn't done)
%    \begin{macrocode}
 \UseInstance{floatareasetup}{#1}
}
%    \end{macrocode}
% \end{macro}
%
%
%    
%    \begin{macrocode}
\DeclareTemplateType{floatareasetup}{0}
%    \end{macrocode}
%
%    \begin{macrocode}
\DeclareTemplate{floatareasetup}{std}{0}{
   name            =n \floatareasetup@name,
   class-close-list=n \floatareasetup@class@close,
   all-close-list  =n \floatareasetup@all@close,
   max-float-num   =C \floatareasetup@maxfloats,
 }
 {

  \DoParameterAssignments
%    \end{macrocode}
%    
%    Declare area data structures if necessary\ldots
%    \begin{macrocode}
  \@ifundefined{\floatareasetup@name @float@count}
      {\expandafter\newcount\csname \floatareasetup@name
                                     @float@count\endcsname}
      {}
%    \end{macrocode}
%    \ldots and initialise them
%    \begin{macrocode}
  \global\expandafter\let
     \csname pagesetup@ \floatareasetup@name @float@num\endcsname
        \floatareasetup@maxfloats
  \global\expandafter\let
     \csname pagesetup@ \floatareasetup@name @class@close\endcsname
        \floatareasetup@class@close
  \global\expandafter\let
     \csname pagesetup@ \floatareasetup@name @all@close\endcsname
        \floatareasetup@all@close
%    \end{macrocode}
%    Let's not forget that area itself (some macros wouldn't like if
%    this is |\relax| rather than |\@empty|.
%    \begin{macrocode}
  \global\expandafter\let
       \csname area@\floatareasetup@name\endcsname
       \@empty
}
%    \end{macrocode}
%
%    
%    \begin{macrocode}
\DeclareTemplateType{pagesetup2}{0}
%    \end{macrocode}
%
%    \begin{macrocode}
\DeclareTemplate{pagesetup2}{std}{0}{
   column-num    =+c \col@count,
   column-width  =+l \columnwidth,
   column-height =+l \textheight,          % FMi tmp
   column-sep    =+l \columnsep,
   max-float-num =C \pagesetup@max@float@num,
   float-callout-constraint            =n \pagesetup@float@callout,
   float-callout-span-constraint       =n \pagesetup@float@callout@span,
   bottom-float-footnote-constraint    =n \pagesetup@float@footnote,
   flush-float-callout-constraint      =n \pagesetup@flush@float@callout,
   flush-float-callout-span-constraint =n \pagesetup@flush@float@callout@span,
   flush-bottom-float-footnote-constraint =n
                                         \pagesetup@flush@float@footnote,
% 
%    \end{macrocode}
%
%    \begin{macrocode}
   area-list =n \pagesetup@area@list,
%    \end{macrocode}
%    \begin{macrocode}
   defer-class-close-list=n \pagesetup@ddd@class@close,
   defer-all-close-list  =n \pagesetup@ddd@all@close,
%    \end{macrocode}
%    
%    \begin{macrocode}
   footnote-setup =i {footnotesetup} \pagesetup@footnote@setup,
%    \end{macrocode}
%    
%    \begin{macrocode}
   float-float-sep        =+l \pagesetup@float@float@sep,
   float-area-sep         =+l \pagesetup@float@area@sep,
   float-text-sep         =+l \pagesetup@float@text@sep,
   float-inline-sep       =+l \pagesetup@float@inline@sep,
%    \end{macrocode}
%    
%    \begin{macrocode}
   grid-point-sep   =+L \pagesetup@grid@point@sep,
  }
  {
%    \end{macrocode}
%    We start off with ensuring that we are at the top of a page. This
%    is a temporary solution until after pagesetup is more settled and
%    those bits which are only possible if there is nothing left on
%    recent contributions are properly separated from those things
%    which can be changed midway.
%    \begin{macrocode}
    \newpage

    \global\col@count\@ne
    \def\pagesetup@max@float@num{3}
    \def\pagesetup@float@callout{column}
    \def\pagesetup@float@footnote{none}
    \def\pagesetup@flush@float@callout{\pagesetup@float@callout}
    \def\pagesetup@flush@float@footnote{\pagesetup@float@footnote}
    \def\pagesetup@float@callout@span{flexible}
    \def\pagesetup@flush@float@callout@span{\pagesetup@float@callout@span}
%    \end{macrocode}
%
%    \begin{macrocode}
    \let\pagesetup@ddd@class@close\maxdimen %impossible value
    \let\pagesetup@ddd@all@close\@empty
%    \end{macrocode}
%    
%    \begin{macrocode}
% tmp:
    \def\pagesetup@footnote@setup{\UseTemplate{footnotesetup}{std}{}}
%    \end{macrocode}
%    
%    \begin{macrocode}
    \global\let\pagesetup@grid@point@sep\z@  % no grid points by default

    \DoParameterAssignments
%    \end{macrocode}
%    
%    \begin{macrocode}
     \ifdim \pagesetup@grid@point@sep = \z@
       \global\let\snap@to@grid\dummy@snap@to@grid
%    \end{macrocode}
%    Next two lines need some refinement, we need some mechanism to
%    fully disable those grid points. |\IgnoreAlignToGrid| doesn't do
%    the trick.\footnote{FIX!}
%    \begin{macrocode}
       \IgnoreAlignToGrid
       \global\let\std@AlignToGrid\relax
     \else
       \global\let\snap@to@grid\std@snap@to@grid
       \ObeyAlignToGrid
     \fi
%    \end{macrocode}
%    Split the |\pagesetup@float@text@sep| specification and store the
%    minus part in |\pagesetup@float@text@shrink|. Right now the plus
%    part is not used. Actually this is only really necessary if we do
%    grid typesetting but we do it anyway so that the spec is always
%    checked for offending infinite stretch.
%    \begin{macrocode}
    \split@skip{float-text-sep}
               \pagesetup@float@text@sep
               \dimen@
               {\global\pagesetup@float@text@shrink}
%    \end{macrocode}
%    
%    \begin{macrocode}
    \split@skip{float-inline-sep}
               \pagesetup@float@inline@sep
               \dimen@
               {\global\pagesetup@float@inline@shrink}
    \global\pagesetup@float@inline@sep \@ne\pagesetup@float@inline@sep
%    \end{macrocode}
%
%    \begin{macrocode}
    \global\hsize\columnwidth
    \global\linewidth\columnwidth

% all taken away from AtBeginDocument (where it is was added in xo-final)
  \setlength \@tempdima {\headheight + \headsep}
  \xdef \pagebodytopvpos {\the\@tempdima}

  \setlength \@tempdima {\@tempdima + \textheight}
  \xdef \pagebodybotvpos {\the\@tempdima}

%% horiz
  \setlength \@tempdima {\columnwidth + \columnsep}
  \xdef \columndisplacement {\the\@tempdima}

%    \end{macrocode}
%    
%    Perhaps we could use |\known@areas| instead and save the
%    test (would need a change in xo-place as well!).\footnote{Check!}
%    \begin{macrocode}
    \ifx\pagesetup@ddd@class@close\maxdimen %  not set?
       \let\pagesetup@ddd@class@close\pagesetup@area@list
    \fi
%    \end{macrocode}
%    
%    \begin{macrocode}
    \test@named@key
        \std@check@callout@constraints
        {check@callout@\pagesetup@float@callout}
        {float-callout-constraint}
        {`none',~ `page',~ `column',~ or~ `after'}
%    \end{macrocode}
%    
%    \begin{macrocode}
    \test@named@key
        \relaxed@check@callout@constraints
        {check@callout@\pagesetup@flush@float@callout}
        {flush-float-callout-constraint}
        {`none',~ `page',~ `column',~ or~ `after'}
%    \end{macrocode}
%    
%    \begin{macrocode}
    \test@named@key
        \std@check@some@constraints
        {check@float@footnote@\pagesetup@float@footnote}
        {bottom-float-footnote-constraint}
        {`none'~ or~ `forbidden'}
%    \end{macrocode}
%    
%    \begin{macrocode}
    \test@named@key
        \relaxed@check@some@constraints
        {check@float@footnote@\pagesetup@flush@float@footnote}
        {flush-bottom-float-footnote-constraint}
        {`none'~ or~ `forbidden'}
%    \end{macrocode}
%    
%    \begin{macrocode}
    \test@named@key
        \std@calculate@target@fl@column
        {calculate@target@fl@column@\pagesetup@float@callout@span}
        {float-callout-span-constraint}
        {`strict'~ or~ `flexible'}
%    \end{macrocode}
%    
%    \begin{macrocode}
    \test@named@key
        \relaxed@calculate@target@fl@column
        {calculate@target@fl@column@\pagesetup@flush@float@callout@span}
        {flush-float-callout-span-constraint}
        {`strict'~ or~ `flexible'}
%    \end{macrocode}
%    
%    \begin{macrocode}
    \global\let\check@some@constraints\std@check@some@constraints
    \global\let\check@callout@constraints\std@check@callout@constraints
    \global\let\calculate@target@fl@column\std@calculate@target@fl@column
%    \end{macrocode}
%    
%    \begin{macrocode}
    \global\let\used@areas\@empty
%    \end{macrocode}
%    
%    \begin{macrocode}
    \expandafter
    \@for \expandafter \@tempa \expandafter:\expandafter=
           \pagesetup@area@list \do
        {
%    \end{macrocode}
%    Perhaps the instance  should be called the moment we declare the
%    area. on the other hand calling it within the page setup would
%    allow to use collections!\footnote{Think and decide}
%    \begin{macrocode}
          \xdef\used@areas{\used@areas\@tempa} 
% after next line \@tempa is gone ... David, template.sty needs a cleanup
          \UseInstance{floatareasetup}{\@tempa}
%\show\@tempa
        }

%    \end{macrocode}
%    
%    \begin{macrocode}
    \pagesetup@footnote@setup
%    \end{macrocode}
%
%    Finally another bit of the initalisation that we have to redo at
%    this point since now some of the values may have
%    changed.\footnote{Temporary solution until the initialisation bit
%    and the pagesetup is properly sorted out.}
%    \begin{macrocode}
    \initialise@next@page
    \setup@collection@or
  }
%
%    \end{macrocode}
%    
%    \begin{macrocode}
\endinput
%    \end{macrocode}
%
% \endinput
%
% $Log: xo-page.dtx,v $
% Revision 1.34  2000/08/11 07:14:27  latex3
% added header
%
% Revision 1.33  2000/08/07 19:50:48  latex3
% setup the collection@or again when a pagesetup is selected since its
% parameter might have changed.
%
% Revision 1.32  2000/08/06 19:02:51  latex3
% move parts initialisation to \begin{document}
% move \newpage in pagesetup template first to ensure that previous page
% is finished with old values
%
% Revision 1.31  2000/08/06 09:02:57  latex3
% documented that footnotehandling needs fixing
%
% Revision 1.30  2000/08/04 10:23:24  latex3
% removed float-text-shrink and float-inline-shrink keys and instead
% used \split@skip
%
% Revision 1.29  2000/07/26 20:45:51  latex3
% first implementation of grid point support
%
% Revision 1.28  2000/07/21 13:52:53  latex3
% more renaming for float sequence class concept
%
% Revision 1.27  2000/07/18 21:07:03  latex3
% introduced float sequence classes
%
% Revision 1.26  2000/07/12 16:47:44  latex3
% use grid points properly in template
%
% Revision 1.25  2000/07/10 21:04:30  latex3
% integration of float specing parameters in page setup
% support for grid typesetting in page setup
%
% Revision 1.24  2000/07/04 19:44:58  latex3
% fix marginal footnote template (did change column calculations)
%
% Revision 1.23  2000/06/26 15:01:29  latex3
% docu update
%
% Revision 1.22  2000/06/21 09:56:09  latex3
% update docu
%
% Revision 1.21  2000/06/18 15:00:35  latex3
% move "Bottom float footnote relation" to xo-place file
%
% Revision 1.20  2000/06/16 11:06:05  latex3
% support float-callout-span-constraint
%
% Revision 1.19  2000/06/15 17:49:55  latex3
% moved macros about placment checking to xo-place since they don't
% belong here
%
% Revision 1.18  2000/06/15 15:21:33  latex3
% implemented new semantics for area names
%
% Revision 1.17  2000/06/13 21:21:08  latex3
% added support for columnheight (tmp)
% define \pagebodytopvpos etc inside template so that it can change
%
% Revision 1.16  2000/05/03 18:48:42  latex3
% initialise float area data structure the moment the float area is
% declared (otherwise you can't declare areas if they are not used!)
%
% add keys defer-type-close-list and defer-all-close-list to pagesetup2
% template
%
% Revision 1.15  2000/04/21 19:03:58  latex3
% added footnote setup
% cleanup pagesetup code
%
% Revision 1.14  2000/04/08 10:48:42  latex3
% code cleanup
%
% Revision 1.13  2000/04/08 08:16:53  latex3
% default for \pagesetup@flush@float@callout changed
%
% Revision 1.12  2000/03/31 17:07:24  latex3
% don't fail in footnote/float test if there isn't any bottom area at
% all!
%
% Revision 1.11  2000/03/24 15:34:27  latex3
% version that starts supporting spans (still a hack yet)
%
% Revision 1.10  2000/03/22 15:51:51  latex3
% some normalisations of names
%
% Revision 1.9  2000/03/17 20:25:02  latex3
% initialise fl@0
% support relaxing constraints when flushing
%
% Revision 1.8  2000/03/15 19:58:00  latex3
% assigning values to \column@number should always be done globally
%
% Revision 1.7  2000/03/05 19:41:21  latex3
% support multiple columns (6 max right now)
%
% Revision 1.6  2000/02/26 18:21:16  david
% fixed footnote bottom constraint code
%
% Revision 1.5  2000/02/26 16:52:25  david
% *** empty log message ***
%
% Revision 1.4  2000/02/17 22:41:38  david
% *** empty log message ***
%
% Revision 1.4  2000/02/16  13:41:06  latex3
% added 3col support
%
% Revision 1.3  2000/02/13  14:38:53  latex3
% was 1.2.1.2
%
% Revision 1.2.1.2  2000/02/13  11:51:27  latex3
% fixed \check@callout@after (hopefully correct now)
% added \check@some@constraints logic
%
