%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}{}

\unprotect

\unexpanded\gdef\showprint{\dotripleempty\page_layouts_show_print}

\gdef\page_layouts_show_print[#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

\unexpanded\gdef\showframe {\dodoubleempty\page_layouts_show_frame}
\unexpanded\gdef\showsetups{\dosingleempty\page_layouts_show_setups}
\unexpanded\gdef\showlayout{\dosingleempty\page_layouts_show_layout}

\gdef\page_layouts_show_frame[#1][#2]%
  {\ifsecondargument
     \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=]
   \else\iffirstargument
     \showframe
       [\v!header,\v!text,\v!footer]
       [#1]
   \else
     \showframe
       [\v!top,\v!header,
        \v!text,
        \v!footer,\v!bottom]
       [\v!leftedge,\v!leftmargin,
        \v!text,
        \v!rightmargin,\v!rightedge]
   \fi\fi
   \setupbackgrounds
     [\c!state=\v!repeat]}

\gdef\page_layouts_show_setups[#1]%
  {\ctxcommand{showlayoutvariables("#1")}}

\gdef\page_layouts_show_layout[#1]%
  {\ctxcommand{showlayout("#1")}}

\unexpanded\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}

%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

\unexpanded\gdef\showgrid
  {\dosingleempty\page_grids_show}

\gdef\page_grids_show[#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 % global ?
     {\ifcase\c_page_grids_location\or\or\box#1\hskip-\makeupwidth\fi
      \begingroup % color
      \ifcase\layoutcolumns\else
        \gray
        \setlayoutcomponentattribute{\v!grid:\v!columns}%
        \hpack \layoutcomponentboxattribute to \makeupwidth
          {\dorecurse\layoutcolumns
             {\hskip\layoutcolumnwidth
              \ifnum\recurselevel<\layoutcolumns
                \vrule
                  \s!height\ht\scratchbox
                  \s!depth \dp\scratchbox
                  \s!width \layoutcolumndistance
              \fi}}%
        \hskip-\makeupwidth
      \fi
      \setlayoutcomponentattribute{\v!grid:\v!lines}%
      \hpack \layoutcomponentboxattribute{\box\scratchbox}%
      \endgroup
      \ifcase\c_page_grids_location\or\hskip-\makeupwidth\box#1\fi}%
   \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}

%D The next command shows some statistics (we might add more in due time):

\unexpanded\gdef\showusage
  {\gtoksapp\everyaftershipout{\ctxcommand{showusage()}}%
   \glet\showusage\relax}

\protect \endinput
