%D \module
%D   [       file=plus-rul,
%D        version=2003.03.16, 
%D          title=\CONTEXT\ Plus Macros,
%D       subtitle=Ruled Stuff Handling,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=PRAGMA-ADE]
%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 Plus Macros / Ruled Content Handling}

\unprotect 

%D \definesidebar[whow][rulecolor=green,distance=]
%D 
%D \starttext 
%D 
%D \input tufte \par
%D \startsidebar 
%D   \input tufte \par
%D   \input tufte \par
%D   \startsidebar[whow]
%D     \input tufte \par
%D     \input tufte \par
%D     \input tufte \stopsidebar \par
%D   \input tufte \par
%D   \input tufte \stopsidebar \par
%D \input tufte \par
%D \input tufte \par
%D \startsidebar 
%D   \input tufte \par
%D   \input tufte \par
%D   \input tufte \par
%D   \input tufte \par
%D   \input tufte \stopsidebar \par
%D \input tufte \par
%D \input tufte \par
%D \startsidebar 
%D   \input tufte 
%D   \input tufte 
%D   \input tufte 
%D   \input tufte 
%D   \input tufte 
%D \stopsidebar

\newcounter\currentsidebar
\newdimen  \sidebardistance

\definesystemvariable{sr}

\def\setupsidebars 
  {\dodoubleargument\dosetupsidebars}

\def\dosetupsidebars[#1][#2]%
  {\ifsecondargument
     \getparameters[\??sr#1][#2]%
   \else
     \getparameters[\??sr][#1]%
   \fi}

% \setupMPvariables
%   [mpos:bar]
%   [linecolor=red,
%    linewidth=2pt,
%    distance=5pt]

\setupsidebars
  [\c!lijndikte=2pt,
   \c!lijnkleur=red,
   \c!afstand=.5\bodyfontsize]

\def\definesidebar
  {\dodoubleempty\dodefinesidebar}

\def\dodefinesidebar[#1][#2]%
  {\copyparameters
     [\??sr#1][\??sr]
     [\c!lijndikte,\c!lijnkleur,\c!afstand]%
   \getparameters
     [\??sr#1][#2]}

\def\startsidebar
  {\dosingleempty\dostartsidebar}

\def\dostartsidebar[#1]%
  {\bgroup
   \dontleavehmode
   \checktextbackgrounds
   \doglobal\increment\currentsidebar
   \doifelsenothing{#1}
     {\advance\sidebardistance\@@srafstand}
     {\doifelsevaluenothing{\??sr#1\c!afstand}
        {\advance\sidebardistance\@@srafstand}
        {\sidebardistance\getvalue{\??sr#1\c!afstand}}}%
   \startpositionoverlay{text-1}%
     \expanded{\setMPpositiongraphicrange 
       {b:side:\currentsidebar}%
       {e:side:\currentsidebar}%
       {mpos:bar}%
       {self=side:\currentsidebar,
        linewidth=\getvalue{\??sr#1\c!lijndikte},
        linecolor=\getvalue{\??sr#1\c!lijnkleur},
        distance=\the\sidebardistance}}%
   \stoppositionoverlay
   \edef\stopsidebar{\noexpand\dostopsidebar{\currentsidebar}}%
   \bpos{side:\currentsidebar}\ignorespaces}

\def\dostopsidebar#1%
  {\removelastspace\tpos{side:#1}\carryoverpar\egroup}

\startMPpositionmethod{mpos:bar}
  \startMPpositiongraphic{mpos:bar}{linecolor,linewidth,distance}%
    StartPage ;
      path p ; p := 
      if \MPp\MPbself=\MPp\MPeself : 
        (xpart ulcorner Field[Text][Text],\MPy\MPbself+\MPh\MPbself) --
        (xpart llcorner Field[Text][Text],\MPy\MPeself-\MPd\MPeself) ;
      elseif RealPageNumber=\MPp\MPbself : 
        (xpart ulcorner Field[Text][Text],\MPy\MPbself+\MPh\MPbself) --
        (llcorner Field[Text][Text]) ;
      elseif RealPageNumber=\MPp\MPeself : 
        (ulcorner Field[Text][Text]) --
        (xpart llcorner Field[Text][Text],\MPy\MPeself-\MPd\MPeself) ;
      else : 
        (ulcorner Field[Text][Text]) --
        (llcorner Field[Text][Text]) ;
      fi ; 
      p := p shifted (-llcorner Field[Text][Text]-(\MPvar{distance},0)) ; 
      interim linecap := butt ; 
      draw p 
        withpen pencircle scaled \MPvar{linewidth}  
        withcolor \MPvar{linecolor} ; 
    StopPage ;
  \stopMPpositiongraphic
  \MPpositiongraphic{mpos:bar}{}%
\stopMPpositionmethod

%D We now reimplement the margin rules handler defined in 
%D \type {core-rul}:
%D
%D \setupmarginrules[level=5]
%D
%D \startmarginrule[1]
%D First we set the level at~5. Next we typeset this first
%D paragraph as a level~1 one. As expected no rule show up.
%D \stopmarginrule
%D
%D \startmarginrule[5]
%D The second paragraph is a level~5 one. As we can see here,
%D the marginal rule gets a width according to its level.
%D \stopmarginrule
%D
%D \startmarginrule[8]
%D It will of course be no surprise that this third paragraph
%D has a even thicker margin rule. This behavior can be
%D overruled by specifying the width explictly.
%D \stopmarginrule

\definesidebar
  [\v!marge]
  [\c!lijnkleur=black,
   \c!lijndikte=\@@kalijndikte,
   \c!afstand=\dimexpr(\linkermargeafstand-\@@kalijndikte/2)]

\def\complexstartmarginrule[#1]%
  {\bgroup
   \ifnum#1<\@@kaniveau\relax
     \let\stopmarginrule\egroup
   \else
     \def\@@kadefaultwidth{#1}%
     \let\stopmarginrule\dostopmarginrule 
     \@EA\startsidebar\@EA[\@EA\v!marge\@EA]%
   \fi}

\def\dostopmarginrule
  {\stopsidebar
   \egroup}

\protect \endinput 
