%D \module
%D   [       file=scrn-pag,
%D        version=1998.01.15,
%D          title=\CONTEXT\ Screen Macros,
%D       subtitle=Pages, % moved code
%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.

% pagecomments will be done differently

\writestatus{loading}{ConTeXt Screen Macros / Pages}

\registerctxluafile{scrn-pag}{autosuffix}

\unprotect

\installcorenamespace{interactionscreen}

\installparameterhandler \??interactionscreen {interactionscreen}
\installsetuphandler     \??interactionscreen {interactionscreen}

\newdimension\canvaswidth
\newdimension\canvasheight
\newdimension\canvasbackoffset
\newdimension\canvastopoffset
\newdimension\canvasmaxwidth
\newdimension\canvasmaxheight
\newdimension\canvascropoffset
\newdimension\canvastrimoffset
\newdimension\canvasbleedoffset
\newdimension\canvasartoffset

\mutable\lettonothing\currentinteractionscreendelay
\mutable\lettonothing\currentinteractionscreenheight
\mutable\lettonothing\currentinteractionscreenwidth

\newconditional\c_scrn_canvas_tight_page

\def\scrn_canvas_calculate
  {\begingroup
   \edef\currentinteractionscreenwidth {\interactionscreenparameter\c!width }%
   \edef\currentinteractionscreenheight{\interactionscreenparameter\c!height}%
   \canvasbackoffset\backspace
   \canvastopoffset\topspace
   \donetrue
   \ifx\currentinteractionscreenwidth\v!max
     \global\canvaswidth\printpaperwidth
   \orelse\ifx\currentinteractionscreenwidth\v!fit
     \donefalse
     \global\canvaswidth\dimexpr
       \leftcombitotal
     + \makeupwidth
     + \rightcombitotal
     + 2\dimexpr
          \interactionscreenparameter\c!backspace
        + \interactionscreenparameter\c!horoffset
        \relax
     \relax
   \orelse\ifx\currentinteractionscreenwidth\v!tight
     \donefalse
     \ifdim\backspace>\canvaswidth
       \ifdim\backspace>\zeropoint\relax
         \global\advanceby\canvasbackoffset -\canvaswidth
       \fi
     \fi
     \global\advanceby\canvaswidth\dimexpr
       \makeupwidth
     + \rightcombitotal
     + 2\dimexpr
          \interactionscreenparameter\c!backspace
        + \interactionscreenparameter\c!horoffset
        \relax
     \relax
   \else
     \donefalse
     \global\canvaswidth\currentinteractionscreenwidth
   \fi
   \ifx\currentinteractionscreenheight\v!max
     \global\canvasheight\printpaperheight
   \orelse\ifx\currentinteractionscreenheight\v!fit
     \donefalse
     \global\canvasheight\dimexpr
       \topheight
     + \topdistance
     + \makeupheight
     + \bottomdistance
     + \bottomheight
     + 2\dimexpr
          \interactionscreenparameter\c!topspace
        + \interactionscreenparameter\c!veroffset
        \relax
     \relax
   \orelse\ifx\currentinteractionscreenheight\v!tight
     \donefalse
     \global\canvasheight\dimexpr\topheight+\topdistance\relax
     \ifdim\topspace>\canvasheight
       \ifdim\topspace>\zeropoint\relax
         \global\advanceby\canvastopoffset -\canvasheight
       \fi
     \fi
     \global\advanceby\canvasheight\dimexpr
       \makeupheight
     + \bottomdistance
     + \bottomheight
     + 2\dimexpr
          \interactionscreenparameter\c!topspace
        + \interactionscreenparameter\c!veroffset
        \relax
     \relax
   \else
     \donefalse
     \global\canvasheight\currentinteractionscreenheight
   \fi
   \ifconditional\c_page_layouts_location_is_set
     \global\c_scrn_canvas_tight_page\conditionalfalse
   \fi
   \ifconditional\c_scrn_canvas_tight_page
     \global\canvasmaxwidth \paperwidth
     \global\canvasmaxheight\paperheight
   \else
     \global\canvasmaxwidth \printpaperwidth
     \global\canvasmaxheight\printpaperheight
   \fi
   % new:
   \ifdone
     \global\canvascropoffset \layoutcropoffset
     \global\canvastrimoffset \layouttrimoffset
     \global\canvasbleedoffset\layoutbleedoffset
     \global\canvasartoffset  \layoutartoffset
   \else
     \global\canvascropoffset \zeropoint
     \global\canvastrimoffset \zeropoint
     \global\canvasbleedoffset\zeropoint
     \global\canvasartoffset  \zeropoint
   \fi
   \endgroup}

\newdimension\layoutcropoffset
\newdimension\layouttrimoffset
\newdimension\layoutbleedoffset
\newdimension\layoutartoffset

\appendtoks
   \edef\p_cropoffset{\layoutparameter\c!cropoffset}%
   \ifx\p_cropoffset\v!auto
     \ifconditional\c_page_layouts_location_is_middle
       \scratchheight\dimexpr(\printpaperheight-\paperheight)/2\relax
       \scratchwidth \dimexpr(\printpaperwidth -\paperwidth )/2\relax
       \layoutcropoffset\ifdim\scratchheight>\scratchwidth\scratchheight\else\scratchwidth\fi
       \layouttrimoffset \layoutparameter\c!trimoffset
       \layoutbleedoffset\layoutparameter\c!bleedoffset
       \layoutartoffset  \layoutparameter\c!artoffset
     \else
       \writestatus\m!system{auto crop etc only supported when location=middle}%
       \layoutcropoffset \zeropoint
       \layouttrimoffset \zeropoint
       \layoutbleedoffset\zeropoint
       \layoutartoffset  \zeropoint
     \fi
   \else
     \layoutcropoffset \layoutparameter\c!cropoffset
     \layouttrimoffset \layoutparameter\c!trimoffset
     \layoutbleedoffset\layoutparameter\c!bleedoffset
     \layoutartoffset  \layoutparameter\c!artoffset
   \fi
   \relax
\to \everysetuplayout

\setuplayout
  [\c!cropoffset=\zeropoint,
   \c!trimoffset=\zeropoint,
   \c!bleedoffset=\zeropoint,
   \c!artoffset=\zeropoint]

% test the next when something is changed here:
%
% \setuppapersize[A5][A3]  \showframe
%
% % \setuplayout[location=middle] \setupinteractionscreen[option=fit]
% % \setuplayout[location=middle] \setupinteractionscreen[option=auto]
% %                               \setupinteractionscreen[option=fit]
% %                               \setupinteractionscreen[option=auto]
%
% \starttext \input ward \stoptext

\let\scrn_canvas_synchronize_simple \relax
\let\scrn_canvas_synchronize_complex\relax

\appendtoks
    \glet\scrn_canvas_synchronize_simple \scrn_canvas_synchronize_simple_indeed
    \glet\scrn_canvas_synchronize_complex\scrn_canvas_synchronize_complex_indeed
\to \everysetuplayout

\appendtoks
    \glet\scrn_canvas_synchronize_simple \scrn_canvas_synchronize_simple_indeed
    \glet\scrn_canvas_synchronize_complex\scrn_canvas_synchronize_complex_indeed
\to \everysetuplayouttarget

\def\scrn_canvas_synchronize_common {
    mode        {\interactionscreenparameter\c!option}%
    singlesided \ifsinglesided true\else false\fi\space
    doublesided \ifdoublesided true\else false\fi\space
    copies      \numexpr\interactionscreenparameter\c!copies\relax
    print       {\interactionscreenparameter\c!print}%
}

\appendtoks
    \lettonothing\scrn_canvas_synchronize_common
\to \everyaftershipout

\def\scrn_canvas_synchronize_only
  {\clf_setupcanvas
     paperwidth  \printpaperwidth
     paperheight \printpaperheight
     \scrn_canvas_synchronize_common
   \relax}

\def\scrn_canvas_synchronize_set#width#height%
  {\clf_setupcanvas
     paperwidth  \dimexpr#width\relax
     paperheight \dimexpr#height\relax
   \relax}

\let\scrn_canvas_synchronize_reset\scrn_canvas_synchronize_only

\def\scrn_canvas_synchronize_simple_indeed
  {\scrn_canvas_synchronize_only
  %\glet\scrn_canvas_synchronize_simple \relax
   \glet\scrn_canvas_synchronize_complex\relax}

\def\scrn_canvas_synchronize_complex_indeed
  {\scrn_canvas_calculate % otherwise we need to hook it into setuppage etc
   \clf_setupcanvas
        leftoffset  \canvasbackoffset
        topoffset   \canvastopoffset
        width       \canvaswidth
        height      \canvasheight
        paperwidth  \canvasmaxwidth
        paperheight \canvasmaxheight
        cropoffset  \canvascropoffset
        trimoffset  \canvastrimoffset
        bleedoffset \canvasbleedoffset
        artoffset   \canvasartoffset
        \scrn_canvas_synchronize_common
   \relax
  %\glet\scrn_canvas_synchronize_simple \relax
   % one shot only
   \glet\scrn_canvas_synchronize_complex\relax}

\appendtoks
    \begingroup
    \edef\p_option{\interactionscreenparameter\c!option}%
    \ifx\p_option\v!max
      \global\c_scrn_canvas_tight_page\conditionaltrue
    \orelse\ifx\p_option\v!fit
      \global\c_scrn_canvas_tight_page\conditionaltrue
    \else
      \global\c_scrn_canvas_tight_page\conditionalfalse
    \fi
    \endgroup
\to \everysetupinteractionscreen

\setupinteractionscreen
  [\c!width=\printpaperwidth,
   \c!height=\printpaperheight,
   \c!horoffset=\zeropoint,
   \c!veroffset=\zeropoint,
   \c!backspace=\backspace,
   \c!topspace=\topspace,
   \c!copies=\plusone, % not the best place but backend anyway
   \c!option=\v!auto]

\appendtoks
    \glet\scrn_canvas_synchronize_simple \scrn_canvas_synchronize_simple_indeed
    \glet\scrn_canvas_synchronize_complex\scrn_canvas_synchronize_complex_indeed
\to \everysetupinteractionscreen

%D Conditional page breaks:

\permanent\tolerant\protected\def\screen[#list]%
  {\iflocation
     \page[#list]%
   \fi}

%D Page transitions:

\lettonothing\scrn_transitions_list

\permanent\tolerant\protected\def\setuppagetransitions[#list]%
  {\edef\scrn_transitions_list{#list}%
   \ifx\scrn_transitions_list\v!reset
     \lettonothing\scrn_transitions_list
   \fi}

\def\scrn_transitions_set
  {\iflocation \ifempty\scrn_transitions_list \else
     \scrn_transitions_set_indeed
   \fi \fi}

\def\scrn_transitions_set_indeed
  {\begingroup
   \edef\currentinteractionscreendelay{\interactionscreenparameter\c!delay}%
   \clf_setpagetransition
      n     {\scrn_transitions_list}%
      delay \ifx\currentinteractionscreendelay\v!none \zerocount\else\currentinteractionscreendelay\fi
   \relax
   \endgroup}

\prependtoks
    \scrn_transitions_set
\to \everyshipout

\setupinteractionscreen
  [\c!delay=\v!none]

\setuppagetransitions
  [\v!reset]

\protect \endinput

