%D \module
%D   [       file=page-run,
%D        version=2000.10.20,
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Runtime Macros,
%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 / Runtime Macros}

\registerctxluafile{page-run}{} % maybe rename this one

\unprotect

\permanent\protected\tolerant\gdef\showprint[#1]#*[#2]#*[#3]% only english
  {\setbuffer[crap]%
      \unprotect
        \definepapersize[X][\c!width=4em, \c!height=6em]
        \definepapersize[Y][\c!width=12em,\c!height=14em]
        \setuppapersize[X,#1][Y,#2]
        \setuplayout[#3]
        \setuplayout[\v!page]
        \framed
          [\c!offset=\v!overlay,\c!strut=\v!no,
           \c!width=\paperwidth,\c!height=\paperheight]
          {\ss ABC\par DEF}%
      \protect
   \endbuffer
   \framed[\c!offset=\v!overlay,\c!strut=\v!no]{\typesetbuffer[crap]}}

% \switchtobodyfont[8pt]
%
% \startcombination[4*4]
%   {\showprint}                                      {\strut}
%   {\showprint[][][location=middle]}                 {\type{location=middle}}
%   {\showprint[][][location=middle,marking=on]}      {\type{markering=aan}\break\type{location=middle}}
%   {\showprint[][][location=middle,marking=on,nx=2]} {\type{markering=aan}\break\type{location=middle}\break\type{nx=2}}
%   {\showprint[][][location=left]}                   {\type{location=left}}
%   {\showprint[][][location=right]}                  {\type{location=right}}
%   {\showprint[][][location={left,bottom}]}          {\type{location={left,bottom}}}
%   {\showprint[][][location={right,bottom}]}         {\type{location={right,bottom}}}
%   {\showprint[][][nx=2,ny=1]}                       {\type{nx=2,ny=1}}
%   {\showprint[][][nx=1,ny=2]}                       {\type{nx=1,ny=2}}
%   {\showprint[][][nx=2,ny=2]}                       {\type{nx=2,ny=2}}
%   {\showprint[][][nx=2,ny=2,location=middle]}       {\type{nx=2,ny=2}\break\type{location=middle}}
%   {\showprint[][][backoffset=3pt]}                  {\type{rugoffset=.5cm}}
%   {\showprint[][][topoffset=3pt]}                   {\type{kopoffset=.5cm}}
%   {\showprint[][][scale=1.5]}                       {\type{schaal=1.5}}
%   {\showprint[][][scale=0.8]}                       {\type{schaal=0.8}}
% \stopcombination
%
% \startcombination[3*4]
%   {\showprint[landscape][]         [location=middle]} {\type{landscape}}
%   {\showprint[]         [landscape][location=middle]} {\strut\break\type{landscape}}
%   {\showprint[landscape][landscape][location=middle]} {\type{landscape}\break\type{landscape}}
%   {\showprint[90]       []         [location=middle]} {\type{90}}
%   {\showprint[]         [90]       [location=middle]} {\strut\break\type{90}}
%   {\showprint[90]       [90]       [location=middle]} {\type{90}\break\type{90}}
%   {\showprint[180]      []         [location=middle]} {\type{180}}
%   {\showprint[]         [180]      [location=middle]} {\strut\break\type{180}}
%   {\showprint[180]      [180]      [location=middle]} {\type{180}\break\type{180}}
%   {\showprint[mirrored] []         [location=middle]} {\type{mirrored}}
%   {\showprint[]         [mirrored] [location=middle]} {\strut\break\type{mirrored}}
%   {\showprint[mirrored] [mirrored] [location=middle]} {\type{mirrored}\break\type{mirrored}}
% \stopcombination

\permanent\protected\tolerant\gdef\showframe[#1]#*[#2]%
  {\ifarguments
     \showframe
       [\v!top,\v!header,
        \v!text,
        \v!footer,\v!bottom]
       [\v!leftedge,\v!leftmargin,
        \v!text,
        \v!rightmargin,\v!rightedge]
   \or
     \showframe
       [\v!header,\v!text,\v!footer]
       [#1]
   \or
     \setupbackgrounds
       [\v!page]
       [\c!frame=\v!on,
        \c!corner=\v!rectangular,
        \c!frameoffset=\zeropoint,
        \c!framedepth=\zeropoint,
        \c!framecolor=layout:page]
     \setupbackgrounds
       [#1][#2]
       [\c!background=,
        \c!frame=\v!on,
        \c!corner=\v!rectangular,
        \c!frameoffset=\zeropoint,
        \c!framedepth=\zeropoint,
        \c!framecolor=]
   \fi
   \setupbackgrounds
     [\c!state=\v!repeat]}

\permanent\protected\tolerant\gdef\showsetups[#1]%
  {\ctxcommand{showlayoutvariables("#1")}}

\permanent\protected\tolerant\gdef\showlayout[#1]%
  {\ctxcommand{showlayout("#1")}}

\permanent\protected\gdef\showmargins
  {\starttabulate
     \NC asynchrone   \NC \doifelseoddpage     {odd}   {even} \NC \NR
     \NC synchrone    \NC \doifelserightpage   {right} {left} \NC \NR
     \NC right margin \NC \the\rightmarginwidth               \NC \NR
     \NC left margin  \NC \the\leftmarginwidth                \NC \NR
     \NC outer margin \NC \the\outermarginwidth               \NC \NR
     \NC inner margin \NC \the\innermarginwidth               \NC \NR
   \stoptabulate}

\permanent\protected\gdef\showcentering
  {\setupbackgrounds
     [\v!text]
     [\c!background=centeringlines]}

\defineoverlay
  [centeringlines]
  [\vpack to \d_overlay_height \bgroup
     \vfill
     \hpack to \d_overlay_width \bgroup
        \hss
        \hskip1cm
        \dorecurse{15}{%
            \kern-.25\onepoint
            \ifodd\recurselevel
                \darkred
            \else
                \darkblue
            \fi
            \vrule
                height.5\d_overlay_height
                depth .5\d_overlay_height
                width .5\onepoint
            \hskip1cm
        }%
        \hss
     \egroup
     \vfill
   \egroup]

%D Page grids:

\newconstant\c_page_grids_location
\newconstant\c_page_grids_line_mode
\newconstant\c_page_grids_lineno_mode
\newconstant\c_page_grids_columns_mode

\permanent\protected\tolerant\gdef\showgrid[#1]%
  {\c_page_grids_location    \plusone % downward compatible default
   \c_page_grids_line_mode   \plusone
   \c_page_grids_lineno_mode \plusone
   \c_page_grids_columns_mode\plusone
   \processallactionsinset
     [#1]%
     [   \v!reset=>\c_page_grids_location    \zerocount
                   \c_page_grids_columns_mode\zerocount,
        \v!bottom=>\c_page_grids_location    \plusone,
           \v!top=>\c_page_grids_location    \plustwo,
          \v!none=>\c_page_grids_line_mode   \zerocount,
           \v!all=>\c_page_grids_line_mode   \plusone,
         \v!lines=>\c_page_grids_line_mode   \plustwo,
         \v!frame=>\c_page_grids_line_mode   \plusthree,
      \v!nonumber=>\c_page_grids_lineno_mode \zerocount,
         \v!right=>\c_page_grids_lineno_mode \plusone,
          \v!left=>\c_page_grids_lineno_mode \plustwo,
         \v!outer=>\c_page_grids_lineno_mode \plusthree,
         \v!inner=>\c_page_grids_lineno_mode \plusfour,
       \v!columns=>\c_page_grids_columns_mode\plusone]% new option
   \ifcase\c_page_grids_location
     \let\page_grids_add_to_box\gobbleoneargument
   \else % 1=bottom 2=top
     \let\page_grids_add_to_box\page_grids_add_to_box_indeed
   \fi
   \ifcase\c_page_grids_columns_mode
     \let\page_grids_add_to_one\gobbleoneargument
     \let\page_grids_add_to_mix\gobbleoneargument
   \else
     \let\page_grids_add_to_one\page_grids_add_to_one_indeed
     \let\page_grids_add_to_mix\page_grids_add_to_mix_indeed
   \fi}

% if really needed for speed we can cache the grid

\glet\page_grids_add_to_box\gobbleoneargument
\glet\page_grids_add_to_one\gobbleoneargument
\glet\page_grids_add_to_mix\gobbleoneargument

\gdef\page_grids_add_to_box_indeed#1% to be checked for color and layer ..... use mp
  {\startcolor[layout:grid]%
   \resetvisualizers
   \gridboxlinemode  \c_page_grids_line_mode
   \gridboxlinenomode\c_page_grids_lineno_mode
   \setgridbox\scratchbox\makeupwidth\textheight % todo: check color
   \global\setbox#1\hpack\bgroup % global ?
     \ifcase\c_page_grids_location\or\or\box#1\hskip-\makeupwidth\fi
     \begingroup % color
     \ifcase\layoutcolumns\else
       \setlayoutcomponentattribute{\v!grid:\v!columns}%
       \hpack \layoutcomponentboxattribute to \makeupwidth\bgroup
         \gray
         \dorecurse\layoutcolumns
           {\hkern\layoutcolumnwidth
            \ifnum\recurselevel<\layoutcolumns
              \vrule
                \s!height\ht\scratchbox
                \s!depth \dp\scratchbox
                \s!width \layoutcolumndistance
            \fi}%
       \egroup
       \hkern-\makeupwidth
     \fi
     \ifnum\layoutrows>\plusone
       \vpack to \textheight\bgroup
         \gray
         \forgetall
         \resetvisualizers
        %\resetteststrut
         \offinterlineskip
         \hsize\makeupwidth
         \topskipcorrection
         \getrawnoflines\layoutrowdistance
         \scratchheight\noflines\strutht
         \scratchdepth\noflines\strutdp
         \getrawnoflines\textheight
         \scratchdimen\dimexpr\makeupwidth+\lineheight\relax
         \advanceby\layoutrows\plusone
         \localcontrolledrepeat\noflines
           {\hkern-.5\lineheight\relax
            \ifcase\numexpr\currentloopiterator;\layoutrows\relax
              \vrule
                \s!height\scratchheight
                \s!depth \scratchdepth
            \else
              \novrule
                \s!height\strutht
                \s!depth \strutdp
            \fi
            \s!width\scratchdimen
            \par}%
         \vfill
       \egroup
       \hkern-\makeupwidth
     \fi
     \setlayoutcomponentattribute{\v!grid:\v!lines}%
     \hpack \layoutcomponentboxattribute{\box\scratchbox}%
     \endgroup
     \ifcase\c_page_grids_location\or\hskip-\makeupwidth\box#1\fi
   \egroup
   \stopcolor}

\gdef\page_grids_add_to_one_indeed#1%
  {\begingroup
   \resetvisualizers
   \global\setbox#1\vpack{\noindent\backgroundline[layout:one]{\box#1}}%
   \endgroup}

\gdef\page_grids_add_to_mix_indeed#1%
  {\begingroup
   \resetvisualizers
   \global\setbox#1\vpack{\noindent\backgroundline[layout:mix]{\box#1}}%
   \endgroup}

\protect

\continueifinputfile{s-layout-show.mkxl}

\setupwhitespace[big]

\setuphead[section][page=yes]

\starttext

\startsubject[title=\type{\showprint}]
    \showprint
\stopsubject

\startsubject[title=\type{\showframe}]
    \showframe
\stopsubject

\startsubject[title=\type{\showlayout}]
    \showlayout
\stopsubject

\startsubject[title=\type{\showsetups}]
    \showsetups
\stopsubject

\startsubject[title=\type{\showmargins}]
    \showmargins
\stopsubject

\startsubject[title=\type{\showgrid}]
    \showgrid
\stopsubject

\stoptext
