%D \module
%D   [       file=page-grd,   % moved from page-ini
%D        version=2011.12.07, % 2000.10.20,
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Grids,
%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 / Grids}

\unprotect

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

\unexpanded\def\showgrid
  {\dosingleempty\page_grids_show}

\def\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!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

\let\page_grids_add_to_box\gobbleoneargument
\let\page_grids_add_to_one\gobbleoneargument
\let\page_grids_add_to_mix\gobbleoneargument

\def\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\hbox % global ?
     {\ifcase\c_page_grids_location\or\or\box#1\hskip-\makeupwidth\fi
      \begingroup % color
      \ifcase\layoutcolumns\else
        \gray
        \setlayoutcomponentattribute{\v!grid:\v!columns}%
        \hbox \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}%
      \hbox \layoutcomponentboxattribute{\box\scratchbox}%
      \endgroup
      \ifcase\c_page_grids_location\or\hskip-\makeupwidth\box#1\fi}%
   \stopcolor}

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

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

\protect \endinput
