%D \module
%D   [       file=core-int,
%D        version=1995.01.01,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Interaction,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% evt interactionbaren runtime laden (scheelt 8K)

%D Still to be done properly.

\writestatus{loading}{Context Core Macros / Interaction}

% interactions 5 and 6 to be translated

\startmessages  dutch  library: interactions
  title: interactie
      1: aspect ratio -- x -- (b x h)
      2: actief
      3: niet actief
      4: geen paginasynchronisatie (--) in hmode
      5: onbekend attachment --
      6: attachment file -- bestaat niet
\stopmessages

\startmessages  english  library: interactions
  title: interaction
      1: aspect ratio -- x -- (b x h)
      2: active
      3: inactive
      4: no pagesynchronisation (--) in hmode
      5: unknown attachment --
      6: attachment file -- does not exist
\stopmessages

\startmessages  german  library: interactions
  title: Interaktion
      1: Aspekt des Verhaeltnis -- x -- (B x H)
      2: aktiv
      3: inaktiv
      4: keine Seitensynchronisation (--) im hmode
      5: unknown attachment --
      6: attachment file -- does not exist
\stopmessages

\startmessages  czech  library: interactions
  title: interakce
      1: pomer -- x -- (s x v)
      2: aktivni
      3: neaktivni
      4: zadna strankova synchronizace (--) v hmode
      5: unknown attachment --
      6: attachment file -- does not exist
\stopmessages

\startmessages  italian  library: interactions
  title: interazione
      1: rapporto -- x -- (b x a)
      2: attiva
      3: inattiva
      4: sincronizzazione di pagina (--) non disponibile in hmode
      5: unknown attachment --
      6: attachment file -- does not exist
\stopmessages

\startmessages  norwegian  library: interactions
  title: interaksjon
      1: forholdstall -- x -- (b x h)
      2: aktiv
      3: inaktiv
      4: ingen sidesynkronisering (--) i hmode
      5: unknown attachment --
      6: attachment file -- does not exist
\stopmessages

\startmessages  romanian  library: interactions
  title: interactiuni
      1: aspectul -- x -- (b x h)
      2: activ
      3: inactiv
      4: nu exista sincronizare pt. pagini (--) in hmode
      5: unknown attachment --
      6: attachment file -- does not exist
\stopmessages

\startmessages  dutch  library: versions
  title: versie
      1: er mankeert een @+
      2: markeren pagina's
      3: geselecteerde pagina's: --
\stopmessages

\startmessages  english  library: versions
  title: version
      1: missing @+
      2: marking pages
      3: selected pages: --
\stopmessages

\startmessages  german  library: versions
  title: Version
      1: fehlendes @+
      2: Erstelle Seiten
      3: Ausgewaehlte Seiten: --
\stopmessages

\startmessages  czech  library: versions
  title: verze
      1: postradam @+
      2: oznacuji se strany
      3: oznacene strany: --
\stopmessages

\startmessages  italian  library: versions
  title: version
      1: @+ mancante
      2: marcatura pagine
      3: pagine selezionate: --
\stopmessages

\startmessages  norwegian  library: versions
  title: versjon
      1: manglende @+
      2: markerer sider
      3: valgte sider: --
\stopmessages

\startmessages  romanian  library: versions
  title: versiuni
      1: lipseste @+
      2: pagini marcate
      3: pagini selectate: --
\stopmessages

\unprotect

\definesystemconstant {link}

\definesystemvariable {lk}

% \expand vs \expanded

% linked registers implementeren als een koppeling == mooier

\presetlocalframed[\??lk]

\newcounter\numberoflinks

\def\stelkoppelingenin%
  {\dodoubleargument\getparameters[\??lk]}

\def\definieerkoppeling[#1]%  % local loading !
  {\doifundefined{\s!link:#1:\s!list}
     {\expanded{\definetwopasslist{\s!link:#1}}%
      \expanded{\doloadtwopassdata{\s!link:#1}}%
      \getfirsttwopassdata{\s!link:#1}%
      \setxvalue{\s!link:#1:f}{\twopassdata}%
      \getlasttwopassdata{\s!link:#1}%
      \setxvalue{\s!link:#1:l}{\twopassdata}%
      \setxvalue{\s!link:#1:s}{\noftwopassitems}%
      \gettwopassdata{\s!link:#1}%
      \setxvalue{\s!link:#1:c}{\twopassdata}%
      \setxvalue{\s!link:#1:n}{\twopassdata}}}

\def\koppeling[#1]#2%
  {\bgroup
   \definieerkoppeling[#1]%
   \doglobal\increment\numberoflinks
   \gettwopassdata{\s!link:#1}%
   \edef\numberoflinks{0\getvalue{\s!link:#1:s}}%
   \edef\firstlink{0\getvalue{\s!link:#1:f}}%
   \edef\lastlink{0\getvalue{\s!link:#1:l}}%
   \edef\currentlink{0\getvalue{\s!link:#1:n}}%
   \edef\prevlink{0\getvalue{\s!link:#1:c}}%
   \iftwopassdatafound
     \edef\nextlink{0\twopassdata}%
     \setxvalue{\s!link:#1:n}{\nextlink}%
     \setxvalue{\s!link:#1:c}{\currentlink}%
   \else
     \edef\nextlink  {0\getvalue{\s!link:#1:l}}%
   \fi
   \edef\writelink
     {\writeutilitycommand
        {\twopassentry
           {\s!link:#1}%
           {\numberoflinks}%
           {\noexpand\realfolio}}}%
   \writelink
   \ifnum\noflinks<2
     \locationfalse
   \fi
   \iflocation
     \hbox
       {%\setupinteraction[\c!width=\!!zeropoint]%
        \setinteractionparameter\c!width\!!zeropoint
        \doganaareenpagina\??lk\gotobegincharacter\firstlink\hss
        \ifnum\noflinks>2
          \hskip\@@lkafstand
          \doganaareenpagina\??lk\gobackwardcharacter\prevlink\hss
        \fi
        \hskip\@@lkafstand
        #2\relax
        \hskip\@@lkafstand
        \ifnum\noflinks>2
          \doganaareenpagina\??lk\goforwardcharacter\nextlink\hss
          \hskip\@@lkafstand
        \fi
        \doganaareenpagina\??lk\gotoendcharacter\lastlink}%
   \else
     \hbox{#2}%
   \fi
   \egroup}

\def\definieerkoppeling[#1]%  % local loading !
  {\doifundefined{\s!link:#1:\s!list}
     {\expanded{\definetwopasslist{\s!link:#1}}%      \expanded{\doloadtwopassdata{\s!link:#1}}%
      \getfirsttwopassdata{\s!link:#1}%
      \let\firstlink\twopassdata
      \getlasttwopassdata{\s!link:#1}%
      \let\lastlink\twopassdata
      \let\noflinks\noftwopassitems
      \gettwopassdata{\s!link:#1}%
      \let\currentlink\twopassdata
      \let\nextlink\twopassdata
      \setxvalue{\s!link:#1:}%
        {\firstlink:\lastlink:\noflinks:\currentlink:\nextlink}}}

\def\koppeling[#1]#2%
  {\bgroup
   \definieerkoppeling[#1]%
   \doglobal\increment\numberoflinks
   \gettwopassdata{\s!link:#1}%
   \def\next[##1:##2:##3:##4:##5]%
     {\edef\firstlink  {0##1}%
      \edef\lastlink   {0##2}%
      \edef\noflinks   {0##3}%
      \edef\prevlink   {0##4}%
      \edef\currentlink{0##5}}%
   \expanded{\next[\getvalue{\s!link:#1:}]}%
   \edef\nextlink
     {0\iftwopassdatafound\twopassdata\else\lastlink\fi}%
   \setxvalue{\s!link:#1:}%
     {\firstlink:\lastlink:\noflinks:\currentlink:\nextlink}%
   \edef\writelink
     {\writeutilitycommand
        {\twopassentry
           {\s!link:#1}%
           {\numberoflinks}%
           {\noexpand\realfolio}}}%
   \writelink
   \ifnum\noflinks<2
     \locationfalse
   \fi
   \iflocation
     \hbox
       {%\setupinteraction[\c!width=\!!zeropoint]%
        \setinteractionparameter\c!width\!!zeropoint
        #2\relax
        \hskip\@@lkafstand
        \doganaareenpagina\??lk\gotobegincharacter\firstlink\hss
        \ifnum\noflinks>2
          \doganaareenpagina\??lk\gobackwardcharacter\prevlink\hss
        \fi
        \ifnum\noflinks>2
          \doganaareenpagina\??lk\goforwardcharacter\nextlink\hss
          \hskip\@@lkafstand
        \fi
        \doganaareenpagina\??lk\gotoendcharacter\lastlink}%
   \else
     \hbox{#2}%
   \fi
   \egroup}

\let\setupinteractionscreens\empty

\def\docalculateinteractionscreen
  {\doifelse\@@scwidth\v!fit
     {\!!widtha\leftedgewidth
      \advance\!!widtha \leftedgedistance
      \advance\!!widtha \leftmarginwidth
      \advance\!!widtha \leftmargindistance
      \ifdim\backspace>\!!widtha\ifdim\backspace>\zeropoint\relax
        \advance\backspace -\!!widtha
      \fi\fi
      \advance\!!widtha \makeupwidth
      \advance\!!widtha \rightmargindistance
      \advance\!!widtha \rightmarginwidth
      \advance\!!widtha \rightedgedistance
      \advance\!!widtha \rightedgewidth
      \scratchdimen\@@scbackspace
      \advance\scratchdimen \@@schoroffset
      \advance\!!widtha 2\scratchdimen}
     {\doifelse\@@scwidth\v!max
        {\!!widtha\printpaperwidth}
        {\!!widtha\@@scwidth}}%
   \doifelse\@@scheight\v!fit
     {\!!heighta\topheight
      \advance\!!heighta \topdistance
      \ifdim\topspace>\!!heighta\ifdim\topspace>\zeropoint\relax
        \advance\topspace -\!!heighta
      \fi\fi
      \advance\!!heighta \makeupheight
      \advance\!!heighta \bottomdistance
      \advance\!!heighta \bottomheight
      \scratchdimen\@@sctopspace
      \advance\scratchdimen \@@scveroffset
      \advance\!!heighta 2\scratchdimen}
     {\doifelse\@@scheight\v!max
        {\!!heighta\printpaperheight}
        {\!!heighta\@@scheight}}%
   \doif\@@scdelay\v!none{\let\@@scdelay\zerocountervalue}}

% The macro is not to be changed; only the \@@ia-variables
% may be set! ConTeXt is the producer but we no longer
% mention the pragma site, since we don't want to be bothered
% with remarks about third party documents and/or associated
% with documents produced outside our control.

\def\doprepareidentity
  {\let\!!stringa\@@iakeyword
   \let\@@iakeyword\empty
   \def\doprepareidentity##1{\edef\@@iakeyword
     {\@@iakeyword\ifx\@@iakeyword\empty\else\space\fi##1}}%
   \@EA\processcommacommand\@EA[\!!stringa]\doprepareidentity
   \global\let\doprepareidentity\relax}

\def\doinitializeidentity#1%
  {%\iflocation
     \doprepareidentity
     \dosetupidentity % no \expanded{..} will be done in special (else no pdfdoc)
       {\@@iatitle}{\@@iasubtitle}{\@@iaauthor}{#1}%
       {\@@iadate}{\@@iakeyword}%
     \global\let\initializeidentity\relax
  }%\fi}

\def\initializeidentity % not to be changed !
  {\doinitializeidentity{ConTeXt/user/\jobname.tex}}

\appendtoks \initializeidentity \to \everyshipout

\def\initializepaper
  {\bgroup
   \ifx\@@ppleft \empty
   \ifx\@@ppright\empty
   \ifx\@@pptop \empty
   \ifx\@@ppbottom \empty
   \ifx\@@pcstate\v!start
     \locationfalse\fi\else
     \locationfalse\fi\else
     \locationfalse\fi\else
     \locationfalse\fi\else
     \locationfalse\fi
   \iflocation % without screen settings
     \egroup
     \dosetuppaper\papersize\paperwidth\paperheight
   \else
     \egroup
     \dosetuppaper\printpapersize\printpaperwidth\printpaperheight
   \fi}

\appendtoks \initializepaper \to \everyshipout

\def\doinitializepaper
  {\bgroup
   \docalculateinteractionscreen
   \ifdim\!!widtha>\paperwidth\ifdim\!!widtha>\zeropoint
     \paperwidth\!!widtha
   \fi\fi
   \ifdim\!!heighta>\paperheight\ifdim\!!heighta>\zeropoint
     \paperheight\!!heighta
   \fi\fi
   \dosetuppaper
     {\printpapersize}
     {\the\paperwidth}
     {\the\paperheight}%
   \egroup}

\let\@@pcscreendata\empty

\def\dosetupinteractionscreens % met a, b en \number
  {\doifnot\@@pcstate\v!start\dodosetupinteractionscreens}

\setvalue{\??sc\c!option\v!max        }{1} % tzt share with driver
\setvalue{\??sc\c!option\v!bookmark   }{2} % tzt share with driver
\setvalue{\??sc\c!option\v!fit        }{3} % tzt share with driver
\setvalue{\??sc\c!option\v!doublesided}{4} % tzt share with driver

\def\dodosetupinteractionscreens % met a, b en \number
  {\bgroup
   \docalculateinteractionscreen
   \!!counte=0\getvalue{\??sc\c!option\@@scoption}\relax
   % niet waterdicht
   \doifnot{\the\!!widtha\the\!!heighta}\@@pcscreendata
     {\xdef\@@pcscreendata{\the\!!widtha\the\!!heighta}%
      \showmessage\m!interactions1{\withoutpt\the\!!widtha,\withoutpt\the\!!heighta}}%
   % needs to be split: dimensions for each page
   % and mode per document and only once !
   \dosetupscreen \rugoffset\kopoffset\!!widtha\!!heighta{\the\!!counte}%
   \dosetupcropbox\rugoffset\kopoffset\!!widtha\!!heighta
   \egroup}

\def\dosetupinteractionscreen[#1]%
  {\getparameters[\??sc][#1]%
   \ifproductionrun
     \let\initializepaper\doinitializepaper
     \let\setupinteractionscreens\dosetupinteractionscreens
   \fi}

\appendtoks \setupinteractionscreens \to \everyfirstshipout % needed to get option=max etc working
\appendtoks \setupinteractionscreens \to \everyshipout      % needed for page/screen dimensions

\def\setupinteractionscreen
  {\dosingleempty\dosetupinteractionscreen}

%D Due to requests I finally decided to support bookmarks, a
%D driver dependant way of showing tables of content. The most
%D simple way of support is hooking bookmark generation into
%D the existing list mechanisms. That way users can generate
%D bookmarks automatically, although its entirely valid to add
%D bookmarks by defining alternative ones. These will be added
%D at the appropriate place in the list.

% \hoofdstuk{het eerste hoofdstuk}
%
% \bookmark {de eerste bookmark} % optional overruled hoofdstuk
%
% .... text ....
%
% \placebookmarks [hoofdstuk,paragraaf,subparagraaf,subsubparagraaf,mylist]
%                 [open list]
%
% \bookmark[mylist]{whatever}

\def\@@bookmark {bm::}
\def\@@booklevel{bl::}
\def\@@bookcount{bc::}

\definelist[\@@bookmark]

% \appendtoks\flushpostponedbookmark\to\everypar
% \appendtoks\flushpostponedbookmark\to\neverypar
%
% \let\flushpostponedbookmark\relax
%
% \def\simplebookmark#1%
%   {\ifx\flushpostponedbookmark\relax \else
%      \bgroup
%      \convertargument#1\to\ascii
%      \writestatus\m!systems{clashing bookmarks: \ascii}% ECHTE MESSAGE MAKEN
%      \egroup
%    \fi
%    \doglobal\prependtoks
%      \writetolist[\@@bookmark]{}{#1}%
%    \to\postponedbookmarks}

\newtoks\postponedbookmarks

\def\flushpostponedbookmark
  {\the\postponedbookmarks
   \global\postponedbookmarks\emptytoks}

\def\simplebookmark#1%
  {\doglobal\prependtoks
     \writetolist[\@@bookmark]{}{#1}%
   \to\postponedbookmarks}

\def\complexbookmark[#1]#2%
  {\doglobal\appendtoks\writetolist[#1]{}{#2}\to\postponedbookmarks}

\definecomplexorsimple\bookmark

\newif\iftracebookmarks \tracebookmarksfalse

\let\tracebookmarks\tracebookmarkstrue

\def\placebookmarks
  {\dodoubleempty\doplacebookmarks}

\def\doplacebookmarks[#1][#2]%
  {\iflocation
     \iffirstargument
       \bgroup
       \ifsecondargument
         \edef\openbookmarklist{#2}%
       \else
         \let\openbookmarklist\empty
       \fi
       \global\let\bookmarklevellist\empty
       \def\bookmarklevelcount{0}%
       \doprocessbookmarks[#1]\dogetbookmarkelement
       \dolistelement{}{}{}{}{}{}% needed to finish the first pass
       \doprocessbookmarks[#1]\doputbookmarkelement
       \flushbookmark
       \egroup
     \else
       \expanded{\placebookmarks\@EA[\getvalue{\??ih\v!content\c!list}]}%
     \fi
   \fi}

\def\doprocessbookmarks[#1]#2%
  {\let\dolistelement#2\relax
   \scratchcounter\zerocount
   \def\docommando##1%
     {\advance\scratchcounter \plusone
      \getlistlevel[##1]\listlevel{\the\scratchcounter}%
      \setxvalue{\@@bookcount\the\scratchcounter}{1}%
      \setxvalue{\@@booklevel##1}{\listlevel}}%
   \processcommalist[#1]\docommando
   \setxvalue{\@@bookcount0}{1}%
   \global\chardef\currentbookmarklevel\zerocount
   \global\chardef\previousbookmarklevel\zerocount
   \doutilities{listentries,#1,\@@bookmark}\jobname{#1}\relax\relax}

\def\dodogetbookmarkelement#1#2#3#4#5#6%
  {\doifelsenothing{#1}
     {\global\chardef\currentbookmarklevel\zerocount}
     {\global\chardef\currentbookmarklevel\getvalue{\@@booklevel#1}\relax}%
   \ifnum\currentbookmarklevel>\previousbookmarklevel
     \setxvalue{\@@bookcount\the\currentbookmarklevel}{1}%
   \else\ifnum\currentbookmarklevel<\previousbookmarklevel
     \bgroup
     \!!counta\previousbookmarklevel
     \doloop
       {\let\bookmarktag\empty
        \!!countb\!!counta
        \advance\!!countb \minusone
        \dorecurse\!!countb
          {\edef\bookmarktag
             {\bookmarktag\getvalue{\@@bookcount\recurselevel}:}}%
        \edef\bookmarklevelcount
          {\getvalue{\@@bookcount\the\!!counta}}%
        \xdef\bookmarklevellist
          {\bookmarklevellist/\bookmarktag:\bookmarklevelcount/}%
        \advance\!!counta \minusone
        \ifnum\!!counta=\currentbookmarklevel
          \exitloop
        \fi}%
     \egroup
     \@EA\doglobal\@EA\increment\csname \@@bookcount\the\currentbookmarklevel\endcsname\relax
   \else
     \@EA\doglobal\@EA\increment\csname \@@bookcount\the\previousbookmarklevel\endcsname\relax
   \fi\fi
   \global\utilitydonetrue
   \global\chardef\previousbookmarklevel\currentbookmarklevel}

\def\getbookmarklevelcount
  {\@EA\def\@EA\docommando\@EA[\@EA##\@EA1\@EA/\bookmarktag:##2/##3]%
     {\def\bookmarklevelcount{##2}}%
   \@EA\@EA\@EA\docommando\@EA\@EA\@EA[\@EA\bookmarklevellist\@EA/\bookmarktag:0/]}

\def\dodoputbookmarkelement#1#2#3#4#5#6%
  {\doifelsenothing{#1}
     {\global\chardef\currentbookmarklevel\zerocount}
     {\global\chardef\currentbookmarklevel\getvalue{\@@booklevel#1}\relax}%
   \ifnum\currentbookmarklevel>\previousbookmarklevel
     \setxvalue{\@@bookcount\the\currentbookmarklevel}{1}%
   \else\ifnum\currentbookmarklevel<\previousbookmarklevel
     \@EA\doglobal\@EA\increment\csname \@@bookcount\the\currentbookmarklevel\endcsname\relax
   \else
     \@EA\doglobal\@EA\increment\csname \@@bookcount\the\previousbookmarklevel\endcsname\relax
   \fi\fi
   \let\bookmarktag\empty
   \!!countb\currentbookmarklevel
   \dorecurse\!!countb
     {\edef\bookmarktag
        {\bookmarktag\getvalue{\@@bookcount\recurselevel}:}}%
   \getbookmarklevelcount
   \iftracebookmarks
     \bgroup
     \par
     \bookmarktag\quad
     \dorecurse\currentbookmarklevel{\quad}\unskip#1\quad
     (\bookmarklevelcount)\quad
     \egroup
   \fi
   \global\chardef\previousbookmarklevel\currentbookmarklevel
   \global\utilitydonetrue
   \insertsomebookmark{#1}{\the\currentbookmarklevel}{\bookmarklevelcount}{#4}{#6}}

\def\dogetbookmarkelement#1#2#3#4#5#6%
  {\doifnot{#1}\@@bookmark
     {\dodogetbookmarkelement{#1}{#2}{#3}{#4}{#5}{#6}}}

\def\doputbookmarkelement#1#2#3#4#5#6%
  {\doifelse{#1}\@@bookmark
     {\localbookmark{#4}}
     {\flushbookmark
      \dodoputbookmarkelement{#1}{#2}{#3}{#4}{#5}{#6}}}

\let\flushbookmark\relax
\let\localbookmark\gobbleoneargument

\def\insertsomebookmark#1#2#3#4#5%
  {\gdef\flushbookmark
     {\doinsertsomebookmark{#1}{#2}{#3}{#4}{#5}{g}}%
   \gdef\localbookmark##1%
     {\doinsertsomebookmark{#1}{#2}{#3}{##1}{#5}{l}}}

\def\doinsertsomebookmark#1#2#3#4#5#6%
  {\global\utilitydonetrue
   \global\let\localbookmark\gobbleoneargument
   \global\let\flushbookmark\relax
   \doifinstringelse{#1}\openbookmarklist
     {\chardef\openbookmark\plusone}
     {\chardef\openbookmark\zerocount}%
   \iftracebookmarks(#6: #4)\quad(\the\openbookmark)\par\fi
   \doinsertbookmark{#2}{#3}{#4}{#5}{\openbookmark}}

% \startinteractionmenu[rechts]
%   \but [eerste]  eerste  \\
%   \txt hello world       \\
%   \but [tweede]  tweede  \\
%   \nop                   \\
%   \but [tweede]  tweede  \\
%   \rul whow              \\
%   \but [tweede]  tweede  \\
%   \raw hello world       \\
%   \but [tweede]  tweede  \\
%   \com \vfill            \\
%   \but [derde]   derde   \\
% \stopinteractionmenu

\newif\iflocationmenupermitted

\def\testinteractionmenu#1%
   {\iflocation
      \doifelse\@@iamenu\v!on
        {\doifelsevalue{\??am#1\c!state}\v!start
           {\global\locationmenupermittedtrue}
           {\global\locationmenupermittedfalse}}
        {\global\locationmenupermittedfalse}%
    \else
      \global\locationmenupermittedfalse
    \fi}

\def\dodisableinteractionmenu[#1][#2][#3]%
  {\def\dododisableinteractionmenu##1%
     {\doifelse{#3}{}
        {\letvalue{\??am##1\c!obstruction}\empty}
        {\edef\interactieblokkade{\getvalue{\??am##1\c!obstruction}}
         \def\docommando####1%
           {#1{####1}{\interactieblokkade}}% #1 = \remove or \add
         \processcommalist[#3]\docommando
         \setevalue{\??am##1\c!obstruction}{\interactieblokkade}}}%
   \processcommalist[#2]\dododisableinteractionmenu}

\def\disableinteractionmenu
  {\dotripleempty\dodisableinteractionmenu[\addtocommalist]}

\def\enableinteractionmenu
  {\dotripleempty\dodisableinteractionmenu[\removefromcommalist]}

% ja   : kader/achtergrond met tekst
% leeg : kader/achtergrond maar geen tekst
% nee  : alleen ruimte reserveren
% geen : helemaal weglaten

\newif\iflocationdummy
\newif\ifskippedmenuitem

\newif\iflocationempty
\newif\iflocationclick

% ja   : kader/achtergrond met tekst
% leeg : kader/achtergrond maar geen tekst
% nee  : alleen ruimte reserveren
% geen : helemaal weglaten
%
% \setupinteractionmenu[right][samepage=yes,  unknownreference=yes]
% \setupinteractionmenu[right][samepage=empty,unknownreference=empty]
% \setupinteractionmenu[right][samepage=no,   unknownreference=no]
% \setupinteractionmenu[right][samepage=none, unknownreference=none]
%
% \startinteractionmenu[right]
% \but [firstpage] first \\
% \but [lastpage] last \\
% \but [somepage] crap \\
% \stopinteractionmenu

\def\dosetlocationboxcontent#1[#2]#3[#4]%
  {\global\skippedmenuitemfalse
   \setbox\locationbox\hbox
     {\resetgoto % anders cyclische aanroep !
      \localframed[#1][#2]{\dolocationattributes{#1}\c!style\c!color{#3}}}%
   \iflocationclick
     \hbox{\gotolocation{#4}{\box\locationbox}}%
   \else
     \hbox{\box\locationbox}%
   \fi}

\let\dosetlocationboxyes\dosetlocationboxcontent

\def\dosetlocationboxempty#1[%
  {\dosetlocationboxcontent{#1}[\c!empty=\v!yes,}

\def\dosetlocationboxno#1[%
  {\dosetlocationboxcontent{#1}[\c!empty=\v!yes,\c!frame=,\c!background=,}

\def\dosetlocationboxnone#1[#2]#3[#4]%
  {\global\skippedmenuitemtrue}

% the following version looks ok but is not, since it is unaware of
% some reference properties
%
% \def\setlocationboxyes#1%
%   {\locationclicktrue
%    \ifx\currentouterreference\empty
%      \ifrealreferencepage\!!doneatrue\else\!!doneafalse\fi
%    \else
%      \!!doneafalse
%    \fi
%    \if!!donea
%      \ifcase\csname\??am\??am\csname#1\c!samepage\endcsname\endcsname\relax
%        \copycsname#1\c!color\endcsname\csname#1\c!contrastkleur\endcsname
%        \@EAEAEA\dosetlocationboxyes
%      \or
%        \@EAEAEA\dosetlocationboxempty
%      \or
%        \@EAEAEA\dosetlocationboxno
%      \or
%        \@EAEAEA\dosetlocationboxnone
%      \fi
%    \else
%      \@EA\dosetlocationboxcontent
%    \fi{#1}}
%
% \def\setlocationboxnop#1%
%   {\locationclickfalse
%    \ifcase\csname\??am\??am\csname#1\c!unknownreference\endcsname\endcsname\relax
%      \@EA\dosetlocationboxyes
%    \or
%      \@EA\dosetlocationboxempty
%    \or
%      \@EA\dosetlocationboxno
%    \or
%      \@EA\dosetlocationboxnone
%    \fi{#1}}
%
% \def\setlocationbox#1[#2]#3[#4]%
%   {\bgroup % really needed !
%    \edef\permittedreferences{\csname#1\c!obstruction\endcsname}%
%    \doifreferencepermittedelse{#4}
%      {\setlocationboxyes{#1}[#2]{#3}[#4]}
%      {\setlocationboxnop{#1}[#2]{#3}[#4]}%
%    \egroup}
%
% \def\setlocationboxraw#1[#2]#3[#4]%
%   {\localframed[#1][#2]{\dolocationattributes{#1}\c!style\c!color{#3}}}

\def\setlocationboxyes#1[#2]#3[#4]%
  {\locationclicktrue
   \setbox\locationbox\hbox
     {\resetgoto % anders cyclische aanroep !
      \global\skippedmenuitemfalse
      \gotolocation
        {#4}% % needed
        {\ifrealreferencepage
           \ifcase\csname\??am\??am\csname#1\c!samepage\endcsname\endcsname\relax
             \copycsname#1\c!color\endcsname\csname#1\c!contrastcolor\endcsname
             \localframed[#1][#2]{\dolocationattributes{#1}\c!style\c!color{#3}}%
           \or
             \localframed[#1][\c!empty=\v!yes,#2]{\dolocationattributes{#1}\c!style\c!color{#3}}%
           \or
             \localframed[#1][\c!empty=\v!yes,\c!frame=,\c!background=,#2]{\dolocationattributes{#1}\c!style\c!color{#3}}%
           \or
             \global\skippedmenuitemtrue
           \fi
         \else
           \localframed[#1][#2]{\dolocationattributes{#1}\c!style\c!color{#3}}%
         \fi}}%
   \ifskippedmenuitem\else\box\locationbox\fi}

\def\setlocationboxnop#1[#2]#3[#4]%
  {\locationclickfalse
   \setbox\locationbox\hbox
     {\resetgoto % anders cyclische aanroep !
      \global\skippedmenuitemfalse
      \ifcase\csname\??am\??am\csname#1\c!unknownreference\endcsname\endcsname\relax
        \localframed[#1][#2]{\dolocationattributes{#1}\c!style\c!color{#3}}%
      \or
        \localframed[#1][\c!empty=\v!yes,#2]{\dolocationattributes{#1}\c!style\c!color{#3}}%
      \or
        \localframed[#1][\c!empty=\v!yes,\c!frame=,\c!background=,#2]{\dolocationattributes{#1}\c!style\c!color{#3}}%
      \or
        \global\skippedmenuitemtrue
      \fi}%
   \ifskippedmenuitem\else\box\locationbox\fi}

\def\setlocationboxraw#1[#2]#3[#4]%
  {\localframed[#1][#2]{\dolocationattributes{#1}\c!style\c!color{#3}}}

\def\setlocationbox#1[#2]#3[#4]%
  {\bgroup % really needed !
   \edef\permittedreferences{\csname#1\c!obstruction\endcsname}%
   \doifreferencepermittedelse{#4}%
     {\setlocationboxyes{#1}[#2]{#3}[#4]}%
     {\setlocationboxnop{#1}[#2]{#3}[#4]}%
   \egroup}

\def\dodosetlocationcommanditem#1#2#3[#4]#5\\%
  {\bgroup
   \leavevmode
   \doifelse{#5}{[]}
     {\doifassignmentelse{#4}{#3}{\setlocationbox{\??am#1}[]{#3}[#4]}}
     {#3}%
   \ifskippedmenuitem \else
     \getvalue{\??am#1#2}%
   \fi
   \egroup}

\def\dosetlocationcommanditem#1#2#3%
  {\dodosetlocationcommanditem{#1}{#2}#3[]\\}

\def\setlocationnop#1[#2]#3%
  {\localframed[#1][#2]{#3}}

\def\executeamboxcommands#1#2#3#4#5%
  {%\processaction
   %  [\getvalue{\??am#1\c!dummy}]
   %  [  \v!yes=>\chardef\handleunknownmenuitem=0\relax,
   %   \v!empty=>\chardef\handleunknownmenuitem=1\relax,
   %      \v!no=>\chardef\handleunknownmenuitem=2\relax]%
   \getvalue{\??am#1#3}\relax
   \ifextendedmenu
     \setamboxcommands{#1}{#4}%
     \def\next
       {\ignorespaces#2}%
   \else
     \def\dolocationcommand##1%
       {\dosetlocationcommanditem{#1}{#4}{##1}}%
     \def\next
       {\processcommalist[#2]\dolocationcommand}%
   \fi
   \next
   \unskip
   \getvalue{\??am#1#5}}

\newcounter\currentamposition

\newtoks\everysetmenucommands

\def\setamboxcommands#1#2%
  {\def\currentmenu{#1}%    % kan nog eerder
   \def\currentsubmenu{#2}% % ? ?
   \doglobal\newcounter\currentamposition
   \the\everysetmenucommands}

\def\menu@@amboxcommand#1\\%
  {\bgroup
   \leavevmode\ignorespaces#1\unskip\relax
   \ifskippedmenuitem \else
     \getvalue{\??am\currentmenu\currentsubmenu}%
   \fi
   \egroup
   \ignorespaces}

\appendtoks
  \let\@@amboxcommand\menu@@amboxcommand
\to \everysetmenucommands

\def\menu@raw[#1]#2\\%
  {\@@amboxcommand\gotobox{\ignorespaces#2\unskip}[#1]\\}%

\def\menu@but[#1]#2\\%
  {\@@amboxcommand\do@@amposition\currentmenu{#1}{\setlocationbox{\??am\currentmenu}[]{\ignorespaces#2\unskip}[#1]}\\}%

\def\menu@got[#1]#2\\% pas op! offset
  {\@@amboxcommand\setlocationbox{\??am\currentmenu}[\c!frame=\v!off,\c!background=]{\ignorespaces#2\unskip}[#1]\\}%

\def\menu@nop#1\\%
 %{\@@amboxcommand\phantom{\localframed[\??am\currentmenu][]{#1}}\\}%
  {\@@amboxcommand\setlocationboxraw{\??am\currentmenu}[\c!frame=\v!off,\c!background=,\c!empty=\v!yes]{\ignorespaces#1\unskip}[]\\}%

\def\menu@txt#1\\%
  {\@@amboxcommand\localframed[\??am\currentmenu][\c!frame=\v!off,\c!background=]{\ignorespaces#1\unskip}\\}%

\def\menu@rul#1\\% ook \do@@amposition !
  {\@@amboxcommand\localframed[\??am\currentmenu][]{\ignorespaces#1\unskip}\\}%

\def\menu@com#1\\%
  {\ignorespaces#1\unskip\ignorespaces}%

\appendtoks
  \let\raw\menu@raw
  \let\but\menu@but
  \let\got\menu@got
  \let\nop\menu@nop
  \let\txt\menu@txt
  \let\rul\menu@rul
  \let\com\menu@com
\to \everysetmenucommands

\ifx\do@@amposition\undefined
  \let\do@@amposition\gobbletwoarguments % hook for positional thingies
\fi

\let\currentmenu\empty

% beware : never change the concept of pbgoffset

\def\@@amhbox#1#2#3#4%
  {\def\currentmenu{#3}%
   \testinteractionmenu{#3}%
   \iflocationmenupermitted
     \bgroup
     \showcomposition
     \def\dolocationcommand##1{\dosetlocationcommanditem{#3}{##1}}%
     \dimen0=\makeupwidth
     \advance\dimen0 \pagebackgroundhoffset
     \advance\dimen0 \pagebackgroundhoffset
     \advance\dimen0 -\getvalue{\??am#3\c!leftoffset}%
     \advance\dimen0 -\getvalue{\??am#3\c!rightoffset}%
     \setbox0\hbox to \dimen0
       {\forgetall
        \executeamboxcommands{#3}{#4}\c!left\c!middle\c!right}%
     \setbox0\hbox{\do@@ammenuposition{#3}{\box0}}%
     \wd0=\makeupwidth
     % geen \ht=#2 setting (yet)
     \hskip-\pagebackgroundhoffset
     \hskip \getvalue{\??am#3\c!leftoffset}%
     \box0\relax
     \egroup
   \else
     #1\relax
   \fi}

\def\@@amvbox#1#2#3#4% don't change skipping, this one works!
  {\def\currentmenu{#3}%
   \testinteractionmenu{#3}%
   \iflocationmenupermitted
     \bgroup
     \showcomposition
     \dimen0=\textheight
     \advance\dimen0 \pagebackgroundvoffset
     \advance\dimen0 \pagebackgroundvoffset
     \advance\dimen0 \pagebackgrounddepth
     \advance\dimen0 -\getvalue{\??am#3\c!topoffset}%
     \advance\dimen0 -\getvalue{\??am#3\c!bottomoffset}%
     \setbox0\vbox to \dimen0
       {\forgetall                     % Voor't geval de afstand
       %\setupblank[\v!standard]%   % (tijdelijk) is aangepast.
        \restorestandardblank
        \hsize#2\relax
        \executeamboxcommands{#3}{#4}\c!before\c!inbetween\c!after}%
     \setbox0\vbox{\hbox{\do@@ammenuposition{#3}{\box0}}}%
     \setbox0\vbox
       {\vskip-\pagebackgroundvoffset
        \vskip\getvalue{\??am#3\c!topoffset}%
        \ht0=\zeropoint
        \box0
        \vskip\pagebackgroundvoffset}% overbodig
     \ht0=\textheight
     \wd0=#2\relax
     \box0
     \egroup
   \else
     #1\relax
   \fi}

\ifx\do@@ammenuposition\undefined
  \let\do@@ammenuposition\gobbleoneargument % hook for positional thingies
\fi

\setvalue{\??am\s!do\v!right}{\@@amvbox{\dodummypageskip\v!right}\rightedgewidth}
\setvalue{\??am\s!do\v!left }{\@@amvbox{\dodummypageskip\v!left}\leftedgewidth}
\setvalue{\??am\s!do\v!top }{\@@amhbox{\dodummypageskip\v!top}\topheight}
\setvalue{\??am\s!do\v!bottom }{\@@amhbox{\dodummypageskip\v!bottom}\bottomheight}

\def\dointeractionmenu#1#2%
  {\getvalue{\??am\s!do\getvalue{\??am#1\c!location}}{#1}{#2}}

\unexpanded\def\interactionmenu[#1]%
  {\getvalue{\??am\c!menu#1}}

\def\horizontalinteractionmenu#1#2#3#4%
  {\ifdim#2>\zeropoint % new
     \dimen2\zeropoint
     \setbox0\hbox
       {\def\docommando##1%
          {\doifnotvalue{\??am##1\c!state}\v!none
             {\hskip\dimen2
              \setbox2\hbox to #2
                {\getvalue{\??am##1#3}\interactionmenu[##1]\getvalue{\??am##1#4}}%
              \doifelsevalue{\??am##1\c!distance}\v!overlay
                {\dimen2\zeropoint
                 \wd2\zeropoint}%
                {\dimen2=\getvalue{\??am##1\c!distance}}%
              \box2}}%
       \startinteraction
       \processcommacommand[\getvalue{\??am#1}]\docommando
       \stopinteraction}%
     \wd0=#2\relax
     \box0\relax
   \fi}

\def\verticalinteractionmenu#1#2#3#4%
  {\ifdim#2>\zeropoint % new
     \dimen2\zeropoint
     \setbox0\vbox
       {\def\docommando##1%
          {\doifnotvalue{\??am##1\c!state}\v!none
             {\vskip\dimen2
              \setbox2\vbox to #2
                {\getvalue{\??am##1#3}\interactionmenu[##1]\getvalue{\??am##1#4}}%
              \doifelsevalue{\??am##1\c!distance}\v!overlay
                {\dimen2\zeropoint
                 \offinterlineskip
                 \dp2\zeropoint
                 \ht2\zeropoint}%
                {\dimen2=\getvalue{\??am##1\c!distance}}%
              \box2}}%
        \startinteraction
        \processcommacommand[\getvalue{\??am#1}]\docommando
        \stopinteraction}%
     \ht0=#2\relax
     \dp0\zeropoint
     \box0\relax
   \fi}

\letvalue{\??am\v!left }\empty
\letvalue{\??am\v!right}\empty
\letvalue{\??am\v!top }\empty
\letvalue{\??am\v!bottom }\empty

% todo : \defineinteractionmenuclass

\def\interactionmenus[#1]%
  {\iflocation
     \getvalue{\??am\??am\c!menu#1}%
   \else
     \dodummypageskip{#1}%
   \fi}

\setvalue{\??am\??am\c!menu\v!left}%
  {\horizontalinteractionmenu\v!left\leftedgewidth\c!left\c!right}
\setvalue{\??am\??am\c!menu\v!right}%
  {\horizontalinteractionmenu\v!right\rightedgewidth\c!left\c!right}
\setvalue{\??am\??am\c!menu\v!top}%
  {\verticalinteractionmenu\v!top\topheight\c!before\c!after}
\setvalue{\??am\??am\c!menu\v!bottom}%
  {\verticalinteractionmenu\v!bottom\bottomheight\c!before\c!after}

% this can be implemented with the following command (which
% is new, undocumented, experimental, untested, etc etc)

\def\defineinteractionmenuclass
  {\dodoubleargument\dodefineinteractionmenuclass}

\def\dodefineinteractionmenuclass[#1][#2]% tag hori|veri
  {\doifelse{#2}\v!vertical
     {\setvalue{\??am\??am\c!menu#1}%
        {\verticalinteractionmenu
           {#1}{\getvalue{\??am#1\c!width}}\c!before\c!after}}
     {\setvalue{\??am\??am\c!menu#1}%
        {\horizontalinteractionmenu
           {#1}{\getvalue{\??am#1\c!height}}\c!left\c!right}}}

% \setupinteraction[menu=on,state=start]
%
% \defineinteractionmenuclass[test]   [vertical]
% \defineinteractionmenuclass[another][horizontal]
%
% \defineinteractionmenu[test]   [left][state=start,width=4cm]
% \defineinteractionmenu[another][top] [state=start,height=1cm]
%
% \startinteractionmenu[test]
%   \but [firstpage] test-a \\
%   \but [nextpage]  test-b \\
% \stopinteractionmenu
%
% \startinteractionmenu[another]
%   \but [firstpage] test-a \\
%   \but [nextpage]  test-b \\
% \stopinteractionmenu
%
% \setupheadertexts[{\interactionmenu[another]}]
%
% \starttext
%
% test \interactionmenu[test] \page
% test \interactionmenu[test] \page
%
% \stoptext

%D This can save complicated menu macros when one want to
%D keep control over parts of a menu (i.e.\ turn them on and
%D off). We could have achieved something similar with modes.

\def\local@@ambox#1#2#3#4% don't change skipping, this one works!
  {\bgroup
   \testinteractionmenu{#3}%
   \iflocationmenupermitted
     \executeamboxcommands{#3}{#4}\c!before\c!inbetween\c!after
   \else
     #1\relax
   \fi
   \egroup}

\unprotected\def\includemenu[#1]%
  {\doifvalue{\??am#1\c!state}\v!local
     {\bgroup
      \letvalue{\??am#1\c!state}\v!start
      \let\@@amvbox\local@@ambox
      \let\@@amhbox\local@@ambox
      \getvalue{\??am\c!menu#1}%
      \egroup}}

%D We also need an explicit position control some day. I'll
%D do that when I need it. [The stacking order.]

% for the moment we will support the old method
%
% \stelinteractionmenuin[right][{abc[xyz]},...]
% \stelinteractionmenuin[right][key=val,...]

\newif\ifextendedmenu

\def\defineinteractionmenu
  {\dotripleempty\dodefineinteractionmenu}

\def\dodefineinteractionmenu[#1][#2]% compatibility hack
  {\convertargument#2\to\ascii        % will disappear soon
   \doifinstringelse[\ascii
     \dodosetupinteractionlistmenux
     \dododefineinteractionmenu
   [#1][#2]}

% [name] [location]
% [name] [location] [pars]

\def\dododefineinteractionmenu[#1][#2][#3]%
  {% main settings
   \letvalue{\??am\c!menu#1}\empty
   % \setvalue{\??am\c!menu#1}%
   %   {\extendedmenufalse\dointeractionmenu{#1}{}}%
   \setvalue{\@@dodolistelement#1}%
     {\def\dosomelistelement{\dodomenulistelement{#1}}}%
   \presetlocalframed[\??am#1]%
   % register location
   \expanded{\addtocommalist{#1}\@EA\noexpand\csname\??am#2\endcsname}%
   % inherit settings
   \doifnot{#1}{#2}
     {\copyparameters[\??am#1][\??am#2]
        [\c!left,\c!middle,\c!right,\c!before,\c!after,\c!inbetween,%
         \c!width,\c!height,\c!distance,\c!offset,%
         \c!frame,\c!framecolor,\c!rulethickness,%
         \c!background,\c!backgroundcolor,\c!backgroundscreen,%
         \c!style,\c!color,\c!contrastcolor,\c!samepage,\c!unknownreference,%
         \c!leftoffset,\c!rightoffset,\c!topoffset,\c!bottomoffset]}%
   % additional settings
   \getparameters[\??am#1][\c!location=#2,\c!obstruction=,#3]}

\def\setupinteractionmenu
  {\dodoubleargument\dosetupinteractionmenu}

\def\dosetupinteractionmenu[#1][% compatibillity hack
  {\doifnextcharelse\bgroup     % will disappear soon
     {\dodosetupinteractionlistmenuy[#1][}
     {\dodosetupinteractionmenu      [#1][}}

\def\dodosetupinteractionlistmenux[#1][#2][#3]% compatibillity hack
  {\setvalue{\??am\c!menu#1}{\extendedmenufalse\dointeractionmenu{#1}{#2}}}

\def\dodosetupinteractionlistmenuy[#1][#2]% compatibillity hack
  {\setvalue{\??am\c!menu#1}%
     {\extendedmenufalse\dointeractionmenu{#1}{#2}}}

\def\dodosetupinteractionmenu[#1][#2]%
  {\def\docommando##1{\getparameters[\??am##1][#2]}%
   \processcommalist[#1]\docommando}

\setvalue{\??am\??am\v!yes  }{0}
\setvalue{\??am\??am\v!empty}{1}
\setvalue{\??am\??am\v!no }{2}
\setvalue{\??am\??am\v!none}{3}
\setvalue{\??am\??am       }{1} % default

\processbetween{\v!interactionmenu}\dostartinteractionmenu

\def\dostartinteractionmenu#1%
  {\dodostartinteractionmenu#1\dodostopinteractionmenu}

\def\dodostartinteractionmenu[#1]#2\dodostopinteractionmenu
  {\setvalue{\??am\c!menu#1}{\extendedmenutrue\dointeractionmenu{#1}{#2}}}

\def\resetinteractionmenu[#1]%
  {\letvalue{\??am\c!menu#1}\empty}

\def\dodomenulistelement#1#2#3#4#5#6#7%
  {\setbox0=\hbox
     {\let\gotolocation\gobbleoneargument % hack to catch last []
%\locationclickfalse % ipv ^
      \docheckrealreferencepage{#7}%
      \setlocationboxyes
        {\??am#1}% % needed !
        []% no settings
        {\limitatetext{#5}{\getvalue{\??li#2\c!maxwidth}}{\unknown}}% % needed !
        []}% normally the destination, catch by gobble
   \@@amboxcommand\do@@amposition{#1}{#7}% beware, we pass the pagenumber
     {\ignorespaces\linklisttoelement{#2}{#3}{#6}{#7}{\box0}\unskip}\\}

% \scherm moet worden als \page

\def\screen
  {\dosingleempty\doscreen}

\def\doscreen[#1]%
  {\iflocation\page[#1]\fi}

\unexpanded\def\menubutton
  {\dodoubleempty\domenubutton}

\def\domenubutton[#1]%
  {\iffirstargument
     \ifsecondargument
       \@EAEAEA\domenubuttonB
     \else
       \doifassignmentelse{#1}
         {\@EAEAEA\domenubuttonC}
         {\@EAEAEA\domenubuttonD}%
     \fi
   \else
     \@EA\domenubuttonA
   \fi[#1]}

\def\domenubuttonA[#1][#2]#3[#4]% normal button, no parameters
  {\bgroup
  %\locationdummytrue
   \setlocationbox\??bt[]{#3}[#4]%
   \egroup}

\def\domenubuttonB[#1][#2]#3[#4]% menu button, with parameters
  {\bgroup
  %\locationdummytrue
   \setlocationbox{\??am#1}[#2]{#3}[#4]%
   \egroup}

\def\domenubuttonC[#1][#2]#3[#4]% normal button, with parameters
  {\bgroup
  %\locationdummytrue
   \setlocationbox\??bt[#1]{#3}[#4]%
   \egroup}

\def\domenubuttonD[#1][#2]#3[#4]% menu button, no parameters
  {\bgroup
  %\locationdummytrue
   \setlocationbox{\??am#1}[]{#3}[#4]%
   \egroup}

\def\domenubox[#1][#2]#3%
  {\bgroup
   \def\setlocationbox##1[##2]##3[##4]%
     {\localframed[##1][##2]{\dolocationattributes{##1}\c!style\c!color{##3}}}%
   \domenubutton[#1][#2]#3[]%
   \egroup}

\def\menubox
  {\dodoubleempty\domenubox}

\def\domenubox[#1][#2]#3%
  {\bgroup
   \let\setlocationbox\setlocationboxraw
   \domenubutton[#1][#2]#3[]%
   \egroup}

% Hier volgen de synchronisatiemacro's:

\def\syncprefix{sync}
\def\syncmarker{syncmark}

%\definemarking[\syncmarker]
%\setupmarking[\syncmarker][\c!expansie=\v!ja]

\newmark\syncmarker

\newcounter\synccounter

\newif\ifsynchronisation

\def\startsynchronization%
  {\iflocation\ifsynchronisation
     \doglobal\increment\synccounter
   \fi\fi}

\def\stopsynchronization%
  {\iflocation\ifsynchronisation
     %\thisisdestination{\syncprefix:\synccounter}%
     \pagereference[\syncprefix:\synccounter]%
     \ifvmode
       \@EA\setmark\@EA\syncmarker\@EA{\synccounter} % \marking[\syncmarker]{\synccounter}%
     \else
       \showmessage\m!interactions4\synccounter
     \fi
   \fi\fi}

\def\synchronize%
  {\startsynchronization
   \stopsynchronization}

\def\dosetupsynchronization[#1]%
  {\getparameters[\??sy][#1]%
   \doifelse\@@systate\v!start
     \synchronisationtrue
     \synchronisationfalse}

\def\setupsynchronization
  {\dosingleargument\dosetupsynchronization}

\def\definesynchronization
  {\dosingleargument\dodefinesynchronization}

\def\setupsynchronizationbar
  {\dodoubleargument\getparameters[\??ba]}

\presetlocalframed[\??ba]

\setvalue{synchronisatie\v!page}[#1]%
  {\bgroup
  %\setupinteraction[\c!width=\!!zeropoint]%
   \setinteractionparameter\c!width\!!zeropoint
   \setbox0\hbox
     {\localframed[\??ba][]{\dolocationattributes\??ba\c!style\c!color{\strut\@@batext}}}%
   \dontcomplain
   \def\atthebottom
     {\leaders\hrule\!!depth1ex\!!height-.5ex\hfil}%
   \def\atthetop##1##2##3%
     {\dimen0=\wd0
      \divide\dimen0 3
      \multiply\dimen0 ##2\relax
      \dimen2=.25em
      \advance\dimen0 -##3\dimen2
      %\gotodestination
      %  {}{#1}{\syncprefix:##1}{}
      %  {\hbox to \dimen0{\color[\locationcolor\@@bacolor]{\atthebottom}}}}%
      \gotobox
        {\hbox to \dimen0{\color[\locationcolor\@@bacolor]{\atthebottom}}}%
        [#1::\syncprefix:##1]}%
   \hbox
     {\def\check##1##2%
        {\edef##2{0##1\syncmarker}%
         \ifnum0##2=0 \def##2{1}\fi}%
      \check\gettopmark\top
      \check\getfirstmark\first
      \check\getbotmark\bot
      \setbox2\hbox to \wd0
        {\ifnum\top=\first\relax
           \ifnum\first=\bot\relax
             \atthetop\first30\relax
           \else
             \atthetop\first21\hss\atthetop\bot11\relax
           \fi
         \else
           \ifnum\first=\bot\relax
             \atthetop\top11\hss\atthetop\first21\relax
           \else
             \atthetop\top11\hss\atthetop\first11\hss\atthetop\bot11\relax
           \fi
         \fi}%
      \wd2=\zeropoint\box2
      \box0\relax}%
   \egroup}

\setvalue{synchronisatie\v!local}[#1]%
  {\bgroup
  %\setupinteraction[\c!width=\!!zeropoint]%
   \setinteractionparameter\c!width\!!zeropoint
   \def\blackrule{\hbox{\vrule\!!height.5em\!!width.5em}}%
   %\gotodestination
   %   {}{##1}{\syncprefix:#1}{0}
   %   {\color[\locationcolor\@@bacolor]{\blackrule}}%
   \gotobox %
     {\color[\locationcolor\@@bacolor]{\blackrule}}%
     [#1::\syncprefix:\synccounter]%
   \egroup}

\def\synchronizationbar[#1][#2]%
  {\iflocation\ifsynchronisation
     \bgroup
     \setupsynchronizationbar
       [\c!text=\getvalue{doc:des:#1},#2]%
     \getvalue{synchronisatie\@@baalternative}[#1]%
     \egroup
   \fi\fi}

% Dit is leuke toepassing van glue!

\newbox\meterbox

\newif\ifbalksymbool

\def\doganaareenpagina#1#2#3% nog checken !
  {\checkreferences  % nodig ??
   \iflocation
     \ifnum#3=\realpageno
       {#2}%
     \else
       \doifelsenothing{#1}
         {\hbox{\gotorealpage{}{}{#3}
            {#2}}}
         {\hbox{\gotorealpage{}{}{#3}
            {\dolocationattributes{#1}\c!style\c!color{#2}}}}%
     \fi
   \else
     {#2}%
   \fi}

\presetlocalframed[\??ib]

\def\interactionbara
  {\iflocation
     \bgroup
    %\setupinteraction[\c!width=\!!zeropoint]%
     \setinteractionparameter\c!width\!!zeropoint
     \setupblackrules[\c!height=\v!max,\c!depth=\v!max]% maten ??
     \!!widthb\@@ibwidth
     \advance\!!widthb -2.75em\relax
     \!!widtha\!!widthb
     \divide\!!widtha \lastpage\relax
     \bgroup
       \advance\realpageno \minusone
       \ifvoid\meterbox
         \bgroup
         \processaction
           [\@@ibstep]
           [   \v!small=>\dimen0=.25em\relax,
              \v!medium=>\dimen0=.5em\relax,
               \v!big=>\dimen0=1em\relax,
             \s!unknown=>\dimen0=\!!widtha]%
         \ifdim\!!widtha<\dimen0\relax
           \!!counta\dimen0\relax
           \!!countb\!!widtha
           \divide\!!counta \!!countb
         \else
           \!!counta\@@ibstep\relax
         \fi
         \!!widtha=\!!counta\!!widtha
         \setbox0\hbox{\blackrule[\c!width=\!!widtha]}%
         \global\setbox\meterbox\hbox to \!!widthb
           {\hss
            % brrr
            \for \teller=1 \to \lastpage \step \!!counta \do
              {\gotorealpage{}{}{\teller}{\copy0}}%
            \hss}%
         \global\wd\meterbox\zeropoint
         \egroup
       \fi
     \egroup
     \noindent
     \strut
     \hbox to \@@ibwidth
       {\dontcomplain
        \setupblackrules[\c!width=1em]%
        \doganaareenpagina\??ib\blackrule\firstpage
        \hss
        \color[middlegray]{\copy\meterbox}%
        \hbox to \!!widthb
          {\ifdim\!!widtha<1em\relax
             \!!widtha=1em\relax
           \fi
           \setupblackrules[\c!width=\!!widtha]%
           \ifnum\realpageno>\plusone
             \!!counta\realpageno
             \advance\!!counta -2\relax
             \hskip\zeropoint\!!plus\!!counta \s!sp\relax % cm gives overflow
             % or just: \hskip\zeropoint\!!plus\!!counta \relax % cm gives overflow
             \doganaareenpagina\??ib\blackrule\prevpage
           \fi
           \color[\@@ibcontrastcolor]{\blackrule[\c!width=.5em]}%
           \ifnum\realpageno<\lastpage\relax
             \doganaareenpagina\??ib\blackrule\nextpage
             \!!counta\lastpage
             \advance\!!counta -\realpageno
             \advance\!!counta \minusone
             \hskip\zeropoint\!!plus\!!counta \s!sp\relax % cm gives overflow
             % or just \hskip\zeropoint\!!plus\!!counta\relax % cm gives overflow
           \fi}%
        \hss
        \doganaareenpagina\??ib\blackrule\lastpage}%
     \egroup
   \fi}

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

\def\interactionbarc
  {\iflocation
     \ifnum\lastpage>\plusone
       \hbox to \@@ibwidth
         {\setupblackrules[\c!height=\@@ibheight,\c!depth=\@@ibdepth]%
          \def\gotox##1%
            {\doganaareenpagina{}{\blackrule[\c!width=##1]}}%
          \dimen0=\@@ibwidth\relax
          \advance\dimen0 -4em
          \!!counta\lastpage
          \advance\!!counta \minusone
          \divide\dimen0 \!!counta
          \!!counta\realpageno
          \advance\!!counta \minusone
          \!!widtha\!!counta\dimen0
          \!!countb\lastpage
          \advance\!!countb -\realpageno
          \!!widthb\!!countb\dimen0
          \startcolor[\locationcolor\@@ibcolor]%
          \gotox{1em}\firstpage
          \hss
          \gotox\!!widtha\prevpage
          \color[\@@ibcontrastcolor]{\blackrule[\c!width=1em]}%
          \gotox\!!widthb\nextpage
          \hss
          \gotox{1em}\lastpage
          \stopcolor}%
     \fi
   \fi}

\def\interactionbard
  {\iflocation\ifshowingsubpage
     \ifnum\nofsubpages>\plusone
       \hbox
       \bgroup
      %\setupinteraction[\c!width=\!!zeropoint]%
       \setinteractionparameter\c!width\!!zeropoint
       \ifbalksymbool % beter: 3 chars assign en 3*box
         \setupsymbolset[\@@iasymbolset]%
         \setbox0\hbox{\symbol[\v!previous]}%
         \setbox2\hbox{\symbol[\v!somewhere]}%
         \setbox4\hbox{\symbol[\v!next]}%
       \else
         \setbox0\hbox
           {\vrule
              \!!height\@@ibheight
              \!!depth\@@ibdepth
              \!!width\@@ibwidth}%
         \setbox2\copy0
         \setbox4\copy0
       \fi
       \startcolor[\locationcolor\@@ibcolor]%
       \for\teller=1\to\nofsubpages\step1\do % brr, \dostepwiserecurse
         {\bgroup
          \increment(\teller,\firstsubpage)\relax
          \decrement\teller\relax
          \ifnum\teller<\realpageno\relax
            \gotorealpage{}{}{\teller}{\copy0}\relax
          \else\ifnum\teller=\realpageno\relax
            \color
              [\@@ibcontrastcolor]
              {\gotorealpage{}{}{\teller}{\copy2}}%
          \else
            \gotorealpage{}{}{\teller}{\copy4}\relax
          \fi\fi
          \egroup
          \hskip\@@ibdistance}%
       \unskip
       \stopcolor
       \egroup
     \fi
   \fi\fi}

\def\interactionbare%  KAN WORDEN GECOMBINEERD MET D
  {\iflocation\ifshowingsubpage
     \ifnum\nofsubpages>\plusone
       \bgroup
       \!!widthb\@@ibdistance
       \multiply\!!widthb \nofsubpages
       \advance\!!widthb -\@@ibdistance % (n-1)
       \!!widtha\@@ibwidth
       \advance\!!widtha -\!!widthb
       \divide\!!widtha \nofsubpages\relax
       \ifdim\!!widtha<\@@ibdistance\relax
         \interactionbarf
       \else
        %\setupinteraction[\c!width=\!!zeropoint]%
         \setinteractionparameter\c!width\!!zeropoint
         \noindent
         \hbox to \@@ibwidth
           \bgroup
             \ifbalksymbool
               \setupsymbolset[\@@iasymbolset]%
               \setbox0\hbox{\symbol[\v!previous]}%
               \setbox2\hbox{\symbol[\v!somewhere]}%
               \setbox4\hbox{\symbol[\v!next]}%
             \else
               \setbox0\hbox
                  {\vrule
                     \!!height\@@ibheight
                     \!!depth\@@ibdepth
                     \!!width\!!widtha}%
               \setbox2\copy0
               \setbox4\copy0
             \fi
             \startcolor[\locationcolor\@@ibcolor]%
             \for\teller=1\to\nofsubpages\step1\do
               {\bgroup
                \increment(\teller,\firstsubpage)\relax
                \decrement\teller\relax
                \ifnum\teller<\realpageno\relax
                  \gotorealpage{}{}{\teller}{\copy0}\relax
                \else\ifnum\teller=\realpageno\relax
                  \color
                    [\@@ibcontrastcolor]
                    {\gotorealpage{}{}{\teller}{\copy2}}%
                \else
                  \gotorealpage{}{}{\teller}{\copy4}\relax
                \fi\fi
                \egroup
                \hss}%
             \unskip
             \stopcolor
           \egroup
       \fi
       \egroup
     \fi
   \fi\fi}

\def\interactionbarf%  !! KAN WORDEN GECOMBINEERD MET D !!
  {\iflocation\ifshowingsubpage
     \ifnum\nofsubpages>\plusone
      %\setupinteraction[\c!width=\!!zeropoint]%
       \setinteractionparameter\c!width\!!zeropoint
       \noindent
       \hbox to \@@ibwidth
       \bgroup
       \!!countb\zerocount
       \loop
         \advance\!!countb \plusone
         \!!countc\nofsubpages
         \divide\!!countc \!!countb
         \advance\!!countc \plusone
         \!!widthb\@@ibdistance
         \multiply\!!widthb \!!countc
         \advance\!!widthb -\@@ibdistance
         \!!widtha\@@ibwidth
         \advance\!!widtha -\!!widthb
         \divide\!!widtha \!!countc
         \ifdim\!!widtha<\@@ibdistance\relax
       \repeat
\advance\!!countc -2
\!!widtha-\@@ibdistance
\!!widtha=\!!countc\!!widtha
\advance\!!widtha \@@ibwidth
\advance\!!countc \plusone
\divide\!!widtha \!!countc
       \ifbalksymbool
         \setupsymbolset[\@@iasymbolset]%
         \setbox0\hbox{\symbol[\v!previous]}%
         \setbox4\hbox{\symbol[\v!somewhere]}%
         \setbox8\hbox{\symbol[\v!next]}%
         \setbox2\copy4
         \setbox6\copy4
       \else
         \setbox0\hbox
           {\vrule
              \!!height\@@ibheight
              \!!depth\@@ibdepth
              \!!width\!!widtha}%
         \setbox4\copy0
         \setbox8\copy0
         \setbox2\hbox
           {\vrule
              \!!height.5\ht0
              \!!depth.5\dp0
              \!!width\!!widtha}%
         \ht2\ht0
         \dp2\dp0
         \setbox6\copy2
       \fi
       \def\gotox##1%
         {\ifnum\teller=\realpageno
            \color
              [\@@ibcontrastcolor]
              {\gotorealpage{}{}{\teller}{\copy##1}}%
          \else
            \gotorealpage{}{}{\teller}{\copy##1}%
          \fi
          \!!countf\zerocount
          \hss}%
       \startcolor[\locationcolor\@@ibcolor]%
       \!!countc\realpageno \advance\!!countc -2
       \!!countd\realpageno \advance\!!countd 2
       \!!countf\zerocount
       \for\teller=\firstsubpage\to\lastsubpage\step1\do
         {\!!doneafalse
          \advance\!!countf \plusone
          \ifnum\teller=\firstsubpage\relax \!!doneatrue \fi
          \ifnum\teller=\lastsubpage\relax  \!!doneatrue \fi
          \ifnum\teller>\!!countc \ifnum\teller<\!!countd \!!doneatrue \fi\fi
          \if!!donea
            \ifnum\teller<\realpageno
              \gotox0%
            \else\ifnum\teller>\realpageno
              \gotox4%
            \else
              \gotox8%
            \fi\fi
          \else\ifnum\!!countf=\!!countb
            \ifnum\teller<\realpageno
              \gotox2%
            \else\ifnum\teller>\realpageno
              \gotox6%
            \else
              \gotox4%
            \fi\fi
          \fi\fi}%
       \unskip
       \stopcolor
       \egroup
     \fi
   \fi\fi}

\def\interactionbarg
  {\ifnum\lastsubpage>\firstsubpage\relax
     \interactionbuttons
       [\v!firstsubpage,\v!previoussubpage,\v!nextsubpage,\v!lastsubpage]%
   \fi}

\def\checkinteractionbar#1#2#3%
  {\ifdim\@@ibwidth=\zeropoint\def\@@ibwidth{#1}\fi
   \doifnothing\@@ibheight{\def\@@ibheight{#2}}%
   \doifnothing\@@ibdepth{\def\@@ibdepth{#3}}}

\def\complexinteractionbar[#1]%
  {\doifelse{#1}\v!reset
    {\global\setbox\meterbox\box\voidb@x}%
    {\bgroup
       \iflocation
         \checksubpages % goes wrong / loads \numberofpages too
         \getparameters[\??ib][#1]%
         \doif\@@ibstate\v!start
           {\startinteraction
            \processaction % breedte defaults !
              [\@@ibalternative]
              [         c=>\checkinteractionbar{.5em}\v!max \v!max,
                        d=>\checkinteractionbar{.5em}{.5em} \!!zeropoint,
                        e=>\checkinteractionbar{.5em}{.5em} \!!zeropoint,
                        f=>\checkinteractionbar{.5em}{.5em} \!!zeropoint,
               \s!default=>\checkinteractionbar{10em}\v!broad\!!zeropoint,
               \s!unknown=>\checkinteractionbar{10em}\v!broad\!!zeropoint]%
            \doifelse\@@ibsymbol\v!yes
              \balksymbooltrue\balksymboolfalse
            \getvalue{interactionbar\@@ibalternative}%
            \stopinteraction}%
       \fi
     \egroup}}

\definecomplexorsimpleempty\interactionbar

\def\setupinteractionbar
  {\dodoubleargument\getparameters[\??ib]}

% Er wordt vooralsnog uitgegaan van een symmetrische
% start-stop situatie.

\def\c!profiel!! {profiel:}  % brrr
\def\c!versie!!  {versie:}

\def\dodefineprofile[#1][#2]%
  {\iflocation
     \def\dododefineprofile##1%
       {\def\dodododefineprofile####1%
          {\doifdefinedelse{\c!profiel!!####1}%
             {\edef\!!stringa{\getvalue{\c!profiel!!####1}}%
              \setevalue{\c!profiel!!####1}{\!!stringa,##1}}%
             {\setevalue{\c!profiel!!####1}{##1}}}%
        \processcommalist[#2]\dodododefineprofile}%
     \processcommalist[#1]\dododefineprofile
   \fi}

\def\defineprofile%
  {\dodoubleargument\dodefineprofile}

% Als met \getpar wordt gewerkt, dan moet \next worden toegepast.

% TZT initialisatie!

\def\profilepage{}

\let\dosetprofilepage\relax
\let\dogetprofilepage\relax

\def\processprofile#1[#2]%
  {\iflocation
     \par % needed for pdftex
     \bgroup
     \dosetprofilepage
     \dogetprofilepage
     \def\processoneprofile##1##2%
       {\ExpandBothAfter\doifinsetelse{##2}{\processedprofiles}%
          {\doifsomething{##1}{(##1)}}%
          {\addtocommalist{##2}\processedprofiles
            ##1\relax
            \ifcase#1\relax
              \dobeginofprofile{##2}\paperwidth\paperheight\profilepage
            \else
              \doendofprofile
            \fi}}%
     \let\processedprofiles\empty
     \def\doprocessprofile##1%
       {\doifelse{\@@pfoption}{\v!test}%
          {\goodbreak\blank\nobreak\tt[\space
           \ifcase#1\v!start\else\v!stop\fi profiel\space ##1:\space
           \doifdefinedelse{\c!profiel!!##1}%
             {\def\dodoprocessprofile####1%
                {\processoneprofile
                   {\goto{####1}[\c!profiel!!####1]}%
                   {####1}%
                 \space}%
              \processcommacommand
                [\getvalue{\c!profiel!!##1}]\dodoprocessprofile}%
             {- }%
           ]\nobreak\blank}%
          {\doifdefined{\c!profiel!!##1}%
             {\def\dodoprocessprofile####1%
                {\processoneprofile{}{####1}}%
              \processcommacommand
                 [\getvalue{\c!profiel!!##1}]\dodoprocessprofile}}}%
     \processcommalist[#2]\doprocessprofile
     \egroup
     \par % needed for pdftex
   \fi}

\def\startprofile[#1]%
  {\iflocation
     \bgroup
     \addtocommalist{#1}\actualprofile
     \def\stopprofile%
       {\processprofile1[#1]%
        \egroup}%
     \def\next{\processprofile0[#1]}% % \DoAfterFi \processprofile0[#1]%
   \else                              % ^^^^^^^^^^ will be obsolete
     \let\next\relax                  % since ugly and never used
   \fi
   \next}

\let\stopprofile\relax

\def\dofollowprofile#1[#2]%
  {\iflocation
     \hbox
       {\dostartgoto
          \data
            {\dolocationattributes\??ia\c!style\c!color{#1\presetgoto}}%
          \start
            \dostartgotoprofile\buttonwidth\buttonheight{#2}%
          \stop
            \dostopgotoprofile
        \dostopgoto}%
   \else
     {#1}%
   \fi}

\def\followprofile#1[#2]%
  {\iflocation
     \doif\@@pfoption\v!test{\pagereference[\c!profiel!!#2]}%
     \dofollowprofile{#1}[#2]%
   \fi}

\def\setupprofiles%
  {\dodoubleargument\getparameters[\??pf]}

% Als er nog geen tekst op de pagina staat, dan heeft het
% profiel betrekking op het bovenstaande, dus soms een vorige
% pagina! Vreemd, omdat PDF paginagewijs werkt. Gelukkig
% biedt /page een oplossing. Echter: expansie van een
% \special kan niet worden uitgesteld, zodat alleen een
% two-pass een oplossing vormt. Het onderstaande kan komen
% te vervallen als Acrobat dit ondervangt. Het scheelt een
% pass en een lijst.
%
% Er kunnen eventueel twee lijsten worden gebruikt. Een voor
% het begin (start) en een voor het eind (stop). Nu staat
% alles in een lijst.

\definetwopasslist{\s!profile}

\newcounter\currentprofile

\def\dosetprofilepage%
  {\doglobal\increment\currentprofile
   \edef\docommando%
     {\writeutilitycommand%
        {\twopassentry%
           {\s!profile}%
           {\currentprofile}%
           {\noexpand\realfolio}}}%
   \docommando}

\def\dogetprofilepage%
  {\gettwopassdata{\s!profile}%
   \let\profilepage=\twopassdata}

\newcounter\versionlevel
\newcounter\versionorder

\newif\ifrecentversion

\let\oldatcharacter=@

\def\minimumversion{0}
\def\actualversion{0}

\def\dosetupversions[#1]%
  {\getparameters[\??ve][#1]
   \stripcharacter.\from\@@venumber\to\minimumversion
   \setversion}

\def\setupversions%
  {\dosingleargument\dosetupversions}

\definetwopasslist{\s!versionbegin}
\definetwopasslist{\s!versionend}

\def\actualprofile{}

\def\doresetpageversion%
  {\edef\docommando%
     {\writeutilitycommand%
        {\twopassentry%
           {\s!versionend}%
           {\versionorder}%
           {\noexpand\realfolio}}}%
   \docommando}

\def\dosetpageversion#1%
  {\recentversiontrue
   \doglobal\increment\versionorder\relax
   \edef\docommando%
     {\writeutilitycommand%
        {\twopassentry%
           {\s!versionbegin}%
           {\versionorder}%
           {\noexpand\realfolio}}}%
   \docommando
   \let\resetpageversion=\doresetpageversion}

\def\recentcontributions{}

\def\checkrecentcontributions%
  {\gettwopassdata{\s!versionbegin}%
   \iftwopassdatafound
     \!!counta\twopassdata\relax
     \gettwopassdata{\s!versionend}%
     \iftwopassdatafound
       \!!countb\twopassdata\relax
       \doglobal\increment\versionorder\relax
       \writeutilitycommand%
         {\twopassentry%
            {\s!versionbegin}%
            {\versionorder}%
            {\the\!!counta}}%
       \writeutilitycommand%
         {\twopassentry%
            {\s!versionend}%
            {\versionorder}%
            {\the\!!countb}}%
       \for\teller=\!!counta\to\!!countb\step1\do%
         {\@EA\doglobal\@EA\addtocommalist\@EA{\teller}{\recentcontributions}}%
       \let\next\checkrecentcontributions
     \else
       \let\next\relax
     \fi
   \else
     \let\next\relax
   \fi
   \next}

\def\docheckpageversion%
  {\ExpandBothAfter\doifinsetelse{\realfolio}{\recentcontributions}
     {\geselecteerdtrue}%
     {\geselecteerdfalse}}

\let\setpageversion   = \gobbleoneargument
\let\resetpageversion = \relax
\let\checkpageversion = \relax

\def\complexstartversion[#1]%
  {\bgroup
   \doifelsenothing\actualprofile
     {\startprofile[#1]}%
     {\startprofile[#1,\actualprofile]}%
   \def\docomplexstartversie##1%
     {\stripcharacter.\from##1\to\actualversion
      \ifnum\versionlevel>\zerocount\relax
        \ifnum\actualversion=\zerocount
          \setpageversion\actualversion   % unknown version
        \else
          \ifnum\actualversion<\minimumversion\relax
            \relax                        % old version
          \else
            \setpageversion\actualversion % new version
          \fi
        \fi
      \fi}%
   \doglobal\increment\versionlevel\relax
   \doifelsenothing{#1}
     {\docomplexstartversie{0}}%
     {\processcommalist[#1]\docomplexstartversie}}

\definecomplexorsimpleempty\startversion

\def\stopversion%
  {\stopprofile
   \doglobal\decrement\versionlevel
   \ifnum\versionlevel<\zerocount
     \showmessage\m!versions1\empty
   \else
     \resetpageversion
     \egroup
   \fi}

\bgroup
\catcode`@=\active
\gdef\setversion%
  {\catcode`@=\active        % we can't use \@@active here
   \long\def@##1##2 %
     {\ifx##1+%
        \startversion[##2]%
      \else\ifx##1-%
        \stopversion
      \else
        \oldatcharacter##1##2 %
      \fi\fi}}
\egroup

\def\markversion
  {\showmessage\m!versions2\empty
   \let\setpageversion\dosetpageversion
   \let\resetpageversion\relax
   \let\checkpageversion\relax}

\def\selectversion
  {\checkrecentcontributions
   \showmessage\m!versions3\recentcontributions
   \let\setpageversio\gobbleoneargument
   \let\resetpageversion\relax
   \let\checkpageversion\docheckpageversion
   \setversion}

\def\dodefineversion[#1][#2]%
  {\setvalue{\c!versie!!#1}{#2}%
   \defineprofile[#1][#2]}

\def\defineversion
  {\dodoubleargument\dodefineversion}

\def\followversion
  {\followprofile}

\def\followprofileversion#1[#2][#3]%
  {\def\docommando##1%
     {\defineprofile[#2#3][##1]}%
   \processcommacommand[\getvalue{\c!versie!!#3}]\docommando
   \followprofile#1[#2#3]}

\newcounter\currentpagetransition

\newif\ifrandomtransitions

\def\setuppagetransitions%
  {\dosingleempty\dosetuppagetransitions}

\def\dosetuppagetransitions[#1]%
  {\doifelsenothing{#1}
     {\doifnot\@@scdelay\v!none
        {\let\setpagetransition\setsomepagedelay}}
     {\doifelse{#1}\v!start
        {\doifnot\@@scdelay\v!none
           {\let\setpagetransition\setsomepagedelay}}
        {\doglobal\newcounter\currentpagetransition
         \doifinsetelse{#1}{\v!reset,\v!stop}
           {\let\setpagetransition\relax}
           {\let\setpagetransition\setsomepagetransition
            \doifinsetelse\v!random{#1}
              {\randomtransitionstrue}{\randomtransitionsfalse}%
            \edef\userpagetransitions{#1}%
            \@EA\removefromcommalist\@EA{\v!random}\userpagetransitions
            \ifx\userpagetransitions\empty
              \let\userpagetransitions\pagetransitions
            \fi}}}}

\def\setsomepagedelay
  {\expanded{\dosetpagetransition{0}{\@@scdelay}}}

\def\setsomepagetransition
  {\iflocation
     \ifrandomtransitions
       \expanded{\getcommalistsize[\userpagetransitions]}%
       \getrandomnumber\currentpagetransition1\commalistsize
     \else
       \doglobal\increment\currentpagetransition
     \fi
     \expanded{\getfromcommalist[\userpagetransitions][\currentpagetransition]}%
     \doifnumberelse\commalistelement
       {\expanded{\getfromcommalist[\pagetransitions][\commalistelement]}}
       {}%
     \ifx\commalistelement\empty
       \doglobal\newcounter\currentpagetransition
       \setsomepagetransition
     \else
       \doifelse\@@scdelay\v!none
         {\expanded{\dosetpagetransition{\commalistelement}{0}}}
         {\expanded{\dosetpagetransition{\commalistelement}{\@@scdelay}}}%
     \fi
   \fi}

\prependtoks \setpagetransition \to \everyshipout

% temporary here

%D \startbuffer
%D \dorecurse{10}
%D   {\horizontalpositionbar
%D      \pos\recurselevel \min1 \max10
%D      \token\framed{\recurselevel}%
%D    \\}
%D
%D \hbox to 15em
%D   {\hss
%D    \dorecurse{10}
%D      {\verticalpositionbar\pos\recurselevel\min1\max10\token\blackrule\\
%D       \hss}}
%D \stopbuffer

\def\horizontalpositionbar\pos#1\min#2\max#3\token#4\\%
  {\hbox to \hsize
     {\hskip\zeropoint\!!plus #1\!!fill
      \hskip\zeropoint\!!plus-#2\!!fill
      #4\relax
      \hskip\zeropoint\!!plus #3\!!fill
      \hskip\zeropoint\!!plus-#1\!!fill}}

\def\verticalpositionbar\pos#1\min#2\max#3\token#4\\%
  {\vbox to \vsize
     {\vskip\zeropoint\!!plus #1\!!fill
      \vskip\zeropoint\!!plus-#2\!!fill
      \hbox{#4}\relax
      \vskip\zeropoint\!!plus #3\!!fill
      \vskip\zeropoint\!!plus-#1\!!fill}}

\def\horizontalgrowingbar\pos#1\min#2\max#3\height#4\depth#5\\%
  {\hbox to \hsize
     {\scratchcounter#1%
      \advance\scratchcounter -#2%
      \advance\scratchcounter \plusone
      \leaders\vrule\hskip\zeropoint\!!plus \scratchcounter\!!fill
      \vrule\!!width\zeropoint\!!height#4\!!depth#5%
      \hskip\zeropoint\!!plus #3\!!fill
      \hskip\zeropoint\!!plus-#1\!!fill}}

\def\verticalgrowingbar\pos#1\min#2\max#3\width#4\\%
  {\vbox to \vsize
     {\scratchcounter#1%
      \advance\scratchcounter -#2%
      \advance\scratchcounter \plusone
      \leaders\hrule\vskip\zeropoint\!!plus\scratchcounter\!!fill
      \hrule\!!width#4\!!height\zeropoint\!!depth\zeropoint
      \vskip\zeropoint\!!plus #3\!!fill
      \vskip\zeropoint\!!plus-#1\!!fill}}

\newbox\commentbox

\def\doflushcommentanchors
   {\let\next\relax % new
    \processaction
      [\@@cclocation]
      [%      \v!text=>\let\next\relax, % new
            \v!inmargin=>\let\next\inmargin, % brr not the same as inleft|rightmargin
         \v!leftedge=>\let\next\inleftedge,
        \v!rightedge=>\let\next\inrightedge,
        \v!leftmargin=>\let\next\inleftmargin,
       \v!rightmargin=>\let\next\inrightmargin]%
    \next{\hbox{\raise\strutht\box\commentbox}}}

\def\flushcommentanchors % in everypar so indirect
  {\ifvoid\commentbox\else \doflushcommentanchors \fi}

\def\setupcomment
  {\dodoubleargument\getparameters[\??cc]}

\setvalue{\e!start\v!comment}% the dummy triple gobbles trailing spaces
  {\dotripleempty\dostartcommentaar}

\def\comment
  {\dodoubleempty\docomment}

\def\dodocomment#1%
  {\!!widtha\@@ccwidth
   \!!heighta\@@ccheight
   \doifelse\@@ccoption\v!max
     {\let\@@ccopen   \!!plusone}{\let\@@ccopen   \!!zerocount}%
   \doifelse\@@ccoption\v!buffer
     {\let\@@cccollect\!!plusone}{\let\@@cccollect\!!zerocount}%
   \preparecommentvariables
   \doinsertcomment
     \@@cctitle\!!widtha\!!heighta
     \@@cccolor\@@ccopen\@@ccsymbol
     \@@cccollect{#1}}

\def\preparecommentvariables % more will move here as with fields
  {\let\@@DriverCommentLayer\@@cctextlayer}

\def\dopreparecommentaar#1#2%
  {\doifassignmentelse{#1}
     {\getparameters[\??cc][#1]}
     {\getparameters[\??cc][\c!title=#1,#2]}%
   \obeylines
   \doif\@@ccspace\v!yes\obeyspaces}

\def\dostartcommentaar[#1][#2][#3]%
  {\bgroup
   \doifelse\@@ccstate\v!start
     {\dopreparecommentaar{#1}{#2}%
      \long\def\docommando##1%
        {\global\setbox\commentbox\frozenhbox
           {\hbox to \zeropoint
              {\struttedbox{\tbox{\dodocomment{##1}}}\hss}%
            \hskip\ifvoid\commentbox\@@ccmargin\else\@@ccdistance\fi
            \box\commentbox}%
         \egroup}}%
     {\long\def\docommando##1%
        {\egroup}}%
   \grabuntil{\e!stop\v!comment}\docommando}

\letvalue{\e!stop\v!comment}\relax % handy for \expanded{...}

\def\docomment[#1][#2]#3%
  {\doif\@@ccstate\v!start
     {\hbox to \zeropoint
        {\dopreparecommentaar{#1}{#2}%
         \hskip-\@@ccmargin
         \struttedbox{\tbox{\dodocomment{#3}}\hss}}}%
   \ignorespaces}

% \startcomment
%   hello beautiful\\world
% \stopcomment
%
% \startcomment[hello]
%   hello << \'e\'erste >>
%   beautiful
%   world
% \stopcomment
%
% \startcomment[hello][color=green,width=4cm,height=3cm]
%   hello \leftguillemot\ \'e\'erste \rightguillemot\
%   beautiful
%   world
% \stopcommentaar
%
% \startcomment[hello][color=green,width=4cm,height=3cm]
%   hello \leftguillemot\ \'e\'erste \rightguillemot\ test
%
%   beautiful
%
%   world
% \stopcomment
%
% \startcomment[symbol=Balloon]
%   Do we want this kind of rubish? And, why isn't this and
%   some more features related to text annotations so poorly
%   (actually not) documented? Anyhow, by providing this
%   functionality we demonstrate that \pdfTeX\ can do it. By
%   the way, it's funny that when in Acrobat we scale up the
%   text, the symbols scale down.
% \stopcomment

% \definesymbol [comment-normal][{\externalfigure[cow.pdf]}]
% \definesymbol [comment-down]  [{\externalfigure[cow.pdf]}]
%
% \def\CowSymbol#1#2%
%  {\scale
%     [\c!height=#1]
%     {\startMPcode
%        loadfigure "koe.mp" number 1 ;
%        refill currentpicture withcolor #2 ;
%      \stopMPcode}}
%
% \definesymbol [comment-normal]
%   [\CowSymbol{4ex}{red}]
%
% \definesymbol [comment-down]
%   [\CowSymbol{4ex}{green}]
%
% \setupcomment
%   [\c!symbol={comment-normal,comment-down},
%    \c!option=\v!buffer]
%
% \setupfootertexts[\placecomments]

\def\placecomments
  {\doflushcomments}

% \setupinteraction[state=start]
%
% \useattachment[test.tex]
% \useattachment[whatever][test.tex]
% \useattachment[whatever][newname][test.tex]
% \useattachment[whatever][title][newname][test.tex]
%
% % \setupattachments[\c!symbool={symbol-normal,symbol-down}]
%
% \starttext \attachment[whatever] \stoptext

\definesystemvariable{at}

\def\useattachment
  {\doquadrupleempty\douseattachment}

\def\douseattachment[#1][#2][#3][#4]% tag title newname filename
  {\iffourthargument
     \setgvalue{\??at:#1}{{#2}{#3}{#4}}% tooltip kind of case
   \else\ifthirdargument
     \setgvalue{\??at:#1}{{#2}{#2}{#3}}% full path case
   \else\ifsecondargument
     \setgvalue{\??at:#1}{{#2}{#2}{#2}}% obvious case
   \else
     \setgvalue{\??at:#1}{{#1}{#1}{#1}}% worst case
   \fi\fi\fi}

\let\attachmenttitle\empty
\let\attachmentname \empty
\let\attachmentfile \empty

\def\getattachmentdata[#1]%
  {\edef\attachmenttitle{\filterfromvalue{\??at:#1}31}% description
   \edef\attachmentname {\filterfromvalue{\??at:#1}32}% new name
   \edef\attachmentfile {\filterfromvalue{\??at:#1}33}% original
   \expandafter\splitstring\attachmentname\at.\to\!!stringa\and\!!stringb
   \ifx\!!stringb\empty % no suffix, so we need to inherit it
     \expandafter\splitstring\attachmentfile\at.\to\!!stringc\and\!!stringd
     \edef\attachmentname{\attachmentname.\!!stringd}%
   \fi}

\def\attachment
  {\dodoubleempty\doattachment}

\def\doattachment[#1][#2]% currently title equals newname
  {\iflocation
     \ifsecondargument
       \doifundefined{\??at:#2}
         {\showmessage\m!interactions6{#2}%
          \useattachment[#2]}%
       \doif\@@atstatus\v!start
         {\bgroup
          \getattachmentdata[#2]%
          \doiffileelse\attachmentfile
            {\setupattachments[#1]%
             \presetattachmentvariables
\struttedbox{\tbox{%
             \doattachfile
               \attachmenttitle
               {1em}\strutheight\strutdepth\@@atkleur\@@atsymbool
               \attachmentname
               \attachmentfile}%
}}%
            {\showmessage\m!interactions5\attachmentfile}%
          \egroup}%
     \else\iffirstargument
        \attachment[][#1]%
     \fi\fi
   \fi}

\def\presetattachmentvariables
  {\let\@@DriverAttachmentLayer\@@attekstlaag}

\def\setupattachments
  {\dodoubleempty\getparameters[\??at]}

\setupattachments
  [\c!state=\v!start,
   \c!color=\@@iacolor,
   \c!textlayer=,
   \c!symbol=]

% jammer, tussen/midden had erin gemoeten; \c!commando toevoegen

\def\registermenucommand#1%
  {{\textonly\noindent#1\space}} % no math switching

\def\doregistermenubuttons[#1][#2]% [menu id] [register]
  {\bgroup
   \ifsecondargument
     \setupinteractionmenu
       [#1][\c!unknownreference=\v!yes,\c!samepage=\v!yes]%
     \def\docommando##1%
       {\registermenucommand{\menubutton[#1]{##1}[#2:##1]}}%
   \else
     \def\docommando##1%
       {\registermenucommand
          {\button
             [\c!unknownreference=\v!yes,\c!samepage=\v!yes]
             {##1}[#1:##1]}}%
   \fi
   \handletokens abcdefghijklmnopqrstuvwxyz\with\docommando % moet anders
   \egroup}

\def\registermenubuttons
  {\dodoubleempty\doregistermenubuttons}

\stelkoppelingenin
  [\c!distance=.25em,
   \c!width=\v!fit,
   \c!location=\v!low,
   \c!color=\@@iacolor,
   \c!frame=\v!off,
   \c!background=,
   \c!backgroundscreen=\@@rsscreen,
   \c!backgroundcolor=]

\defineinteractionmenu
  [\v!right]
  [\v!right]
  [\c!before=,
   \c!after=\vfil,
   \c!inbetween=\blank,
   \c!distance=\bodyfontsize, % 12pt
   \c!left=\hss,
   \c!right=\hss,
   \c!width=\rightedgewidth,
   \c!height=\v!broad]

\defineinteractionmenu
  [\v!left]
  [\v!left]
  [\c!before=,
   \c!after=\vfil,
   \c!inbetween=\blank,
   \c!distance=\bodyfontsize, % 12pt
   \c!left=\hss,
   \c!right=\hss,
   \c!width=\leftedgewidth,
   \c!height=\v!broad]

\defineinteractionmenu
  [\v!bottom]
  [\v!bottom]
  [\c!before=\vss,
   \c!after=\vss,
   \c!middle=\hfil,
   \c!distance=\bodyfontsize, % 12pt
   \c!width=\v!fit,
   \c!height=\v!broad]

\defineinteractionmenu
  [\v!top]
  [\v!top]
  [\c!before=\vss,
   \c!after=\vss,
   \c!middle=\hfil,
   \c!distance=\bodyfontsize, % 12pt
   \c!width=\v!fit,
   \c!height=\v!broad]

\setupinteractionmenu
  [\v!left,\v!right,\v!top,\v!bottom]
  [\c!offset=.25em,
   \c!position=\v!no,
   \c!frame=\v!on,
   \c!background=,
   \c!backgroundcolor=,
   \c!backgroundscreen=\@@rsscreen,
   \c!style=\@@iastyle,
   \c!color=\@@iacolor,
   \c!contrastcolor=\@@iacontrastcolor,
   \c!state=\v!start,
   \c!samepage=\v!yes,
   \c!unknownreference=\v!empty,
   \c!topoffset=\!!zeropoint,
   \c!bottomoffset=\!!zeropoint,
   \c!leftoffset=\!!zeropoint,
   \c!rightoffset=\!!zeropoint]

\def\placeleftedgetextblock % Is \hss/\hsize really needed here?
  {\hbox to \leftedgewidth % (check outer level and settings)
     {\hsize\leftedgewidth
      \hss
      \interactionmenus[\v!left]}}

\def\placerightedgetextblock % Is \hss/\hsize really needed here?
  {\hbox to \rightedgewidth % (check outer level and settings)
     {\hsize\rightedgewidth
      \interactionmenus[\v!right]%
      \hss}}

\def\placetoptextblock
  {\vbox to \topheight
     {\vsize\topheight
%      \getvalue{\??tk\v!boven\v!tekst\c!voor}
      \getvalue{\??tk\v!top\c!before}
      \interactionmenus[\v!top]
%      \getvalue{\??tk\v!boven\v!tekst\c!na}
      \getvalue{\??tk\v!top\c!after}
      \kern\zeropoint}}

\def\placebottomtextblock
  {\vbox to \bottomheight
     {\vsize\bottomheight
%      \getvalue{\??tk\v!onder\v!tekst\c!voor}
      \getvalue{\??tk\v!bottom\c!before}
      \interactionmenus[\v!bottom]
%      \getvalue{\??tk\v!onder\v!tekst\c!na}
      \getvalue{\??tk\v!bottom\c!after}
      \kern\zeropoint}}

\ifx\leftedgetextcontent\undefined \else

  \appendtoks \placeleftedgetextblock  \hskip-\leftedgewidth  \to \leftedgetextcontent
  \appendtoks \placerightedgetextblock \hskip-\rightedgewidth \to \rightedgetextcontent
  \appendtoks \placetoptextblock       \vskip-\topheight      \to \toptextcontent
  \appendtoks \placebottomtextblock    \vskip-\bottomheight   \to \bottomtextcontent

\fi

\setupinteractionscreen
  [\c!width=\printpaperwidth,
   \c!height=\printpaperheight,
   \c!horoffset=\!!zeropoint,
   \c!veroffset=\!!zeropoint,
   \c!backspace=\backspace,
   \c!topspace=\topspace,
   \c!option=\v!min,
   \c!delay=\v!none]

\setupbuttons
  [\c!state=\v!start,
   \c!width=\v!fit,
   \c!height=\v!broad,
   \c!offset=0.25em,
   \c!frame=\v!on,
   \c!background=,
   \c!backgroundscreen=\@@rsscreen,
   \c!backgroundcolor=,
   \c!style=\@@iastyle,
   \c!color=\@@iacolor,
   \c!contrastcolor=\@@iacontrastcolor,
   \c!samepage=\v!yes,
   \c!unknownreference=\v!yes]

\setupinteractionbar
  [\c!state=\v!start,
   \c!alternative=a,
   \c!symbol=\v!no,
   \c!width=\rightedgewidth,
   \c!height=, % these are taken care
   \c!depth=, % of at calling time
   \c!distance=.5em, % beter relateren aan breedte
   \c!step=1,
   \c!color=\@@iacolor,
   \c!contrastcolor=\@@iacontrastcolor,
   \c!frame=\v!on,
   \c!background=,
   \c!backgroundscreen=\@@rsscreen,
   \c!backgroundcolor=,
   \c!samepage=\v!yes,
   \c!unknownreference=\v!yes]

\setupsynchronizationbar
  [\c!alternative=\v!page,
   \c!width=\rightedgewidth,
   \c!style=\@@iastyle,
   \c!color=\@@iacolor,
   \c!background=,
   \c!backgroundscreen=\@@rsscreen,
   \c!backgroundcolor=]

\setupsynchronization
  [\c!state=\v!stop]

\setupprofiles
  [\c!option=]

\setuppagetransitions
  [\v!reset]

\setupcomment
  [\c!state=\v!start,
   \c!margin=2.5em,
   \c!distance=1em,
   \c!width=.3\textwidth,
   \c!height=.2\textheight,
   \c!color=\@@iacolor,
   \c!title=,
   \c!space=\v!no,
   \c!symbol=\v!normal,
   \c!location=\v!inmargin,
   \c!option=,
   \c!textlayer=]

\setupversions    % beware, @ is made active here,
  [\c!number=1,   % therefore we set this one at the end
   \c!style=\ss,
   \c!color=]

\protect \endinput
