%D \module
%D   [       file=scrn-bar, % was part of scrn-int
%D        version=1995.01.01,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Progress 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 Screen Macros / Progress Bars}

\unprotect

%D The code is a bit upgraded to \MKIV\ but the output is mostly the
%D same. In retrospect this should have been a module. We can move
%D some definitions to scrn-run-bar.mkiv if needed. We can also make
%D the code a bit more efficient.

% todo: replace blackrule by stupid rules

%D \starttyping
%D \setupinteraction
%D   [state=start]
%D
%D \setupsubpagenumber
%D   [state=start]
%D
%D \setuplayout
%D   [middle]
%D
%D \setuppapersize
%D   [S4][S4]
%D
%D \startsetups bars
%D     \ruledvbox to \textheight \bgroup
%D         a \ruledhbox{\interactionbar[a]}\vss
%D         b \ruledhbox{\interactionbar[b]}\vss
%D         c \ruledhbox{\interactionbar[c]}\vss
%D         d \ruledhbox{\interactionbar[d]}\vss
%D         e \ruledhbox{\interactionbar[e]}\vss
%D         f \ruledhbox{\interactionbar[f]}\vss
%D         g \ruledhbox{\interactionbar[g]}\vss
%D     \egroup
%D \stopsetups
%D
%D \setuptexttexts[\setups{bars}]
%D
%D \starttext
%D     \dorecurse {12} {
%D         \startstandardmakeup
%D         \stopstandardmakeup
%D     }
%D \stoptext
%D \stoptyping

\installcorenamespace{interactionbar}

\installframedcommandhandler \??interactionbar {interactionbar} \??interactionbar

\unexpanded\def\interactionbar
  {\dodoubleempty\scrn_bar_direct}

