%D \module
%D   [       file=page-app, % from meta-fig
%D        version=1998.01.15,
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Independent page building,
%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 / Applications}

%D This needs an update with a proper define and inheritance.

\unprotect

\installcorenamespace{fittingpage}

\installframedcommandhandler \??fittingpage {fittingpage} \??fittingpage

\newdimension\d_page_fitting_width
\newdimension\d_page_fitting_height
\newbox      \b_page_fitting

\definepapersize
  [fittingpage]
  [\c!width=\d_page_fitting_width,
   \c!height=\d_page_fitting_height]

\definemakeup
  [fittingpage]
  [\c!textstate=\v!empty,
   \c!doublesided=\v!no,
   \c!location=\v!top, % no topskip
   \c!pagestate=\fittingpageparameter\c!pagestate]%

\definelayout
  [fittingpage]
  [\v!page]
  [\c!width=\d_page_fitting_width,
   \c!height=\d_page_fitting_height,
   \c!location=\v!middle]

\appendtoks
    \protected\frozen\instance\edefcsname\e!start\currentfittingpage\endcsname{\startfittingpage[\currentfittingpage]}%
    \protected\frozen\instance\edefcsname\e!stop \currentfittingpage\endcsname{\stopfittingpage}%
\to \everydefinefittingpage

\permanent\tolerant\protected\def\startfittingpage[#1]#*[#S#2]%
  {\page % this is kind of tricky! there can be preceding page refs
   \autostarttext
   \bgroup % resulting in a zero height page; test fig-make !
   \setupinterlinespace[\v!top=0]% no topskip
   \cdef\currentfittingpage{#1}%
   \dontcomplain
   % runs under current page regime, i.e. page variables passed to mp
   \setupcurrentfittingpage[\c!paper=,#2]% auto or size or nothing
   \ifempty{\fittingpageparameter\c!command}%
     \expandafter\page_fitting_start_normal
   \else
     \expandafter\page_fitting_start_command
   \fi}

\def\page_fitting_start_command
  {\grabuntil{\e!stop\currentfittingpage}\page_fitting_stop_command}

\def\page_fitting_stop_command#1%
  {\page_fitting_start_normal
   \fittingpageparameter\c!command{#1}%
   \page_fitting_stop_normal}

\def\page_fitting_start_normal
   {\setbox\b_page_fitting\hbox
      \bgroup
      \inheritedfittingpageframed
      \bgroup
      \ignorepars}

\protected\def\page_fitting_stop_normal % todo: figure out why a small mp image gets shifted
  {\removeunwantedspaces
   \egroup
   \egroup
   % finalize
   \ifcstok{\fittingpageparameter\c!margin}\v!page
     \setbox\b_page_fitting\hpack\bgroup
       \offsetbox
         [\c!leftoffset=\backspace,
          \c!rightoffset=\cutspace,
          \c!topoffset=\topspace,
          \c!bottomoffset=\bottomspace]
         {\box\b_page_fitting}%
     \egroup
   \fi
   \ifempty{\fittingpageparameter\c!scale}\else
     \setbox\b_page_fitting\hpack\bgroup
       \scale[\c!scale=\fittingpageparameter\c!scale]{\box\b_page_fitting}%
     \egroup
   \fi
   \ifzeropt\ht\b_page_fitting
     \ht\b_page_fitting\onepoint
   \fi
   \ifzeropt\wd\b_page_fitting
     \wd\b_page_fitting\onepoint
   \fi
   \d_page_fitting_width \wd\b_page_fitting
 % \d_page_fitting_height\ht\b_page_fitting
   % a change if plan ...
   \d_page_fitting_height\htdp\b_page_fitting
   \startlocallayout % hm, we change the papersize so we still need it
   \enforced\let\checkcurrentlayout\relax % else interference with odd/even layout
   \processaction
     [\fittingpageparameter\c!paper]
     [   \v!auto=>\let\scratchstringone\printpapersize,
      \s!unknown=>\let\scratchstringone\commalistelement,
      \s!default=>\def\scratchstringone{fittingpage}]%
   \expanded{\setuppapersize[fittingpage][\scratchstringone]}%
   %
   \edef\p_label{\fittingpageparameter\c!label}%
   \ifempty\p_label\else
     \expanded{\setuppagenumber[\c!label=\p_label]}%
   \fi
   %
   \startmakeup[fittingpage]%
     \box\b_page_fitting
   \stopmakeup
   \stoplocallayout % we need to get rid of this and use the built-in
   \egroup
   \autostoptext}

\permanent\let\stopfittingpage\page_fitting_stop_normal

\setupfittingpage
  [%\c!scale=1000,
   %\c!pagestate=,
   \c!strut=\v!none, % was no, but we get a noindent then (with the danger of a lineskip)
   \c!align=\v!normal,
   \c!offset=\v!overlay,
   \c!width=\v!fit,
   \c!height=\v!fit,
   \c!frame=\v!off]

%D \TEX\ pages (for \METAPOST\ pages, see \type {meta-fig}):

%D \starttyping
%D \startTEXpage[align=normal,width=900pt,autowidth=force]
%D     \framed[width=800pt,frame=off]{\number\dimexpr800pt}\par
%D     \input tufte\par
%D \stopTEXpage
%D \stoptyping
%D
%D Beware: the width is the textwidth!

\definefittingpage
  [TEXpage]
  [\c!align=\v!normal] % needed, else problems !

\permanent\protected\def\setupTEXpage
  {\setupfittingpage[TEXpage]}

%D For Mojca:
%D
%D \starttyping
%D \startTEXstream \chapter{Bla} Hello world!\crlf Hello world! \stopTEXstream
%D \startTEXpage   \chapter{Bla} Hello world!\crlf Hello world! \stopTEXpage
%D \stoptyping
%D
%D maybe I should support stream=yes in framed.

\permanent\tolerant\protected\def\startTEXstream[#1]% old code, to be redone
  {\page
   \enableoutputstream[tex]}

\permanent\protected\def\stopTEXstream
  {\disableoutputstream
   \startTEXpage % [\c!align=\v!normal]
     \outputstreamunvbox[tex]%
   \stopTEXpage}

%D Application pages, a quick \MKIV\ hack:

\definetypesetting[TEXapplication]
\definebuffer[TEXapplication]

% we could use a counter and saves runs on numbering them.

\permanent\def\TEXapplicationfilename{\jobname-texapplication.tex}

\permanent\protected\def\stopTEXapplication
  {\savebuffer[\thedefinedbuffer{TEXapplication}][\TEXapplicationfilename]%
   \typesetfile[TEXapplication][\TEXapplicationfilename]\relax}

%D \macros
%D   {startpagefigure}
%D
%D \starttyping
%D \starttext \pagefigure[two.1] \stoptext
%D \stoptyping

\defineexternalfigure[\v!page:\v!figure][\c!offset=\v!overlay] % we force a parent

\permanent\tolerant\protected\def\startpagefigure[#1]#*[#S#2]%
  {\bgroup
   \setupexternalfigure[\v!page:\v!figure][\c!offset=\v!overlay,#2]%
   \startTEXpage[\c!offset=\namedexternalfigureparameter{\v!page:\v!figure}\c!offset,\c!align=\v!normal]%
     \externalfigure[#1][\v!page:\v!figure]\ignorespaces} % so we can put some text below the graphic

\permanent\protected\def\stoppagefigure
  {\stopTEXpage
   \egroup}

\permanent\tolerant\protected\def\pagefigure[#1]#*[#S#2]%
  {\startpagefigure[#1][#2]\stoppagefigure}

\protect \endinput
