%D \module
%D   [       file=core-int,
%D        version=1995.1.1,
%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.

%D Still to be done properly.

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

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

\startmessages  english  library: interactions
  title: interaction
      1: aspect ratio -- x -- (b x h)
      2: active
      3: inactive
      4: no pagesynchronisation (--) in hmode
\stopmessages

\startmessages  german  library: interactions
  title: Interaktion
      1: Aspekt des Verhaeltnis -- x -- (B x H)
      2: aktiv
      3: inaktiv
      4: keine Seitensynchronisation (--) im hmode
\stopmessages

\startmessages  czech  library: interactions
  title: interakce
      1: pomer -- x -- (s x v)
      2: aktivni
      3: neaktivni
      4: zadna strankova synchronizace (--) v hmode
\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
\stopmessages

\startmessages  norwegian  library: interactions
  title: interaksjon
      1: forholdstall -- x -- (b x h)
      2: aktiv
      3: inaktiv
      4: ingen sidesynkronisering (--) i hmode
\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
\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!breedte=\!!zeropoint]%
        \setinteractionparameter\c!breedte\!!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!breedte=\!!zeropoint]%
        \setinteractionparameter\c!breedte\!!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\berekeninteractiescherm
  {\doifelse\@@scbreedte\v!passend
     {\!!widtha\linkerrandbreedte
      \advance\!!widtha \linkerrandafstand
      \advance\!!widtha \linkermargebreedte
      \advance\!!widtha \linkermargeafstand
      \ifdim\rugwit>\!!widtha\ifdim\rugwit>\zeropoint\relax
        \advance\rugwit -\!!widtha
      \fi\fi
      \advance\!!widtha \zetbreedte
      \advance\!!widtha \rechtermargeafstand
      \advance\!!widtha \rechtermargebreedte
      \advance\!!widtha \rechterrandafstand
      \advance\!!widtha \rechterrandbreedte
      \scratchdimen\@@scrugwit
      \advance\scratchdimen \@@scrugoffset
      \advance\!!widtha 2\scratchdimen}
     {\doifelse\@@scbreedte\v!max
        {\!!widtha\printpapierbreedte}
        {\!!widtha\@@scbreedte}}%
   \doifelse\@@schoogte\v!passend
     {\!!heighta\bovenhoogte
      \advance\!!heighta \bovenafstand
      \ifdim\kopwit>\!!heighta\ifdim\kopwit>\zeropoint\relax
        \advance\kopwit -\!!heighta
      \fi\fi
      \advance\!!heighta \zethoogte
      \advance\!!heighta \onderafstand
      \advance\!!heighta \onderhoogte
      \scratchdimen\@@sckopwit
      \advance\scratchdimen \@@sckopoffset
      \advance\!!heighta 2\scratchdimen}
     {\doifelse\@@schoogte\v!max
        {\!!heighta\printpapierhoogte}
        {\!!heighta\@@schoogte}}%
   \doif\@@scwachttijd\v!geen{\let\@@scwachttijd\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\@@iatrefwoord
   \let\@@iatrefwoord\empty
   \def\doprepareidentity##1{\edef\@@iatrefwoord
     {\@@iatrefwoord\ifx\@@iatrefwoord\empty\else\space\fi##1}}%
   \@EA\processcommacommand\@EA[\!!stringa]\doprepareidentity
   \global\let\doprepareidentity\relax}

\def\doinitializeidentity#1%
  {%\iflocation
     \doprepareidentity
     \expanded
       {\dosetupidentity
          {\@@iatitel}{\@@iasubtitel}{\@@iaauteur}{#1}%
          {\@@iadatum}{\@@iatrefwoord}}%
     \global\let\initializeidentity\relax
  }%\fi}

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

\appendtoks \initializeidentity \to \everyshipout

\def\initializepaper
  {\bgroup
   \ifx\@@pplinks \empty
   \ifx\@@pprechts\empty
   \ifx\@@ppboven \empty
   \ifx\@@pponder \empty
   \ifx\@@pcstatus\v!start
     \locationfalse\fi\else
     \locationfalse\fi\else
     \locationfalse\fi\else
     \locationfalse\fi\else
     \locationfalse\fi
   \iflocation % without screen settings
     \egroup
     \dosetuppaper\papersize\papierbreedte\papierhoogte
   \else
     \egroup
     \dosetuppaper\printpapersize\printpapierbreedte\printpapierhoogte
   \fi}

\appendtoks \initializepaper \to \everyshipout

\def\doinitializepaper
  {\bgroup
   \berekeninteractiescherm
   \ifdim\!!widtha>\papierbreedte\ifdim\!!widtha>\zeropoint
     \papierbreedte\!!widtha
   \fi\fi
   \ifdim\!!heighta>\papierhoogte\ifdim\!!heighta>\zeropoint
     \papierhoogte\!!heighta
   \fi\fi
   \dosetuppaper
     {\printpapersize}
     {\the\papierbreedte}
     {\the\papierhoogte}%
   \egroup}

\let\@@pcscreendata\empty

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

\setvalue{\??sc\c!optie\v!max         }{1} % tzt share with driver
\setvalue{\??sc\c!optie\v!bookmark    }{2} % tzt share with driver
\setvalue{\??sc\c!optie\v!passend     }{3} % tzt share with driver
\setvalue{\??sc\c!optie\v!dubbelzijdig}{4} % tzt share with driver

\def\dodosetupinteractionscreens % met a, b en \number
  {\bgroup
   \berekeninteractiescherm
   \!!counte=0\getvalue{\??sc\c!optie\@@scoptie}\relax
   % niet waterdicht
   \doifnot{\the\!!widtha\the\!!heighta}\@@pcscreendata
     {\xdef\@@pcscreendata{\the\!!widtha\the\!!heighta}%
      \showmessage\m!interactions
        1{\@EA\withoutpt\the\!!widtha,\@EA\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\dostelinteractieschermin[#1]%
  {\getparameters[\??sc][#1]%
   \ifproductionrun
     \let\initializepaper\doinitializepaper
     \let\setupinteractionscreens\dosetupinteractionscreens
   \fi}

\appendtoks \setupinteractionscreens \to \everyshipout

\def\stelinteractieschermin
  {\dosingleempty\dostelinteractieschermin}

%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::}

\definieerlijst[\@@bookmark]

\appendtoks\flushpostponedbookmark\to\everypar
\appendtoks\flushpostponedbookmark\to\neverypar

\newtoks\postponedbookmarks

%\let\flushpostponedbookmark\relax

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

\def\simplebookmark#1%
  {\ifx\flushpostponedbookmark\relax \else
     \bgroup
     \convertargument#1\to\ascii
     \writestatus{system}{clashing bookmarks: \ascii}% ECHTE MESSAGE MAKEN
     \egroup
   \fi
   \doglobal\prependtoks
     \schrijfnaarlijst[\@@bookmark]{}{#1}%
   \to\postponedbookmarks}

\def\complexbookmark[#1]#2%
  {\doglobal\appendtoks\schrijfnaarlijst[#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
       \dolijstelement{}{}{}{}{}{}% needed to finish the first pass
       \doprocessbookmarks[#1]\doputbookmarkelement
       \flushbookmark
       \egroup
     \else
       \expanded{\placebookmarks\@EA[\getvalue{\??ih\v!inhoud\c!lijst}]}%
     \fi
   \fi}

\def\doprocessbookmarks[#1]#2%
  {\let\dolijstelement#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{#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}}

% \startinteractiemenu[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   \\
% \stopinteractiemenu

\newif\iflocationmenupermitted

\def\testinteractiemenu#1%
   {\iflocation
      \doifelse\@@iamenu\v!aan
        {\doifelsevalue{\??am#1\c!status}\v!start
           {\global\locationmenupermittedtrue}
           {\global\locationmenupermittedfalse}}
        {\global\locationmenupermittedfalse}%
    \else
      \global\locationmenupermittedfalse
    \fi}

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

\def\blokkeerinteractiemenu
  {\dotripleempty\doblokkeerinteractiemenu[\addtocommalist]}

\def\geefinteractiemenuvrij
  {\dotripleempty\doblokkeerinteractiemenu[\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!letter\c!kleur{#3}}}%
   \iflocationclick
     \hbox{\gotolocation{#4}{\box\locationbox}}%
   \else
     \hbox{\box\locationbox}%
   \fi}

\let\dosetlocationboxyes\dosetlocationboxcontent

\def\dosetlocationboxempty#1[%
  {\dosetlocationboxcontent{#1}[\c!leeg=\v!ja,}

\def\dosetlocationboxno#1[%
  {\dosetlocationboxcontent{#1}[\c!leeg=\v!ja,\c!kader=,\c!achtergrond=,}

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

\def\setlocationboxyes#1%
  {\locationclicktrue
   \ifx\currentouterreference\empty
     \ifrealreferencepage\!!doneatrue\else\!!doneafalse\fi
   \else
     \!!doneafalse
   \fi
   \if!!donea
     \ifcase\csname\??am\??am\csname#1\c!zelfdepagina\endcsname\endcsname\relax
       \copycsname#1\c!kleur\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!onbekendeverwijzing\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!blokkade\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!letter\c!kleur{#3}}}

\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!ja=>\chardef\handleunknownmenuitem=0\relax,
   %   \v!leeg=>\chardef\handleunknownmenuitem=1\relax,
   %    \v!nee=>\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\naarbox{\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!kader=\v!uit,\c!achtergrond=]{\ignorespaces#2\unskip}[#1]\\}%

\def\menu@nop#1\\%
  {\@@amboxcommand\phantom{\localframed[\??am\currentmenu][]{}}\\}%

\def\menu@txt#1\\%
  {\@@amboxcommand\localframed[\??am\currentmenu][\c!kader=\v!uit,\c!achtergrond=]{\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}%
   \testinteractiemenu{#3}%
   \iflocationmenupermitted
     \bgroup
     \showcomposition
     \def\dolocationcommand##1%
       {\dosetlocationcommanditem{#3}{##1}}%
     \dimen0=\zetbreedte
     \advance\dimen0 \pagebackgroundhoffset
     \advance\dimen0 \pagebackgroundhoffset
     \advance\dimen0 -\getvalue{\??am#3\c!linkeroffset}%
     \advance\dimen0 -\getvalue{\??am#3\c!rechteroffset}%
     \setbox0\hbox to \dimen0
       {\forgetall
        \executeamboxcommands{#3}{#4}\c!links\c!midden\c!rechts}%
     \setbox0\hbox{\do@@ammenuposition{#3}{\box0}}%
     \wd0=\zetbreedte
     % geen \ht=#2 setting (yet)
     \hskip-\pagebackgroundhoffset
     \hskip \getvalue{\??am#3\c!linkeroffset}%
     \box0\relax
     \egroup
   \else
     #1\relax
   \fi}

\def\@@amvbox#1#2#3#4% don't change skipping, this one works!
  {\def\currentmenu{#3}%
   \testinteractiemenu{#3}%
   \iflocationmenupermitted
     \bgroup
     \showcomposition
     \dimen0=\teksthoogte
     \advance\dimen0 \pagebackgroundvoffset
     \advance\dimen0 \pagebackgroundvoffset
     \advance\dimen0 \pagebackgrounddepth
     \advance\dimen0 -\getvalue{\??am#3\c!bovenoffset}%
     \advance\dimen0 -\getvalue{\??am#3\c!onderoffset}%
     \setbox0\vbox to \dimen0
       {\forgetall                     % Voor't geval de afstand
       %\stelblankoin[\v!standaard]%   % (tijdelijk) is aangepast.
        \restorestandardblank
        \hsize#2\relax
        \executeamboxcommands{#3}{#4}\c!voor\c!tussen\c!na}%
     \setbox0\vbox{\hbox{\do@@ammenuposition{#3}{\box0}}}%
     \setbox0\vbox
       {\vskip-\pagebackgroundvoffset
        \vskip\getvalue{\??am#3\c!bovenoffset}%
        \ht0=\zeropoint
        \box0
        \vskip\pagebackgroundvoffset}% overbodig
     \ht0=\teksthoogte
     \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!rechts}%
  {\@@amvbox{\dodummypageskip\v!rechts}\rechterrandbreedte}

\setvalue{\??am\s!do\v!links}%
  {\@@amvbox{\dodummypageskip\v!links}\linkerrandbreedte}

\setvalue{\??am\s!do\v!boven}%
  {\@@amhbox{\dodummypageskip\v!boven}\bovenhoogte}

\setvalue{\??am\s!do\v!onder}%
  {\@@amhbox{\dodummypageskip\v!onder}\onderhoogte}

\def\dointeractiemenu#1#2%
  {\getvalue{\??am\s!do\getvalue{\??am#1\c!plaats}}{#1}{#2}}

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

\def\horizontaalinteractiemenu#1#2#3#4%
  {\ifdim#2>\zeropoint % new
     \dimen2\zeropoint
     \setbox0\hbox
       {\def\docommando##1%
          {\doifnotvalue{\??am##1\c!status}\v!geen
             {\hskip\dimen2
              \setbox2\hbox to #2
                {\getvalue{\??am##1#3}\interactiemenu[##1]\getvalue{\??am##1#4}}%
              \doifelsevalue{\??am##1\c!afstand}\v!overlay
                {\dimen2\zeropoint
                 \wd2\zeropoint}%
                {\dimen2=\getvalue{\??am##1\c!afstand}}%
              \box2}}%
       \startinteractie
       \processcommacommand[\getvalue{\??am#1}]\docommando
       \stopinteractie}%
     \wd0=#2\relax
     \box0\relax
   \fi}

\def\vertikaalinteractiemenu#1#2#3#4%
  {\ifdim#2>\zeropoint % new
     \dimen2\zeropoint
     \setbox0\vbox
       {\def\docommando##1%
          {\doifnotvalue{\??am##1\c!status}\v!geen
             {\vskip\dimen2
              \setbox2\vbox to #2
                {\getvalue{\??am##1#3}\interactiemenu[##1]\getvalue{\??am##1#4}}%
              \doifelsevalue{\??am##1\c!afstand}\v!overlay
                {\dimen2\zeropoint
                 \offinterlineskip
                 \dp2\zeropoint
                 \ht2\zeropoint}%
                {\dimen2=\getvalue{\??am##1\c!afstand}}%
              \box2}}%
        \startinteractie
        \processcommacommand[\getvalue{\??am#1}]\docommando
        \stopinteractie}%
     \ht0=#2\relax
     \dp0\zeropoint
     \box0\relax
   \fi}

\letvalue{\??am\v!links }\empty
\letvalue{\??am\v!rechts}\empty
\letvalue{\??am\v!boven }\empty
\letvalue{\??am\v!onder }\empty

% todo : \defineinteractionmenuclass 

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

\setvalue{\??am\??am\c!menu\v!links}%
  {\horizontaalinteractiemenu\v!links\linkerrandbreedte\c!links\c!rechts}
\setvalue{\??am\??am\c!menu\v!rechts}%
  {\horizontaalinteractiemenu\v!rechts\rechterrandbreedte\c!links\c!rechts}
\setvalue{\??am\??am\c!menu\v!boven}%
  {\vertikaalinteractiemenu\v!boven\bovenhoogte\c!voor\c!na}
\setvalue{\??am\??am\c!menu\v!onder}%
  {\vertikaalinteractiemenu\v!onder\onderhoogte\c!voor\c!na}

% 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!vertikaal
     {\setvalue{\??am\??am\c!menu#1}%
        {\vertikaalinteractiemenu
           {#1}{\getvalue{\??am#1\c!breedte}}\c!voor\c!na}}
     {\setvalue{\??am\??am\c!menu#1}%
        {\horizontaalinteractiemenu
           {#1}{\getvalue{\??am#1\c!hoogte}}\c!links\c!rechts}}}

% \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[{\interactiemenu[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
   \testinteractiemenu{#3}%
   \iflocationmenupermitted
     \executeamboxcommands{#3}{#4}\c!voor\c!tussen\c!na
   \else
     #1\relax
   \fi
   \egroup}

\unprotected\def\includemenu[#1]%
  {\doifvalue{\??am#1\c!status}\v!lokaal
     {\bgroup
      \letvalue{\??am#1\c!status}\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
%
% \stelinteractiemenuin[right][{abc[xyz]},...]
% \stelinteractiemenuin[right][key=val,...]

\newif\ifextendedmenu

\def\definieerinteractiemenu
  {\dotripleempty\dodefinieerinteractiemenu}

\def\dodefinieerinteractiemenu[#1][#2]% compatibility hack
  {\convertargument#2\to\ascii        % will disappear soon 
   \doifinstringelse[\ascii
     \dodostelinteractielijstmenuinx
     \dododefinieerinteractiemenu   
   [#1][#2]}

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

\def\dododefinieerinteractiemenu[#1][#2][#3]%
  {% main settings
   \letvalue{\??am\c!menu#1}\empty
   % \setvalue{\??am\c!menu#1}%
   %   {\extendedmenufalse\dointeractiemenu{#1}{}}%
   \setvalue{\@@dodolijstelement#1}%
     {\def\dosomelijstelement{\dodomenulijstelement{#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!links,\c!midden,\c!rechts,\c!voor,\c!na,\c!tussen,
         \c!breedte,\c!hoogte,\c!afstand,\c!offset,\c!kader,
         \c!achtergrond,\c!achtergrondkleur,\c!achtergrondraster,
         \c!letter,\c!kleur,\c!contrastkleur,\c!zelfdepagina,\c!onbekendeverwijzing,
         \c!linkeroffset,\c!rechteroffset,\c!bovenoffset,\c!onderoffset]}%
   % additional settings
   \getparameters[\??am#1][\c!plaats=#2,\c!blokkade=,#3]}

\def\stelinteractiemenuin
  {\dodoubleargument\dostelinteractiemenuin}

\def\dostelinteractiemenuin[#1][% compatibillity hack
  {\doifnextcharelse\bgroup     % will disappear soon 
     {\dodostelinteractielijstmenuiny[#1][}
     {\dodostelinteractiemenuin      [#1][}}

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

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

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

\setvalue{\??am\??am\v!ja  }{0}
\setvalue{\??am\??am\v!leeg}{1}
\setvalue{\??am\??am\v!nee }{2}
\setvalue{\??am\??am\v!geen}{3}
\setvalue{\??am\??am       }{1} % default

\processbetween{\e!interactiemenu}\dostartinteractiemenu

\def\dostartinteractiemenu#1%
  {\dodostartinteractiemenu#1\dodostopinteractiemenu}

\def\dodostartinteractiemenu[#1]#2\dodostopinteractiemenu
  {\setvalue{\??am\c!menu#1}{\extendedmenutrue\dointeractiemenu{#1}{#2}}}

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

\def\dodomenulijstelement#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!maxbreedte}}{\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 \pagina

\def\simplescherm%  zou niet nodig moeten zijn
  {\iflocation
     \pagina[\v!ja]%
   \fi}

\def\complexscherm[#1]%
  {\iflocation
     \pagina[#1]%
   \fi}

\definecomplexorsimple\scherm

\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!letter\c!kleur{##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}

%\definieermarkering[\syncmarker]
%\stelmarkeringin[\syncmarker][\c!expansie=\v!ja]

\newmark\syncmarker

\newcounter\synccounter

\newif\ifsynchronisation

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

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

\def\synchroniseer%
  {\startsynchronisatie
   \stopsynchronisatie}

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

\def\stelsynchronisatiein
  {\dosingleargument\dostelsynchronisatiein}

\def\definieersynchronisatie
  {\dosingleargument\dodefinieersynchronisatie}

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

\presetlocalframed[\??ba]

\setvalue{synchronisatie\v!pagina}[#1]%
  {\bgroup
  %\setupinteraction[\c!breedte=\!!zeropoint]%
   \setinteractionparameter\c!breedte\!!zeropoint
   \setbox0=\hbox
     {\localframed[\??ba][]%
        {\dolocationattributes\??ba\c!letter\c!kleur{\strut\@@batekst}}}%
   \mindermeldingen
   \def\onder%
     {\leaders\hrule\!!depth1ex\!!height-.5ex\hfil}%
   \def\boven##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\@@bakleur]{\onder}}}}%
      \naarbox
        {\hbox to \dimen0{\color[\locationcolor\@@bakleur]{\onder}}}%
        [#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
             \boven\first30\relax
           \else
             \boven\first21\hss\boven\bot11\relax
           \fi
         \else
           \ifnum\first=\bot\relax
             \boven\top11\hss\boven\first21\relax
           \else
             \boven\top11\hss\boven\first11\hss\boven\bot11\relax
           \fi
         \fi}%
      \wd2=\zeropoint\box2
      \box0\relax}%
   \egroup}

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

\def\synchronisatiebalk[#1][#2]%
  {\iflocation\ifsynchronisation
     \bgroup
     \stelsynchronisatiebalkin
       [\c!tekst=\getvalue{doc:des:#1},#2]%
     \getvalue{synchronisatie\@@bavariant}[#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!letter\c!kleur{#2}}}}%
     \fi
   \else
     {#2}%
   \fi}

\def\interactiebalka
  {\iflocation
     \bgroup
    %\setupinteraction[\c!breedte=\!!zeropoint]%
     \setinteractionparameter\c!breedte\!!zeropoint
     \setupblackrules[\c!hoogte=\v!max,\c!diepte=\v!max]% maten ??
     \!!widthb\@@ibbreedte
     \advance\!!widthb -2.75em\relax
     \!!widtha\!!widthb
     \divide\!!widtha \lastpage\relax
     \bgroup
       \advance\realpageno \minusone
       \ifvoid\meterbox
         \bgroup
         \processaction
           [\@@ibstap]
           [   \v!klein=>\dimen0=.25em\relax,
              \v!middel=>\dimen0=.5em\relax,
               \v!groot=>\dimen0=1em\relax,
             \s!unknown=>\dimen0=\!!widtha]%
         \ifdim\!!widtha<\dimen0\relax
           \!!counta\dimen0\relax
           \!!countb\!!widtha
           \divide\!!counta \!!countb
         \else
           \!!counta\@@ibstap\relax
         \fi
         \!!widtha=\!!counta\!!widtha
         \setbox0\hbox{\blackrule[\c!breedte=\!!widtha]}%
         \global\setbox\meterbox\hbox to \!!widthb
           {\hss
            \for \teller=1 \to \lastpage \step \!!counta \do
              {\gotorealpage{}{}{\teller}{\copy0}}%
            \hss}%
         \global\wd\meterbox\zeropoint
         \egroup
       \fi
     \egroup
     \noindent
     \strut
     \hbox to \@@ibbreedte
       {\mindermeldingen
        \setupblackrules[\c!breedte=1em]%
        \doganaareenpagina\??ib\blackrule\firstpage
        \hss
        \color[middlegray]{\copy\meterbox}%
        \hbox to \!!widthb
          {\ifdim\!!widtha<1em\relax
             \!!widtha=1em\relax
           \fi
           \setupblackrules[\c!breedte=\!!widtha]%
           \ifnum\realpageno>1\relax
             \!!counta=\realpageno
             \advance\!!counta -2\relax
             \hskip\zeropoint\!!plus\!!counta \s!sp\relax % cm gives overflow
             \doganaareenpagina\??ib\blackrule\prevpage
           \fi
           \color[\@@ibcontrastkleur]{\blackrule[\c!breedte=.5em]}%
           \ifnum\realpageno<\lastpage\relax
             \doganaareenpagina\??ib\blackrule\nextpage
             \!!counta=\lastpage\relax
             \advance\!!counta -\realpageno
             \advance\!!counta \minusone
             \hskip\zeropoint\!!plus\!!counta \s!sp\relax % cm gives overflow
           \fi}%
        \hss
        \doganaareenpagina\??ib\blackrule\lastpage}%
     \egroup
   \fi}

\presetlocalframed[\??ib]

\def\interactiebalkc%
  {\iflocation
     \ifnum\lastpage>\plusone
       \hbox to \@@ibbreedte
         {\setupblackrules[\c!hoogte=\@@ibhoogte,\c!diepte=\@@ibdiepte]%
          \def\gotox##1%
            {\doganaareenpagina{}{\blackrule[\c!breedte=##1]}}%
          \dimen0=\@@ibbreedte\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\@@ibkleur]%
          \gotox{1em}\firstpage
          \hss
          \gotox\!!widtha\prevpage
          \color[\@@ibcontrastkleur]{\blackrule[\c!breedte=1em]}%
          \gotox\!!widthb\nextpage
          \hss
          \gotox{1em}\lastpage
          \stopcolor}%
     \fi
   \fi}

\def\interactiebalkd%
  {\iflocation\ifshowingsubpage
     \ifnum\nofsubpages>\plusone
       \hbox
       \bgroup
      %\setupinteraction[\c!breedte=\!!zeropoint]%
       \setinteractionparameter\c!breedte\!!zeropoint
       \ifbalksymbool % beter: 3 chars assign en 3*box
         \setupsymbolset[\@@iasymboolset]%
         \setbox0\hbox{\symbol[\v!vorige]}%
         \setbox2\hbox{\symbol[\v!ergens]}%
         \setbox4\hbox{\symbol[\v!volgende]}%
       \else
         \setbox0\hbox
           {\vrule
              \!!height\@@ibhoogte
              \!!depth\@@ibdiepte
              \!!width\@@ibbreedte}%
         \setbox2\copy0
         \setbox4\copy0
       \fi
       \startcolor[\locationcolor\@@ibkleur]%
       \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
              [\@@ibcontrastkleur]
              {\gotorealpage{}{}{\teller}{\copy2}}%
          \else
            \gotorealpage{}{}{\teller}{\copy4}\relax
          \fi\fi
          \egroup
          \hskip\@@ibafstand}%
       \unskip
       \stopcolor
       \egroup
     \fi
   \fi\fi}

\def\interactiebalke%  KAN WORDEN GECOMBINEERD MET D
  {\iflocation\ifshowingsubpage
     \ifnum\nofsubpages>\plusone
       \bgroup
       \!!widthb\@@ibafstand
       \multiply\!!widthb \nofsubpages
       \advance\!!widthb -\@@ibafstand % (n-1)
       \!!widtha\@@ibbreedte
       \advance\!!widtha -\!!widthb
       \divide\!!widtha \nofsubpages\relax
       \ifdim\!!widtha<\@@ibafstand\relax
         \interactiebalkf
       \else
        %\setupinteraction[\c!breedte=\!!zeropoint]%
         \setinteractionparameter\c!breedte\!!zeropoint
         \noindent
         \hbox to \@@ibbreedte
           \bgroup
             \ifbalksymbool
               \setupsymbolset[\@@iasymboolset]%
               \setbox0\hbox{\symbol[\v!vorige]}%
               \setbox2\hbox{\symbol[\v!ergens]}%
               \setbox4\hbox{\symbol[\v!volgende]}%
             \else
               \setbox0\hbox
                  {\vrule
                     \!!height\@@ibhoogte
                     \!!depth\@@ibdiepte
                     \!!width\!!widtha}%
               \setbox2\copy0
               \setbox4\copy0
             \fi
             \startcolor[\locationcolor\@@ibkleur]%
             \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
                    [\@@ibcontrastkleur]
                    {\gotorealpage{}{}{\teller}{\copy2}}%
                \else
                  \gotorealpage{}{}{\teller}{\copy4}\relax
                \fi\fi
                \egroup
                \hss}%
             \unskip
             \stopcolor
           \egroup
       \fi
       \egroup
     \fi
   \fi\fi}

\def\interactiebalkf%  !! KAN WORDEN GECOMBINEERD MET D !!
  {\iflocation\ifshowingsubpage
     \ifnum\nofsubpages>\plusone
      %\setupinteraction[\c!breedte=\!!zeropoint]%
       \setinteractionparameter\c!breedte\!!zeropoint
       \noindent
       \hbox to \@@ibbreedte
       \bgroup
       \!!countb\zerocount
       \loop
         \advance\!!countb \plusone
         \!!countc\nofsubpages
         \divide\!!countc \!!countb
         \advance\!!countc \plusone
         \!!widthb\@@ibafstand
         \multiply\!!widthb \!!countc
         \advance\!!widthb -\@@ibafstand
         \!!widtha\@@ibbreedte
         \advance\!!widtha -\!!widthb
         \divide\!!widtha \!!countc
         \ifdim\!!widtha<\@@ibafstand\relax
       \repeat
\advance\!!countc -2
\!!widtha-\@@ibafstand
\!!widtha=\!!countc\!!widtha
\advance\!!widtha \@@ibbreedte
\advance\!!countc \plusone
\divide\!!widtha \!!countc
       \ifbalksymbool
         \setupsymbolset[\@@iasymboolset]%
         \setbox0\hbox{\symbol[\v!vorige]}%
         \setbox4\hbox{\symbol[\v!ergens]}%
         \setbox8\hbox{\symbol[\v!volgende]}%
         \setbox2\copy4
         \setbox6\copy4
       \else
         \setbox0\hbox
           {\vrule
              \!!height\@@ibhoogte
              \!!depth\@@ibdiepte
              \!!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
              [\@@ibcontrastkleur]
              {\gotorealpage{}{}{\teller}{\copy##1}}%
          \else
            \gotorealpage{}{}{\teller}{\copy##1}%
          \fi
          \!!countf\zerocount
          \hss}%
       \startcolor[\locationcolor\@@ibkleur]%
       \!!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\interactiebalkb%
  {\ifnum\lastpage>\firstpage\relax
     \interactiebuttons
       [\v!eerstepagina,
        \v!vorigepagina,
        \v!volgendepagina,
        \v!laatstepagina]%
   \fi}

\def\interactiebalkg%
  {\ifnum\lastsubpage>\firstsubpage\relax
     \interactiebuttons
       [\v!eerstesubpagina,
        \v!vorigesubpagina,
        \v!volgendesubpagina,
        \v!laatstesubpagina]%
   \fi}

\def\checkinteractiebalk#1#2#3%
  {\ifdim\@@ibbreedte=\zeropoint\def\@@ibbreedte{#1}\fi
   \doifnothing\@@ibhoogte{\def\@@ibhoogte{#2}}%
   \doifnothing\@@ibdiepte{\def\@@ibdiepte{#3}}}

\def\complexinteractiebalk[#1]%
  {\doifelse{#1}\v!reset
    {\global\setbox\meterbox\box\voidb@x}%
    {\bgroup
       \iflocation
         \checksubpages % goes wrong / loads \numberofpages too
         \getparameters[\??ib][#1]%
         \doif\@@ibstatus\v!start
           {\startinteractie
            \processaction % breedte defaults !
              [\@@ibvariant]
              [         c=>\checkinteractiebalk{.5em}\v!max \v!max,
                        d=>\checkinteractiebalk{.5em}{.5em} \!!zeropoint,
                        e=>\checkinteractiebalk{.5em}{.5em} \!!zeropoint,
                        f=>\checkinteractiebalk{.5em}{.5em} \!!zeropoint,
               \s!default=>\checkinteractiebalk{10em}\v!ruim\!!zeropoint,
               \s!unknown=>\checkinteractiebalk{10em}\v!ruim\!!zeropoint]%
            \doifelse\@@ibsymbool\v!ja
              \balksymbooltrue\balksymboolfalse
            \getvalue{interactiebalk\@@ibvariant}%
            \stopinteractie}%
       \fi
     \egroup}}

\definecomplexorsimpleempty\interactiebalk

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

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

\def\dodefinieerprofiel[#1][#2]%
  {\iflocation
     \def\dododefinieerprofiel##1%
       {\def\dodododefinieerprofiel####1%
          {\doifdefinedelse{\c!profiel!!####1}%
             {\edef\!!stringa{\getvalue{\c!profiel!!####1}}%
              \setevalue{\c!profiel!!####1}{\!!stringa,##1}}%
             {\setevalue{\c!profiel!!####1}{##1}}}%
        \processcommalist[#2]\dodododefinieerprofiel}%
     \processcommalist[#1]\dododefinieerprofiel
   \fi}

\def\definieerprofiel%
  {\dodoubleargument\dodefinieerprofiel}

% 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}\papierbreedte\papierhoogte\profilepage
            \else
              \doendofprofile
            \fi}}%
     \let\processedprofiles\empty
     \def\doprocessprofile##1%
       {\doifelse{\@@pfoptie}{\v!test}%
          {\goodbreak\blanko\nobreak\tt[\spatie
           \ifcase#1\v!start\else\v!stop\fi profiel\spatie ##1:\spatie
           \doifdefinedelse{\c!profiel!!##1}%
             {\def\dodoprocessprofile####1%
                {\processoneprofile
                   {\naar{####1}[\c!profiel!!####1]}%
                   {####1}%
                 \spatie}%
              \processcommacommand
                [\getvalue{\c!profiel!!##1}]\dodoprocessprofile}%
             {- }%
           ]\nobreak\blanko}%
          {\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\startprofiel[#1]%
  {\iflocation
     \bgroup
     \addtocommalist{#1}\actualprofile
     \def\stopprofiel%
       {\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\stopprofiel\relax

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

\def\volgprofiel#1[#2]%
  {\iflocation
     \doif\@@pfoptie\v!test{\pagereference[\c!profiel!!#2]}%
     \dovolgprofiel{#1}[#2]%
   \fi}

\def\stelprofielenin%
  {\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\dostelversiesin[#1]%
  {\getparameters[\??ve][#1]
   \stripcharacter.\from\@@venummer\to\minimumversion
   \setversion}

\def\stelversiesin%
  {\dosingleargument\dostelversiesin}

\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\complexstartversie[#1]%
  {\bgroup
   \doifelsenothing\actualprofile
     {\startprofiel[#1]}%
     {\startprofiel[#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\startversie

\def\stopversie%
  {\stopprofiel
   \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+%
        \startversie[##2]%
      \else\ifx##1-%
        \stopversie
      \else
        \oldatcharacter##1##2 %
      \fi\fi}}
\egroup

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

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

\def\dodefinieerversie[#1][#2]%
  {\setvalue{\c!versie!!#1}{#2}%
   \definieerprofiel[#1][#2]}

\def\definieerversie
  {\dodoubleargument\dodefinieerversie}

\def\volgversie
  {\volgprofiel}

\def\volgprofielversie#1[#2][#3]%
  {\def\docommando##1%
     {\definieerprofiel[#2#3][##1]}%
   \processcommacommand[\getvalue{\c!versie!!#3}]\docommando
   \volgprofiel#1[#2#3]}

\newcounter\currentpagetransition

\newif\ifrandomtransitions

\def\stelpaginaovergangenin%
  {\dosingleempty\dostelpaginaovergangenin}

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

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

\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\@@scwachttijd\v!geen
         {\expanded{\dosetpagetransition{\commalistelement}{0}}}
         {\expanded{\dosetpagetransition{\commalistelement}{\@@scwachttijd}}}%
     \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\blokje\\
%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
   {\inmarge{\hbox{\raise\ht\strutbox\box\commentbox}}}

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

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

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

\def\commentaar
  {\dodoubleempty\docommentaar}

\def\dodocommentaar#1%
  {\!!widtha\@@ccbreedte
   \!!heighta\@@cchoogte
   \doifelse\@@ccoptie\v!max
     {\def\@@ccopen{1}}{\def\@@ccopen{0}}%
   \doifelse\@@ccoptie\v!buffer
     {\def\@@cccollect{1}}{\def\@@cccollect{0}}%
   \doinsertcomment
     \@@cctitel\!!widtha\!!heighta
     \@@cckleur\@@ccopen\@@ccsymbool
     \@@cccollect{#1}}

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

\def\dostartcommentaar[#1][#2][#3]%
  {\bgroup
   \doifelse\@@ccstatus\v!start
     {\dopreparecommentaar{#1}{#2}%
      \long\def\docommando##1%
        {\global\setbox\commentbox\hbox
           {\dodocommentaar{##1}%
            \hskip\ifvoid\commentbox\@@ccmarge\else\@@ccafstand\fi
            \box\commentbox}%
         \egroup}}%
     {\long\def\docommando##1%
        {\egroup}}%
   \grabuntil{\e!stop\e!commentaar}\docommando}

\def\docommentaar[#1][#2]#3%
  {\doif\@@ccstatus\v!start
     {\hbox to \zeropoint
        {\dopreparecommentaar{#1}{#2}%
         \hskip-\@@ccmarge
         \raise\ht\strutbox\hbox{\dodocommentaar{#3}}}}%
   \ignorespaces}

% \startcommentaar
%   hello beautiful\\world
% \stopcommentaar
%
% \startcommentaar[hallo]
%   hello \<< \'e\'erste \>>
%   beautiful
%   world
% \stopcommentaar
%
% \startcommentaar[hallo][kleur=groen,breedte=4cm,hoogte=3cm]
%   hello \<< \'e\'erste \>>
%   beautiful
%   world
% \stopcommentaar
%
% \startcommentaar[hallo][kleur=groen,breedte=4cm,hoogte=3cm]
%   hello \<< \'e\'erste \>>
%
%   beautiful
%
%   world
% \stopcommentaar
%
% \startcommentaar[symbool=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.
% \stopcommentaar

% \definesymbol [comment-normal][{\externalfigure[koe.pdf]}]
% \definesymbol [comment-down]  [{\externalfigure[koe.pdf]}]
%
% \def\CowSymbol#1#2%
%  {\scale
%     [\c!hoogte=#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!symbool={comment-normal,comment-down},
%    \c!optie=\v!buffer]
%
% \setupfootertexts[\placecomments]

\def\placecomments
  {\doflushcomments}

% 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
     \stelinteractiemenuin
       [#1][\c!onbekendeverwijzing=\v!ja,\c!zelfdepagina=\v!ja]%
     \def\docommando##1%
       {\registermenucommand{\menubutton[#1]{##1}[#2:##1]}}%
   \else
     \def\docommando##1%
       {\registermenucommand
          {\button
             [\c!onbekendeverwijzing=\v!ja,\c!zelfdepagina=\v!ja]
             {##1}[#1:##1]}}%
   \fi
   \handletokens abcdefghijklmnopqrstuvwxyz\with\docommando % moet anders
   \egroup}

\def\registermenubuttons
  {\dodoubleempty\doregistermenubuttons}

\stelkoppelingenin
  [\c!afstand=.25em,
   \c!breedte=\v!passend,
   \c!plaats=\v!laag,
   \c!kleur=\@@iakleur,
   \c!kader=\v!uit,
   \c!achtergrond=,
   \c!achtergrondraster=\@@rsraster,
   \c!achtergrondkleur=]

\definieerinteractiemenu
  [\v!rechts]
  [\v!rechts]
  [\c!voor=,
   \c!na=\vfil,
   \c!tussen=\blanko,
   \c!afstand=\bodyfontsize, % 12pt
   \c!links=\hss,
   \c!rechts=\hss,
   \c!breedte=\rechterrandbreedte,
   \c!hoogte=\v!ruim]

\definieerinteractiemenu
  [\v!links]
  [\v!links]
  [\c!voor=,
   \c!na=\vfil,
   \c!tussen=\blanko,
   \c!afstand=\bodyfontsize, % 12pt
   \c!links=\hss,
   \c!rechts=\hss,
   \c!breedte=\linkerrandbreedte,
   \c!hoogte=\v!ruim]

\definieerinteractiemenu
  [\v!onder]
  [\v!onder]
  [\c!voor=\vss,
   \c!na=\vss,
   \c!midden=\hfil,
   \c!afstand=\bodyfontsize, % 12pt
   \c!breedte=\v!passend,
   \c!hoogte=\v!ruim]

\definieerinteractiemenu
  [\v!boven]
  [\v!boven]
  [\c!voor=\vss,
   \c!na=\vss,
   \c!midden=\hfil,
   \c!afstand=\bodyfontsize, % 12pt
   \c!breedte=\v!passend,
   \c!hoogte=\v!ruim]

\stelinteractiemenuin
  [\v!links,\v!rechts,\v!boven,\v!onder]
  [\c!offset=.25em,
   \c!positie=\v!nee,
   \c!kader=\v!aan,
   \c!achtergrond=,
   \c!achtergrondkleur=,
   \c!achtergrondraster=\@@rsraster,
   \c!letter=\@@ialetter,
   \c!kleur=\@@iakleur,
   \c!contrastkleur=\@@iacontrastkleur,
   \c!status=\v!start,
   \c!zelfdepagina=\v!ja,
   \c!onbekendeverwijzing=\v!leeg,
   \c!bovenoffset=\!!zeropoint,
   \c!onderoffset=\!!zeropoint,
   \c!linkeroffset=\!!zeropoint,
   \c!rechteroffset=\!!zeropoint]

%\def\plaatslinkerrandtekstblok {\interactiemenus[\v!links ]}
%\def\plaatsrechterrandtekstblok{\interactiemenus[\v!rechts]}
%\def\plaatsboventekstblok      {\interactiemenus[\v!boven ]}
%\def\plaatsondertekstblok      {\interactiemenus[\v!onder ]}

\def\plaatslinkerrandtekstblok % Is \hss/\hsize really needed here?
  {\hbox to \linkerrandbreedte % (check outer level and settings)
     {\hsize\linkerrandbreedte
      \hss
      \interactiemenus[\v!links]}}

\def\plaatsrechterrandtekstblok % Is \hss/\hsize really needed here?
  {\hbox to \rechterrandbreedte % (check outer level and settings)
     {\hsize\rechterrandbreedte
      \interactiemenus[\v!rechts]%
      \hss}}

\def\plaatsboventekstblok
  {\vbox to \bovenhoogte
     {\vsize\bovenhoogte
%      \getvalue{\??tk\v!boven\v!tekst\c!voor}
      \getvalue{\??tk\v!boven\c!voor}
      \interactiemenus[\v!boven]
%      \getvalue{\??tk\v!boven\v!tekst\c!na}
      \getvalue{\??tk\v!boven\c!na}
      \kern\zeropoint}}

\def\plaatsondertekstblok
  {\vbox to \onderhoogte
     {\vsize\onderhoogte
%      \getvalue{\??tk\v!onder\v!tekst\c!voor}
      \getvalue{\??tk\v!onder\c!voor}
      \interactiemenus[\v!onder]
%      \getvalue{\??tk\v!onder\v!tekst\c!na}
      \getvalue{\??tk\v!onder\c!na}
      \kern\zeropoint}}

\ifx\leftedgetextcontent\undefined \else

  \appendtoks \plaatslinkerrandtekstblok  \hskip-\linkerrandbreedte  \to \leftedgetextcontent
  \appendtoks \plaatsrechterrandtekstblok \hskip-\rechterrandbreedte \to \rightedgetextcontent
  \appendtoks \plaatsboventekstblok       \vskip-\bovenhoogte        \to \toptextcontent
  \appendtoks \plaatsondertekstblok       \vskip-\onderhoogte        \to \bottomtextcontent

\fi

\stelinteractieschermin
  [\c!breedte=\printpapierbreedte,
   \c!hoogte=\printpapierhoogte,
   \c!rugoffset=\!!zeropoint,
   \c!kopoffset=\!!zeropoint,
   \c!rugwit=\rugwit,
   \c!kopwit=\kopwit,
   \c!optie=\v!min,
   \c!wachttijd=\v!geen]

\stelbuttonsin
  [\c!status=\v!start,
   \c!breedte=\v!passend,
   \c!hoogte=\v!ruim,
   \c!offset=0.25em,
   \c!kader=\v!aan,
   \c!achtergrond=,
   \c!achtergrondraster=\@@rsraster,
   \c!achtergrondkleur=,
   \c!letter=\@@ialetter,
   \c!kleur=\@@iakleur,
   \c!contrastkleur=\@@iacontrastkleur,
   \c!zelfdepagina=\v!ja,
   \c!onbekendeverwijzing=\v!ja]

\stelinteractiebalkin
  [\c!status=\v!start,
   \c!variant=a,
   \c!symbool=\v!nee,
   \c!breedte=\rechterrandbreedte,
   \c!hoogte=, % these are taken care
   \c!diepte=, % of at calling time
   \c!afstand=.5em, % beter relateren aan breedte
   \c!stap=1,
   \c!kleur=\@@iakleur,
   \c!contrastkleur=\@@iacontrastkleur,
   \c!kader=\v!aan,
   \c!achtergrond=,
   \c!achtergrondraster=\@@rsraster,
   \c!achtergrondkleur=,
   \c!zelfdepagina=\v!ja,
   \c!onbekendeverwijzing=\v!ja]

\stelsynchronisatiebalkin
  [\c!variant=\v!pagina,
   \c!breedte=\rechterrandbreedte,
   \c!letter=\@@ialetter,
   \c!kleur=\@@iakleur,
   \c!achtergrond=,
   \c!achtergrondraster=\@@rsraster,
   \c!achtergrondkleur=]

\stelsynchronisatiein
  [\c!status=\v!stop]

\stelprofielenin
  [\c!optie=]

\stelprogrammasin
  [\c!gebied=]

\stelpaginaovergangenin
  [\v!reset]

\stelcommentaarin
  [\c!status=\v!start,
   \c!marge=2.5em,
   \c!afstand=1em,
   \c!breedte=.3\tekstbreedte,
   \c!hoogte=.2\teksthoogte,
   \c!kleur=\@@iakleur,
   \c!titel=,
   \c!spatie=\v!nee,
   \c!symbool=\v!normaal,
   \c!optie=]

\stelversiesin    % beware, @ is made active here,
  [\c!nummer=1,   % therefore we set this one at the end
   \c!letter=\ss,
   \c!kleur=]

\protect \endinput
