%D \module
%D   [       file=page-com, % moved from page-imp
%D        version=1998.01.15,
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Page Comments,
%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.

%D This will become runtime loaded code.

\writestatus{loading}{ConTeXt Page Macros / Page Comments}

\unprotect

%D \macros
%D   {setuppagecomment,startpagecomment}
%D
%D This command is not yet documented. Usage:
%D
%D \starttyping
%D \setuppagecomment[state=start,location=right]
%D
%D \startpagecomment
%D \input knuth
%D \stoppagecomment
%D \stoptyping

\installcorenamespace{pagecomment}
\installcorenamespace{pagecommentlocations}
\installcorenamespace{pagecommentstates}

\newdimension\d_page_comments_offset
\newdimension\d_page_comments_page_width
\newdimension\d_page_comments_page_height
\newdimension\d_page_comments_width
\newdimension\d_page_comments_height
\newdimension\d_page_comments_distance

\newconditional\c_page_comment_enabled

\let\p_page_commands_location\v!none
\let\p_page_commands_state   \v!stop

\installsetuponlycommandhandler \??pagecomment {pagecomment}

\appendtoks
    \edef\p_page_commands_location{\directpagecommentparameter\c!location}%
    \edef\p_page_commands_state   {\directpagecommentparameter\c!state}%
    \expandnamespacemacro\??pagecommentstates\p_page_commands_state\v!none
\to \everysetuppagecomment

% \ifdefined\scrn_canvas_synchronize_simple

    \appendtoks
        \ifx\p_page_commands_state\v!start
            \scrn_canvas_synchronize_simple
        \else
            \scrn_canvas_synchronize_complex
        \fi
    \to \everyshipout

% \fi

\defcsname\??pagecommentstates\v!start\endcsname
  {\d_page_comments_offset  {\directpagecommentparameter\c!offset  }%
   \d_page_comments_distance{\directpagecommentparameter\c!distance}%
   \d_page_comments_width   {\directpagecommentparameter\c!width   }%
   \d_page_comments_height  {\directpagecommentparameter\c!height  }%
   \expandnamespacemacro\??pagecommentlocations\p_page_commands_location\v!none
   \definepapersize[\v!pagecomment][\c!height=\d_page_comments_page_height,\c!width=\d_page_comments_page_width]%
   \setuppapersize[\papersize][\v!pagecomment]%
   \setupbackgrounds[\v!paper][\c!background=\v!pagecomment]} % maybe append if already set

\defcsname\??pagecommentstates\v!stop\endcsname
  {\setupbackgrounds[\v!paper][\c!background=]}

\defcsname\??pagecommentstates\v!none\endcsname
  {}

\defcsname\??pagecommentlocations\v!bottom\endcsname
  {\setuplayout[\c!location=]%
   \setuppapersize[\c!top=\vskip\d_page_comments_offset,\c!bottom=\vss,\c!left=\hkern\d_page_comments_offset,\c!right=]%
   \d_page_comments_page_height{\paperheight+2\d_page_comments_offset+\d_page_comments_distance+\d_page_comments_height}%
   \d_page_comments_page_width {\paperwidth +2\d_page_comments_offset}%
   \defineoverlay[\v!pagecomment][\page_comments_top_bottom]}

\defcsname\??pagecommentlocations\v!top\endcsname
  {\setuplayout[\c!location=]%
   \setuppapersize[\c!top=\vss,\c!bottom=\vskip\d_page_comments_offset,\c!left=\hkern\d_page_comments_offset,\c!right=]%
   \d_page_comments_page_height{\paperheight+2\d_page_comments_offset+\d_page_comments_distance+\d_page_comments_height}%
   \d_page_comments_page_width {\paperwidth +2\d_page_comments_offset}%
   \defineoverlay[\v!pagecomment][\page_comments_top_bottom]}

\defcsname\??pagecommentlocations\v!left\endcsname
  {\setuplayout[\c!location=]%
   \setuppapersize[\c!top=\vskip\d_page_comments_offset,\c!bottom=,\c!left=\hss,\c!right=\hkern\d_page_comments_offset]%
   \d_page_comments_page_height{\paperheight+2\d_page_comments_offset}%
   \d_page_comments_page_width {\paperwidth +2\d_page_comments_offset+\d_page_comments_distance+\d_page_comments_width}%
   \defineoverlay[\v!pagecomment][\page_comments_left_right]}

\defcsname\??pagecommentlocations\v!right\endcsname
  {\setuplayout[\c!location=]%
   \setuppapersize[\c!top=\vskip\d_page_comments_offset,\c!bottom=,\c!left=\hkern\d_page_comments_offset,\c!right =\hss]%
   \d_page_comments_page_height{\paperheight+2\d_page_comments_offset}%
   \d_page_comments_page_width {\paperwidth +2\d_page_comments_offset+\d_page_comments_distance+\d_page_comments_width}%
   \defineoverlay[\v!pagecomment][\page_comments_left_right]}

\defcsname\??pagecommentlocations\v!none\endcsname
  {}% \setuppapersize[\c!bottom=,\c!top=,\c!left=,\c!right=]}

\protected\def\page_comments_top_bottom
  {\vpack to \printpaperheight
     {%\forgetall
      \hsize\printpaperwidth\relax
      \ifx\p_page_commands_location\v!bottom
        \vskip{\paperheight+\d_page_comments_distance+\d_page_comments_offset}%
      \else
        \vskip\d_page_comments_offset
        \vss
      \fi
      \hkern\d_page_comments_offset
      \vbox to \d_page_comments_height
        {%\forgetall
         \hsize\paperwidth
         \ifconditional\c_page_comment_enabled
           \getbuffer[\v!pagecomment]%
           \global\c_page_comment_enabled\conditionalfalse
         \fi}%
      \hfill
      \ifx\p_page_commands_location\v!bottom
        \vss
        \vskip\d_page_comments_offset
      \else
        \vskip{\paperheight+\d_page_comments_distance+\d_page_comments_offset}%
      \fi}}

\protected\def\page_comments_left_right
  {\hpack to \printpaperwidth
     {\ifx\p_page_commands_location\v!right
        \hkern{\paperwidth+\d_page_comments_distance+\d_page_comments_offset}%
      \else
        \hkern\d_page_comments_offset
        \hss
      \fi
      \vbox to \printpaperheight
        {%\forgetall
         \vskip\d_page_comments_offset
         \hsize\d_page_comments_width
         \ifconditional\c_page_comment_enabled
           \getbuffer[\v!pagecomment]%
           \global\c_page_comment_enabled\conditionalfalse
         \fi
         \vss}%
      \ifx\p_page_commands_location\v!right
        \hss
        \hkern\d_page_comments_offset
      \else
        \hkern{\paperwidth+\d_page_comments_distance+\d_page_comments_offset}%
      \fi}}

\permanent\protected\defcsname\e!start\v!pagecomment\endcsname
  {\global\c_page_comment_enabled\conditionaltrue
   \grabbufferdatadirect\v!pagecomment{\e!start\v!pagecomment}{\e!stop\v!pagecomment}}

\aliased\letcsname\e!stop\v!pagecomment\endcsname\relax

\setuppagecomment
  [\c!state=, % \v!stop would invoke background calculation
   \c!location=\v!bottom,
   \c!offset=.5cm,
   \c!distance=.5cm,
   \c!height=5cm,
   \c!width=10cm]

\protect \endinput