\def\scrn_bar_direct[#tag][#settings]% somewhat messy
  {\iflocation
     \begingroup
     \doifassignmentelse{#tag}
       {\let\currentinteractionbar\empty
        \setupcurrentinteractionbar[#tag]%
        \edef\currentinteractionbar{\interactionbarparameter\c!alternative}}%
       {\edef\currentinteractionbar{#tag}%
        \ifsecondargument\setupcurrentinteractionbar[#settings]\fi}%
     \doif{\interactionbarparameter\c!state}\v!start
       {\interactionbarparameter\c!command}%
     \endgroup
   \fi}

\newdimen\d_scrn_bar_width
\newdimen\d_scrn_bar_height
\newdimen\d_scrn_bar_depth
\newdimen\d_scrn_bar_distance

%D Interaction buttons, in fact a row of tiny buttons, are
%D typically only used for navigational purposed. The next
%D macro builds such a row based on a specification list.
%D
%D \startbuffer
%D \interactionbuttons[width=\hsize][page,PreviousJump,ExitViewer]
%D \stopbuffer
%D
%D \typebuffer
%D
%D gives
%D
%D \getbuffer
%D
%D Apart from individual entries, one can use \type{page} and
%D \type {subpage} as shortcuts to their four associated buttons.
%D The symbols are derived from the symbols linked to the
%D entries.

\unexpanded\def\interactionbuttons
  {\dodoubleempty\scrn_bar_buttons}

\def\scrn_bar_buttons
  {\iflocation
     \expandafter\scrn_bar_buttons_status
   \else
     \expandafter\scrn_bar_buttons_ignore
   \fi}

\def\scrn_bar_buttons_status[#settings][#list]%
   {\doif{\interactionbarparameter\c!state}\v!start
      {\ifsecondargument
         \scrn_bar_buttons_indeed[#settings][#list]%
       \else
         \scrn_bar_buttons_indeed[][#settings]%
       \fi}}

\def\scrn_bar_buttons_ignore[#settings][#list]% \gobbletwooptionals
  {}

\def\scrn_bar_buttons_indeed[#settings][#list]%
  {\begingroup
  %\let\menuparameter\interactionbarparameter
   \setupcurrentinteractionbar[#settings]%
   \d_scrn_bar_width\interactionbarparameter\c!width
   \ifdim\d_scrn_bar_width=\zeropoint
     \d_scrn_bar_width1.5\emwidth
   \fi
   \doifnothing{\interactionbarparameter\c!height}{\letinteractionbarparameter\c!height\v!broad}%
   \doifnothing{\interactionbarparameter\c!depth }{\letinteractionbarparameter\c!depth\!!zeropoint}%%%
   \letinteractionbarparameter\c!background\empty
   \setbox2\hbox{\inheritedinteractionbarframed{\symbol[\interactionparameter\c!symbolset][\v!previouspage]}}%
   \!!heighta\ht2 % needed because we default to nothing
   \letinteractionbarparameter\c!strut\v!no
   \letinteractionparameter\c!width\zeropoint
   \!!counta\zerocount % new, was 1
   \processallactionsinset
     [#list]
     [   \v!page=>\advance\!!counta\plusfour,
      \v!subpage=>\advance\!!counta\plusfour,
      \s!unknown=>\advance\!!counta\plusone]%
   \ifdim\d_scrn_bar_width=\zeropoint
     \!!widtha\dimexpr2\emwidth+\d_scrn_bar_distance\relax
     \!!widthb\dimexpr\!!counta\!!widtha-\d_scrn_bar_distance\relax
   \else
     \!!widtha\d_scrn_bar_width
     \!!widthb\dimexpr\!!counta\d_scrn_bar_distance-\d_scrn_bar_distance\relax
     \advance\!!widtha -\!!widthb
     \divide\!!widtha \!!counta
     \!!widthb\d_scrn_bar_width
   \fi
   \hbox to \!!widthb
     {\setnostrut
      \startsymbolset[\interactionparameter\c!symbolset]%
      \processallactionsinset
        [#list]
        [   \v!page=>\scrn_bar_goto\v!firstpage
                     \scrn_bar_goto\v!nextpage
                     \scrn_bar_goto\v!previouspage
                     \scrn_bar_goto\v!lastpage,
         \v!subpage=>\scrn_bar_goto\v!firstsubpage
                     \scrn_bar_goto\v!nextsubpage
                     \scrn_bar_goto\v!previoussubpage
                     \scrn_bar_goto\v!lastsubpage,
         \s!unknown=>\scrn_bar_goto\commalistelement]%
      \unskip
      \stopsymbolset}%
   \endgroup}

\def\scrn_bar_goto#action%
  {\button
     [\c!height=\the\!!heighta,\c!width=\the\!!widtha]%
     {\symbol[#action]}% we could expand this one once only
     [#action]%
   \hss}

% todo: this will be \letblackruleparameter\c!width\!!widtha (faster)

\def\scrn_bar_alternative_a
  {\d_scrn_bar_width   \interactionbarparameter\c!width
   \d_scrn_bar_distance\interactionbarparameter\c!distance
   \d_scrn_bar_height  \interactionbarparameter\c!height
   \d_scrn_bar_depth   \interactionbarparameter\c!depth
   \noindent\hbox to \d_scrn_bar_width \bgroup
     \dontcomplain
     \setupblackrules[\c!height=\v!max,\c!depth=\v!max]%
     \!!widthb\dimexpr\d_scrn_bar_width-4\emwidth\relax
     \processaction
       [\interactionbarparameter\c!step]
       [   \v!small=>\scratchcounter 20,
          \v!medium=>\scratchcounter 10,
             \v!big=>\scratchcounter  5,
         \s!unknown=>\scratchcounter 10]%
     \!!widtha\dimexpr\!!widthb/\scratchcounter\relax
     \setupblackrules[\c!width=\!!widtha]%
     \setbox\scratchbox\hbox to \d_scrn_bar_width
       {\hskip2\emwidth
        \setbox\scratchbox\hbox{\blackrule[\c!color=\interactionbarparameter\c!backgroundcolor]}%
        \dorecurse\scratchcounter
          {\hss\normalexpanded{\directgotodumbbox{\copy\scratchbox}[page(\the\numexpr\recurselevel*\lastpage/\scratchcounter\relax)]}}%
        \hss
        \hskip2\emwidth}%
     \wd\scratchbox\zeropoint
     \box \scratchbox
     \setupblackrules[\c!width=\emwidth]%
     \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\blackrule}[\v!firstpage]}%
     \hskip\emwidth
     \ifnum\realpageno>\plusone
       \hskip\zeropoint\!!plus\numexpr\realpageno-\plustwo\relax \s!sp\relax % cm gives overflow
       \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\blackrule}[\v!previouspage)]}%
     \fi
     \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\blackrule}[page(\number\realpageno)]}% todo: \v!currentpage
     \ifnum\realpageno<\lastpage\relax
       \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\blackrule}[\v!nextpage]}%
       \hskip\zeropoint\!!plus\numexpr\lastpage-\realpageno-\plusone\relax \s!sp\relax % cm gives overflow
     \fi
     \hskip\emwidth
     \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\blackrule}[\v!lastpage]}%
   \egroup}

\def\scrn_bar_alternative_b
  {\ifnum\lastpage>\firstpage\relax
     \interactionbuttons[\v!firstpage,\v!previouspage,\v!nextpage,\v!lastpage]%
   \fi}

\def\scrn_bar_alternative_c
  {\ifnum\lastpage>\plusone
     \d_scrn_bar_width\interactionbarparameter\c!width
     \hbox to \d_scrn_bar_width
       {\setupblackrules[\c!height=\interactionbarparameter\c!height,\c!depth=\interactionbarparameter\c!depth,\c!width=\emwidth]%
        \scratchdimen\dimexpr(\d_scrn_bar_width-4\emwidth)/\numexpr\lastpage+\minusone\relax\relax
        \!!widtha\numexpr\realpageno+\minusone\relax\scratchdimen
        \!!widthb\numexpr\lastpage-\realpageno\relax\scratchdimen
        \directgotospecbox\interactionbarparameter{\blackrule}[\v!firstpage]%
        \hss
        \directgotospecbox\interactionbarparameter{\blackrule[\c!width=\!!widtha]}[\v!previouspage]%
        \blackrule[\c!color=\interactionbarparameter\c!contrastcolor]%
        \directgotospecbox\interactionbarparameter{\blackrule[\c!width=\!!widthb]}[\v!nextpage]%
        \hss
        \directgotospecbox\interactionbarparameter{\blackrule}[\v!lastpage]}%
   \fi}

\unexpanded\def\scrn_bar_goto_a#whereto%
  {\symbol[\ifcase#whereto\v!previous\or\v!somewhere\or\v!next\fi]}

\unexpanded\def\scrn_bar_goto_b#whereto%
  {\vrule\!!height\d_scrn_bar_height\!!depth\d_scrn_bar_depth\!!width\!!widtha\relax}

\unexpanded\def\scrn_bar_goto_c#whereto%
  {\symbol[\ifcase#whereto\v!previous\or\v!somewhere\or\v!somewhere\or\v!somewhere\or\v!next\fi}

\unexpanded\def\scrn_bar_goto_d#whereto%
  {\vrule \!!width\!!widtha \ifcase#whereto%
     \!!height  \d_scrn_bar_height \!!depth  \d_scrn_bar_depth \or
     \!!height.5\d_scrn_bar_height \!!depth.5\d_scrn_bar_depth \or
     \!!height  \d_scrn_bar_height \!!depth  \d_scrn_bar_depth \or
     \!!height.5\d_scrn_bar_height \!!depth.5\d_scrn_bar_depth \else
     \!!height  \d_scrn_bar_height \!!depth  \d_scrn_bar_depth \fi}

\newconstant\c_scrn_bar_mode

\unexpanded\def\scrn_bar_goto_x#command%
  {\doifelse{\interactionbarparameter\c!symbol}\v!yes
     {\setupsymbolset[\interactionparameter\c!symbolset]%
      \let\scrn_bar_goto_indeed\scrn_bar_goto_a}
     {\let\scrn_bar_goto_indeed\scrn_bar_goto_b}%
   \dorecurse\nofsubpages
     {\scratchcounter\numexpr\recurselevel+\firstsubpage+\minusone\relax
      \c_scrn_bar_mode
        \ifnum\scratchcounter<\realpageno \zerocount \else
        \ifnum\scratchcounter=\realpageno \plusone   \else
                                          \plustwo   \fi\fi
      \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\scrn_bar_goto_indeed\c_scrn_bar_mode}[page(\the\scratchcounter)]}%
      #command}%
   \unskip}

\def\scrn_bar_alternative_d
  {\ifnum\nofsubpages>\plusone \doif{\namedcounterparameter\s!subpage\c!state}\v!start{%
     \d_scrn_bar_width   \interactionbarparameter\c!width
     \d_scrn_bar_distance\interactionbarparameter\c!distance
     \d_scrn_bar_height  \interactionbarparameter\c!height
     \d_scrn_bar_depth   \interactionbarparameter\c!depth
     \!!widtha\d_scrn_bar_width
     \noindent\hbox{\scrn_bar_goto_x{\hskip\d_scrn_bar_distance}}%
   }\fi}

\def\scrn_bar_alternative_e
  {\ifnum\nofsubpages>\plusone \doif{\namedcounterparameter\s!subpage\c!state}\v!start{%
     \d_scrn_bar_width   \interactionbarparameter\c!width
     \d_scrn_bar_distance\interactionbarparameter\c!distance
     \d_scrn_bar_height  \interactionbarparameter\c!height
     \d_scrn_bar_depth   \interactionbarparameter\c!depth
     \!!widthb\dimexpr\nofsubpages\d_scrn_bar_distance-\d_scrn_bar_distance\relax % (n-1)
     \!!widtha\dimexpr(\d_scrn_bar_width-\!!widthb)/\nofsubpages\relax
     \ifdim\!!widtha<\d_scrn_bar_distance
       \scrn_bar_alternative_f
     \else
       \noindent\hbox to \d_scrn_bar_width{\scrn_bar_goto_x{\hss}\unskip}%
     \fi
   }\fi}

\def\scrn_bar_alternative_f
  {\ifnum\nofsubpages>\plusone \doif{\namedcounterparameter\s!subpage\c!state}\v!start{%
     \d_scrn_bar_width   \interactionbarparameter\c!width
     \d_scrn_bar_distance\interactionbarparameter\c!distance
     \d_scrn_bar_height  \interactionbarparameter\c!height
     \d_scrn_bar_depth   \interactionbarparameter\c!depth
     \noindent \hbox to \d_scrn_bar_width \bgroup
       \doloop
         {\!!countc\numexpr(\nofsubpages/\recurselevel)+\plusone\relax % rounding
          \!!widthb\d_scrn_bar_distance
          \multiply\!!widthb \!!countc
          \advance\!!widthb -\d_scrn_bar_distance
          \!!widtha\d_scrn_bar_width
          \advance\!!widtha -\!!widthb
          \divide\!!widtha \!!countc
          \ifdim\!!widtha<\d_scrn_bar_distance\else
            \!!countb\recurselevel
            \exitloop
          \fi}%
       \ifnum\!!countc>\plusone
         % this is not that well tested
         \advance\!!countc \minustwo
         \!!widtha-\d_scrn_bar_distance
         \!!widtha\!!countc\!!widtha
         \advance\!!widtha \d_scrn_bar_width
         \advance\!!countc \plusone
         \divide\!!widtha \!!countc
       \fi
       \doifelse{\interactionbarparameter\c!symbol}\v!yes
         {\setupsymbolset[\interactionparameter\c!symbolset]%
          \let\scrn_bar_goto_indeed\scrn_bar_goto_c}%
         {\let\scrn_bar_goto_indeed\scrn_bar_goto_d}%
       \!!countc\numexpr\realpageno-\plustwo\relax
       \!!countd\numexpr\realpageno+\plustwo\relax
       \ifnum\!!countc<\plusone \!!countc\plusone \fi
       \!!countf\zerocount
       \dostepwiserecurse\firstsubpage\lastsubpage\plusone
         {\!!doneafalse
          \advance\!!countf \plusone
          \ifnum\recurselevel=\firstsubpage\relax \!!doneatrue \fi
          \ifnum\recurselevel=\lastsubpage \relax \!!doneatrue \fi
          \c_scrn_bar_mode
            \if!!donea
              \ifnum\recurselevel<\realpageno
                \zerocount
              \else\ifnum\recurselevel>\realpageno
                \plustwo
              \else
                \plusfour
              \fi\fi
            \else
              \ifnum\!!countf=\!!countb
                \ifnum\recurselevel<\realpageno
                  \plusone
                \else\ifnum\recurselevel>\realpageno
                  \plusthree
                \else
                  \plustwo
                \fi\fi
              \else
                \plusthree
              \fi
            \fi
          \normalexpanded{\directgotospecbox\noexpand\interactionbarparameter{\scrn_bar_goto_indeed\c_scrn_bar_mode}[page(\recurselevel)]}%
          \hss
          \!!countf\zerocount}%
       \unskip
     \egroup
   }\fi}

\def\scrn_bar_alternative_g
  {\ifnum\lastsubpage>\firstsubpage\relax % no test for state?
     \interactionbuttons[\v!firstsubpage,\v!previoussubpage,\v!nextsubpage,\v!lastsubpage]%
   \fi}

\setupinteractionbar
  [\c!state=\v!start,
   \c!alternative=a,
   \c!symbol=\v!no,
   \c!width=10\emwidth,
   \c!height=.5\emwidth,
   \c!depth=\zeropoint,
   \c!distance=.5\emwidth,
   \c!step=\v!medium,
   \c!foregroundcolor=\interactionbarparameter\c!color,
   \c!foregroundstyle=\interactionbarparameter\c!style,
   \c!color=\interactionparameter\c!color,
   \c!contrastcolor=\interactionparameter\c!contrastcolor,
   \c!style=,
   \c!frame=\v!on,
   \c!background=color,
   \c!backgroundcolor=gray,
   \c!samepage=\v!yes]

\defineinteractionbar[a][\c!command=\scrn_bar_alternative_a]
\defineinteractionbar[b][\c!command=\scrn_bar_alternative_b,\c!height=\v!broad]
\defineinteractionbar[c][\c!command=\scrn_bar_alternative_c,\c!height=\v!max,\c!depth=\v!max]
\defineinteractionbar[d][\c!command=\scrn_bar_alternative_d,\c!width=.5\emwidth]
\defineinteractionbar[e][\c!command=\scrn_bar_alternative_e]
\defineinteractionbar[f][\c!command=\scrn_bar_alternative_f]
\defineinteractionbar[g][\c!command=\scrn_bar_alternative_g,\c!height=\v!broad]

\protect \endinput
