%D \module
%D   [       file=pack-bar,
%D        version=2009.06.26,
%D          title=\CONTEXT\ Packaging Macros,
%D       subtitle=Bars,
%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 Packaging Macros / Bars}

\unprotect

%D This code has been moved from scrn-int to here (was some old experimental code).
%D It could be in scrn-bar but it's static. In the meantime the interface has been
%D adapted to a key|/|value one.
%D
%D \startbuffer
%D  \dorecurse{10}{
%D      \ruledhbox{\horizontalpositionbar[n=#1,min=1,max=10,text=!,color=red]}
%D      \par
%D  }
%D \stopbuffer
%D
%D \typebuffer \stoplinecorrection \getbuffer \stoplinecorrection
%D
%D \startbuffer
%D  \dorecurse{10}{
%D      \ruledhbox{\horizontalgrowingbar[n=#1,min=1,max=10,text=!,color=red]}
%D      \par
%D  }
%D \stopbuffer
%D
%D \typebuffer \stoplinecorrection \getbuffer \stoplinecorrection

%D Maybe [n|min|max] : \numexpr...\relax

\installcorenamespace{positionbar}

\installsimplecommandhandler \??positionbar {positionbar} \??positionbar

\setuppositionbar
  [\c!min=\plusone,
   \c!max=\plusone,
   \c!n=\plusone
   \c!text=?,
   \c!width=\emwidth,
   \c!height=\strutheight,
   \c!depth=\strutdepth]

\def\pack_bar_common_setting
  {\usepositionbarstyleandcolor\c!style\c!color
   \scratchcounter{\positionbarparameter\c!n}%
   \scratchmin{\positionbarparameter\c!min}%
   \scratchmax{\positionbarparameter\c!max}}

\permanent\protected\def\horizontalpositionbar[#S#1]%
  {\hbox to \hsize
     {\setuppositionbar[#1]%
      \pack_bar_common_setting
      \hskip\zeropoint\s!plus \scratchcounter\s!fill
      \hskip\zeropoint\s!plus-\scratchmin    \s!fill
      \positionbarparameter\c!text\relax
      \hskip\zeropoint\s!plus \scratchmax    \s!fill
      \hskip\zeropoint\s!plus-\scratchcounter\s!fill}}

\permanent\protected\def\verticalpositionbar[#S#1]%
  {\vbox to \vsize
     {\hsize{\positionbarparameter\c!width}%
      \setuppositionbar[#1]%
      \pack_bar_common_setting
      \vskip\zeropoint\s!plus \scratchcounter\s!fill
      \vskip\zeropoint\s!plus-\scratchmin    \s!fill
      \positionbarparameter\c!text\relax
      \vskip\zeropoint\s!plus \scratchmax    \s!fill
      \vskip\zeropoint\s!plus-\scratchcounter\s!fill}}

\permanent\protected\def\horizontalgrowingbar[#S#1]%
  {\hpack to \hsize
     {\setuppositionbar[#1]%
      \pack_bar_common_setting
      \vrule
        \s!width  \zeropoint
        \s!height {\positionbarparameter\c!height}%
        \s!depth  {\positionbarparameter\c!depth}%
      \relax
      \leaders
        \vrule
        \hskip
          \zeropoint
          \s!plus\numexpr\scratchcounter-\scratchmin+\plusone\relax\s!fill
      \relax
      \hskip
        \zeropoint
        \s!plus \scratchmax\s!fill
      \relax
      \hskip
        \zeropoint
        \s!plus-\scratchcounter\s!fill
      \relax}}

\permanent\protected\def\verticalgrowingbar[#S#1]%
  {\vpack to \vsize
     {\hsize{\positionbarparameter\c!width}%
      \setuppositionbar[#1]%
      \pack_bar_common_setting
      \hrule
        \s!width  {\positionbarparameter\c!width}% can have changed
        \s!height \zeropoint
        \s!depth  \zeropoint
      \relax
      \leaders
        \hrule
        \vskip
          \zeropoint
          \s!plus \numexpr\scratchcounter-\scratchmin+\plusone\relax\s!fill
      \relax
      \vskip
        \zeropoint
        \s!plus \scratchmax\s!fill
      \relax
      \vskip
        \zeropoint
        \s!plus-\scratchcounter\s!fill
      \relax}}

\protect \endinput
