%D \module
%D   [       file=scrn-int,
%D        version=2011.02.27, % moved from scrn-int
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Widgets,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Interaction Macros / Widgets}

\registerctxluafile{scrn-wid}{autosuffix}

% todo: expansion in comments (default is expanded)
% todo: check renderings ... acrobat crashes too easily on missing one

\unprotect

%D Attachments (mkiv upgraded):
%D
%D As usual in \CONTEXT\ we separate the general definition (frontend)
%D and the rendering (backend).

% hack: x\footnote{x\inleftmargin[scope=local]{\attachment[location=high,file=i-context.pdf]}}x

% old but stil valid method:
%
% \useattachment[test.tex]
% \useattachment[whatever][test.tex]
% \useattachment[whatever][newname][test.tex]
% \useattachment[whatever][title][newname][test.tex]
%
% new method:
%
% \registerattachment[sometag][specification] % name file author title subtitle
%
% \attachment[sometag][extra specs]
% \attachment[test.tex]
% \attachment[file=test.tex]
% \attachment[file=test.tex,method=hidden]
% \attachment[name=newname,file=test.tex]
% \attachment[title=mytitle,name=newname,file=test.tex]
%
% indirect
%
% \defineattachment[whatever5][file=test.tex] \attachment[whatever5][method=hidden]
% \defineattachment[whatever5][file=test.tex,method=hidden] \attachment[whatever5]
%
% direct (no definitions)
%
% \attachment[test][file=oeps.tex,title=Oeps,author=Hans,subtitle=TeX File,method=hidden]
% \attachment[label=test,file=oeps.tex,title=Oeps,author=Hans,subtitle=TeX File,method=hidden]
%
% autolabel:
%
% \attachment[file=oeps.tex,title=Oeps,author=Hans,subtitle=TeX File,method=hidden]
%
% % \setupattachments[\c!symbol={symbol-normal,symbol-down}]

% startattachment -> temp file

\newbox\b_scrn_attachment_collect
\newbox\b_scrn_attachment_link
\newbox\b_scrn_attachment_symbol

\installcorenamespace{attachment}
\installcorenamespace{attachmentlocation}
\installcorenamespace{attachmentmethod}

\installcommandhandler \??attachment {attachment} \??attachment

\aliased\let\setupattachments\setupattachment % convenience and compatibility

\setupattachment
  [\c!state=\v!start,
   \c!color=\interactionparameter\c!color,
  %\c!textlayer=,
  %\c!symbol=,
  %\c!title=,
  %\c!subtitle=,
  %\c!file=, % input filename
  %\c!name=, % new filename
  %\c!author=,
  %\c!method=, % \v!hidden = not in menu
  %\c!buffer=
   \c!symbol=,
   \c!distance=\emwidth,
   \c!width=\v!fit,
   \c!height=\v!fit,
   \c!depth=\v!fit,
   \c!location=\v!high]

\mutable\lettonothing\currentattachmentdepth
\mutable\lettonothing\currentattachmentheight
\mutable\lettonothing\currentattachmentregistered
\mutable\lettonothing\currentattachmentsymbol
\mutable\lettonothing\currentattachmentwidth

\permanent\tolerant\protected\def\registerattachment[#tag]#spacer[#S#settings]% we save (globally) at the lua end
  {\ifarguments\else
     \begingroup
     \cdef\currentattachment{_}%
     \setupcurrentattachment[#settings,\s!parent=\??attachment]%
     \clf_registerattachment
        tag        {#tag}%
        registered {#tag}%
        title      {\attachmentparameter\c!title}%
        subtitle   {\attachmentparameter\c!subtitle}%
        author     {\attachmentparameter\c!author}%
        file       {\attachmentparameter\c!file}%
        name       {\attachmentparameter\c!name}%
        buffer     {\attachmentparameter\c!buffer}%
        mimetype   {\attachmentparameter\c!type}%
     \relax
     \endgroup
   \fi}

\appendtoks
    \protected\frozen\instance\edefcsname        \currentattachment\endcsname{\scrn_attachment_direct[\currentattachment]}%
    \protected\frozen\instance\edefcsname\e!start\currentattachment\endcsname{\scrn_attachment_start [\currentattachment]}%
    \protected\frozen\instance\edefcsname\e!stop \currentattachment\endcsname{\scrn_attachment_stop                      }%
\to \everydefineattachment

\permanent\tolerant\protected\def\scrn_attachment_direct[#tag]#spacer[#registered]#spacer[#S#settings]%
  {\iflocation
     \bgroup
     \cdef\currentattachment{#tag}%
     \ifcstok{\attachmentparameter\c!state}\v!start
       \ifempty{#registered}%
         \scrn_attachment_inject[\v!auto][]%
       \orelse\ifhastok={#registered}%
          \scrn_attachment_inject[\v!auto][#registered]%
       \else
          \scrn_attachment_inject[#registered][#settings]%
       \fi
     \fi
     \egroup
   \fi}

\tolerant\protected\def\scrn_attachment_start[#tag]#spacer[#registered]#spacer[#S#settings]%
  {\cdef\currentattachment{#tag}%
   \unless\iflocation
     \expandafter\scrn_attachment_start_ignore
   \orelse\ifcstok{\attachmentparameter\c!state}\v!start
     \expandafter\scrn_attachment_start_indeed
   \else
     \expandafter\scrn_attachment_start_ignore
   \fi[#registered][#settings]}

\protected\def\scrn_attachment_stop
  {}

\def\scrn_attachment_start_indeed[#registered][#S#settings]%
  {\bgroup
   \ifempty{#registered}%
     \def\scrn_attachment_stop{\scrn_attachment_inject[\v!auto][\c!buffer=\v!attachment]\egroup}%
   \else
     \doifelseassignment{#registered}
       {\def\scrn_attachment_stop{\scrn_attachment_inject[\v!auto][\c!buffer=\v!attachment,#registered]\egroup}}%
       {\def\scrn_attachment_stop{\scrn_attachment_inject[#registered][\c!buffer=\v!attachment,#settings]\egroup}}%
   \fi
   \grabbufferdatadirect\v!attachment{\e!start\currentattachment}{\e!stop\currentattachment}}

\def\scrn_attachment_start_ignore
  {\expandafter\ignoreupto\csname\e!stop\currentattachment\endcsname}

\def\scrn_attachment_inject[#registered][#S#settings]%
  {\edef\currentattachmentregistered{#registered}%
   \setupattachment[\currentattachment][#settings]%
   \expandnamespaceparameter\??attachmentmethod\attachmentparameter\c!method\v!normal}

\defcsname\??attachmentmethod\v!normal\endcsname
  {\edef\currentattachmentsymbol{\attachmentparameter\c!symbol}%
   \edef\currentattachmentwidth {\attachmentparameter\c!width }%
   \edef\currentattachmentheight{\attachmentparameter\c!height}%
   \edef\currentattachmentdepth {\attachmentparameter\c!depth }%
   \ifempty\currentattachmentsymbol
     \ifx\currentattachmentwidth \v!fit\edef\currentattachmentwidth {.5\emwidth}\fi
     \ifx\currentattachmentheight\v!fit\edef\currentattachmentheight{.5\emwidth}\fi
     \ifx\currentattachmentdepth \v!fit\let \currentattachmentdepth \zeropoint\fi
   \else
     \clf_presetsymbollist{\attachmentparameter\c!symbol}%
     % we cannot yet ask for the wd/ht/dp of an xform else we could use those
     \setbox\b_scrn_attachment_symbol\hbox{\symbol[\lastpredefinedsymbol]}%
     \ifx\currentattachmentwidth \v!fit\edef\currentattachmentwidth {\wd\b_scrn_attachment_symbol}\fi
     \ifx\currentattachmentheight\v!fit\edef\currentattachmentheight{\ht\b_scrn_attachment_symbol}\fi
     \ifx\currentattachmentdepth \v!fit\edef\currentattachmentdepth {\dp\b_scrn_attachment_symbol}\fi
   \fi
   \clf_insertattachment
     tag               {\currentattachment}%
     registered        {\currentattachmentregistered}%
     width             {\currentattachmentwidth}%
     height            {\currentattachmentheight}%
     depth             {\currentattachmentdepth}%
     color             {\attachmentparameter\c!color}%
     colormodel        \c_attr_colormodel
     colorvalue        \rawcolorattribute{\attachmentparameter\c!color}%
     transparencyvalue \rawtransparencyattribute{\attachmentparameter\c!color}%
     symbol            {\currentattachmentsymbol}%
     layer             {\attachmentparameter\c!textlayer}%
     % these will be overloaded by registered when available
     title             {\attachmentparameter\c!title}%
     subtitle          {\attachmentparameter\c!subtitle}%
     author            {\attachmentparameter\c!author}%
     file              {\attachmentparameter\c!file}%
     name              {\attachmentparameter\c!name}%
     buffer            {\attachmentparameter\c!buffer}%
     mimetype          {\attachmentparameter\c!type}%
   \relax
   \wd\b_scrn_attachment_link\currentattachmentwidth
   \ht\b_scrn_attachment_link\currentattachmentheight
   \dp\b_scrn_attachment_link\currentattachmentdepth
   \expandnamespaceparameter\??attachmentlocation\attachmentparameter\c!location\s!unknown}

\defcsname\??attachmentmethod\v!hidden\endcsname
  {\clf_insertattachment
     tag        {\currentattachment}%
     registered {\currentattachmentregistered}%
     method     {\v!hidden}%
     title      {\attachmentparameter\c!title}%
     subtitle   {\attachmentparameter\c!subtitle}%
     author     {\attachmentparameter\c!author}%
     file       {\attachmentparameter\c!file}%
     name       {\attachmentparameter\c!name}%
     buffer     {\attachmentparameter\c!buffer}%
     mimetype   {\attachmentparameter\c!type}%
   \relax}

\protected\def\scrn_attachment_flush_traced
  {\hpack\bgroup
     \blackrule % can be a fast one
       [ \c!color=trace:r,
         \c!width=\wd\b_scrn_attachment_link,
        \c!height=\ht\b_scrn_attachment_link,
         \c!depth=\dp\b_scrn_attachment_link]%
     \kern-\wd\b_scrn_attachment_link
     \box\b_scrn_attachment_link
   \egroup}

\protected\def\scrn_attachment_flush_normal
  {\box\b_scrn_attachment_link}

\installtextracker
  {attachments.anchors}
  {\let\scrn_attachment_flush\scrn_attachment_flush_traced}
  {\let\scrn_attachment_flush\scrn_attachment_flush_normal}

\let\scrn_attachment_flush\scrn_attachment_flush_normal

\defcsname\??attachmentlocation\v!inmargin   \endcsname{\inmargin     {\scrn_attachment_flush}}
\defcsname\??attachmentlocation\v!leftedge   \endcsname{\inleftedge   {\scrn_attachment_flush}}
\defcsname\??attachmentlocation\v!rightedge  \endcsname{\inrightedge  {\scrn_attachment_flush}}
\defcsname\??attachmentlocation\v!leftmargin \endcsname{\inleftmargin {\scrn_attachment_flush}}
\defcsname\??attachmentlocation\v!rightmargin\endcsname{\inrightmargin{\scrn_attachment_flush}}
\defcsname\??attachmentlocation\v!high       \endcsname{\high         {\scrn_attachment_flush}}

\defcsname\??attachmentlocation\v!none\endcsname
  {\global\setbox\b_scrn_attachment_collect\hbox\bgroup
     \ifvoid\b_scrn_attachment_collect\else
       \box\b_scrn_attachment_collect
       \hskip{\attachmentparameter\c!distance}\relax
     \fi
     \scrn_attachment_flush
   \egroup}

\defcsname\??attachmentlocation\s!unknown\endcsname
  {\ifvoid\b_scrn_attachment_collect\else
     \box\b_scrn_attachment_collect
   \fi}

\permanent\protected\def\placeattachments
  {\ifvoid\b_scrn_attachment_collect\else
     \box\b_scrn_attachment_collect
   \fi}

\defineattachment[attachment]

% \ifx\currentinterface\defaultinterface \else
%     \defineattachment[\v!attachment]
% \fi

% backward compatible:

\permanent\tolerant\protected\def\useattachment[#tag]#spacer[#title]#spacer[#name]#spacer[#file]%
  {\ifarguments\or
     \registerattachment[#tag][title=#tag,name=#tag,file=#tag]%
   \or
     \registerattachment[#tag][title=#title,name=#title,file=#title]%
   \or
     \registerattachment[#tag][title=#title,name=#title,file=#name]%
   \or
     \registerattachment[#tag][title=#title,name=#name,file=#file]%
   \fi}

%D Comments:

% test
%
% \startcomment
%   hello beautiful\\world
% \stopcomment
%
% test
%
% \startcomment[hello]
%   hello << eerste >>
%   beautiful
%   world
% \stopcomment
%
% test
%
% \startcomment[hello][color=green,width=10cm,height=3cm]
%   hello
%   beautiful
%   world
% \stopcomment
%
% test
%
% \startcomment[hello][color=red,width=4cm,height=3cm]
%   hello
%
%   beautiful
%
%   world
% \stopcomment
%
% test
%
% \startcomment[symbol=Help]
%   Do we want this kind of rubish?
% \stopcomment
%
% test
%
% \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]
%
% \startcomment[hello]
%     oeps
% \stopcomment
%
% test
%
% \setupcomment
%   [\c!symbol=normal,
%    \c!option=max,width=10cm]
%
% \startcomment[hello]
%     oeps
% \stopcomment
%
% test

%D The implementation is mostly the same as for attachments but sharing code
%D will not make it cleaner.

\installcorenamespace{comment}
\installcorenamespace{commentlocation}

\installcommandhandler \??comment {comment} \??comment

\newbox\b_scrn_comment_collect
\newbox\b_scrn_comment_rendering
\newbox\b_scrn_comment_link
\newbox\b_scrn_comment_symbol

\setupcomment
  [\c!state=\v!start,
   \c!distance=\emwidth,
   \c!color=\interactionparameter\c!color,
   \c!space=\v!no,
   \c!symbol=,
  %\c!title=,
  %\c!option=,
  %\c!textlayer=,
   \c!width=\v!fit,
   \c!height=\v!fit,
   \c!depth=\v!fit,
   \c!nx=40,
   \c!ny=10,
   \c!buffer=\v!comment,
   \c!location=\v!high]

\mutable\lettonothing\currentcommentdepth
\mutable\lettonothing\currentcommentheight
\mutable\lettonothing\currentcommentsymbol
\mutable\lettonothing\currentcommentwidth

\appendtoks
    \protected\frozen\instance\edefcsname        \currentcomment\endcsname{\scrn_comment_argument[\currentcomment]}%
    \protected\frozen\instance\edefcsname\e!start\currentcomment\endcsname{\scrn_comment_start   [\currentcomment]}%
    \protected\frozen\instance\edefcsname\e!stop \currentcomment\endcsname{\scrn_comment_stop                     }%
\to \everydefinecomment

\tolerant\protected\def\scrn_comment_argument[#category]#spacer[#title]#spacer[#S#settings]#:#text%%
  {\iflocation
     \cdef\currentcomment{#category}%
     \ifcstok{\commentparameter\c!state}\v!start
       \ifhastok={#title}
         \setupcurrentcomment[#title]%
       \else
         \setupcurrentcomment[\c!title=#title,#settings]%
       \fi
       \clf_assignbuffer{\v!comment}{#text}\catcodetable\relax% todo: expansion control, but expanded by default (xml)
       \scrn_comment_inject
     \fi
   \fi
   \ignorespaces}

\tolerant\protected\def\scrn_comment_start[#category]#spacer[#title]#spacer[#S#settings]%
  {\cdef\currentcomment{#category}%
   \unless\iflocation
     \expandafter\scrn_comment_start_ignore
   \orelse\ifcstok{\commentparameter\c!state}\v!start
     \expandafter\scrn_comment_start_indeed
   \else
     \expandafter\scrn_comment_start_ignore
   \fi[#title][#settings]}

\def\scrn_comment_start_indeed[#title][#S#settings]%
  {\bgroup
   \doifelseassignment{#title}
     {\setupcurrentcomment[#title]}
     {\setupcurrentcomment[\c!title=#title,#settings]}%
   \protected\def\scrn_comment_stop{\scrn_comment_inject\egroup}%
   \grabbufferdatadirect\v!comment{\e!start\currentcomment}{\e!stop\currentcomment}}

\def\scrn_comment_start_ignore
  {\expandafter\ignoreupto\csname\e!stop\currentcomment\endcsname}

\protected\def\scrn_comment_stop
  {}

\installcorenamespace{commentmethods}

\protected\def\scrn_comment_inject
  {\expandnamespaceparameter\??commentmethods\commentparameter\c!method\v!normal}

%D Beware: comments symbols don't scale in acrobat (cf. spec but somewhat
%D weird, esp because for instance attachment symbols do scale).

\defcsname\??commentmethods\v!normal\endcsname
  {\edef\currentcommentsymbol{\commentparameter\c!symbol}%
   \edef\currentcommentwidth {\commentparameter\c!width }%
   \edef\currentcommentheight{\commentparameter\c!height}%
   \edef\currentcommentdepth {\commentparameter\c!depth }%
   \ifempty\currentcommentsymbol
     \ifx\currentcommentwidth \v!fit\edef\currentcommentwidth {.5\emwidth}\fi
     \ifx\currentcommentheight\v!fit\edef\currentcommentheight{.5\emwidth}\fi
     \ifx\currentcommentdepth \v!fit\let \currentcommentdepth \zeropoint\fi
   \else
     \clf_presetsymbollist{\commentparameter\c!symbol}%
     % we cannot yet ask for the wd/ht/dp of an xform else we could use those
     \setbox\b_scrn_comment_symbol\hbox{\symbol[\lastpredefinedsymbol]}%
     \ifx\currentcommentwidth \v!fit\edef\currentcommentwidth {\wd\b_scrn_comment_symbol}\fi
     \ifx\currentcommentheight\v!fit\edef\currentcommentheight{\ht\b_scrn_comment_symbol}\fi
     \ifx\currentcommentdepth \v!fit\edef\currentcommentdepth {\dp\b_scrn_comment_symbol}\fi
   \fi
   \clf_insertcomment
     tag               {\currentcomment}%
     title             {\commentparameter\c!title}%
     subtitle          {\commentparameter\c!subtitle}%
     author            {\commentparameter\c!author}%
     width             {\currentcommentwidth}%
     height            {\currentcommentheight}%
     depth             {\currentcommentdepth}%
     nx                {\commentparameter\c!nx}%
     ny                {\commentparameter\c!ny}%
     colormodel        \c_attr_colormodel
     colorvalue        \rawcolorattribute{\commentparameter\c!color}%
     transparencyvalue \rawtransparencyattribute{\commentparameter\c!color}%
     option            {\commentparameter\c!option}% % todo
     symbol            {\commentparameter\c!symbol}%
     buffer            {\commentparameter\c!buffer}% {\v!comment}%
     layer             {\commentparameter\c!textlayer}%
     space             {\commentparameter\c!space}%
   \relax
   \wd\b_scrn_comment_link\currentcommentwidth
   \ht\b_scrn_comment_link\currentcommentheight
   \dp\b_scrn_comment_link\currentcommentdepth
   \expandnamespaceparameter\??commentlocation\commentparameter\c!location\s!unknown}

\aliased\letcsname\??commentmethods\v!hidden\endcsname\donothing

\protected\def\scrn_comment_flush_traced
  {\hpack\bgroup
     \blackrule % can be a fast one
       [ \c!color=trace:y,
         \c!width=\wd\b_scrn_comment_link,
        \c!height=\ht\b_scrn_comment_link,
         \c!depth=\dp\b_scrn_comment_link]%
     \kern-\wd\b_scrn_comment_link
     \box\b_scrn_comment_link
   \egroup}

\protected\def\scrn_comment_flush_normal
  {\box\b_scrn_comment_link}

\installtextracker
  {comments.anchors}
  {\let\scrn_comment_flush\scrn_comment_flush_traced}
  {\let\scrn_comment_flush\scrn_comment_flush_normal}

\let\scrn_comment_flush\scrn_comment_flush_normal

% todo: dedicated margin classes

\defcsname\??commentlocation\v!inmargin   \endcsname{\inmargin     {\scrn_comment_flush}}
\defcsname\??commentlocation\v!leftedge   \endcsname{\inleftedge   {\scrn_comment_flush}}
\defcsname\??commentlocation\v!rightedge  \endcsname{\inrightedge  {\scrn_comment_flush}}
\defcsname\??commentlocation\v!leftmargin \endcsname{\inleftmargin {\scrn_comment_flush}}
\defcsname\??commentlocation\v!rightmargin\endcsname{\inrightmargin{\scrn_comment_flush}}
\defcsname\??commentlocation\v!high       \endcsname{\high         {\scrn_comment_flush}}

\defcsname\??commentlocation\v!none\endcsname
  {\global\setbox\b_scrn_comment_collect\hbox\bgroup
     \ifvoid\b_scrn_comment_collect\else
       \box\b_scrn_comment_collect
       \hskip{\commentparameter\c!distance}\relax
     \fi
     \scrn_comment_flush
   \egroup}

\defcsname\??commentlocation\s!unknown\endcsname
  {\ifvoid\b_scrn_comment_collect\else
     \box\b_scrn_comment_collect
   \fi}

\permanent\protected\def\placecomments
  {\ifvoid\b_scrn_comment_collect\else
     \box\b_scrn_comment_collect
   \fi}

\definecomment[comment]

% \ifx\currentinterface\defaultinterface \else
%     \definecomment[\v!comment]
% \fi

%D Conform Michals recommendations support for the old movie (and sound) annotations
%D has been removed. This also means that movies are no longer a valid external figure
%D format. So be it.
%D
%D Renderings (adapted to \MKIV\ by Michal Vlasák). We no longer have the \MKII\ internal
%D and external rendering because over time all this widget stuff changed in \PDF\ and
%D Acrobat.
%D
%D \starttyping
%D \setupinteraction[state=start]
%D
%D \starttext
%D     \startTEXpage[offset=10pt]
%D       % \definerenderingwindow[myrenderingwindow][width=\textwidth, height=\textwidth]
%D       % \definerenderingwindow[myrenderingwindow][width=\textwidth, height=\textwidth, openpageaction=StartRendering{myvideo}]
%D         \definerenderingwindow[myrenderingwindow][width=\textwidth, height=\textwidth, openpageaction=StartCurrentRendering, closepageaction=StopCurrentRendering]
%D       % \userendering[myvideo][video/mp4][edited.mp4][embed=yes, auto]
%D         \userendering[myvideo][video/mp4][woodwork.mp4][embed=yes, auto]
%D         % auto = openpageaction=StartCurrentRendering, closepageaction=StopCurrentRendering
%D         \placerenderingwindow[myrenderingwindow][myvideo]
%D         \blank
%D         \goto{start}[StartRendering{myvideo}]
%D         \goto{stop}[StopRendering{myvideo}]
%D     \stopTEXpage
%D \stoptext
%D \stoptyping

\mutable\lettonothing\currentrendering
\mutable\lettonothing\currentrenderingwindow

\permanent\tolerant\protected\def\userendering[#tag]#spacer[#mime]#spacer[#file]#spacer[#option]%
  {\clf_registerrendering
     label    {#tag}%
     mime     {#mime}%
     filename {#file}%
     option   {#option}%
   \relax}

\permanent\tolerant\protected\let\useexternalrendering\userendering

\permanent\def\renderingoption#tag{\clf_renderingvar{#tag}{option}}

\newdimension\d_scrn_rendering_width  \d_scrn_rendering_width  8cm
\newdimension\d_scrn_rendering_height \d_scrn_rendering_height 6cm

%newinteger  \c_scrn_rendering_page % makes no sense otherwise

%D \starttyping
%D \definerenderingwindow[soundplace][width=0pt, height=0pt]
%D \useexternalrendering[mainsound][audio/mp3][sound.mp3][embed=yes]
%D \placerenderingwindow[soundplace][mainsound]
%D \stoptyping

\installcorenamespace{renderingwindow}

\installframedcommandhandler \??renderingwindow {renderingwindow} \??renderingwindow

\appendtoks
    \resetrenderingwindowparameter\c!openpageaction
    \resetrenderingwindowparameter\c!closepageaction
    \setrenderingwindowparameter  \c!width          {\d_scrn_rendering_width }%
    \setrenderingwindowparameter  \c!height         {\d_scrn_rendering_height}%
    \letrenderingwindowparameter  \c!align          \v!flushleft
\to \everypresetrenderingwindow

\permanent\tolerant\protected\def\placerenderingwindow[#window]#spacer[#rendering]% do all in lua
  {\bgroup
   \edef\currentrendering{\ifparameter#rendering\or#rendering\else#window\fi}%
   % create fall back if needed
   \edef\currentrenderingwindow{\namedrenderingwindowparameter{#window}\c!width}% stupid test, we need a proper one here
   \ifempty\currentrenderingwindow
     \let\currentrenderingwindow\s!default
     \definerenderingwindow[\currentrenderingwindow]% why not global
   \else
     \edef\currentrenderingwindow{#window}%
   \fi
   \ifx\currentrenderingwindow\s!default
     \d_scrn_rendering_height\vsize
     \d_scrn_rendering_width \hsize
    %\c_scrn_rendering_page\realpageno
   \else
     \d_scrn_rendering_height{\renderingwindowparameter\c!height}%
     \d_scrn_rendering_width {\renderingwindowparameter\c!width}%
    %\c_scrn_rendering_page\realpageno
   \fi
   \letrenderingwindowparameter\c!offset\v!overlay
   \inheritedrenderingwindowframed
     {\vfilll
      \dontleavehmode
      \clf_insertrenderingwindow
        label     {\currentrendering}%
        width     \d_scrn_rendering_width
        height    \d_scrn_rendering_height
        option    {\renderingoption\currentrendering}%
      % page      \m_scrn_rendering_page
        page      \realpageno
        openpage  {\renderingwindowparameter\c!openpageaction}%
        closepage {\renderingwindowparameter\c!closepageaction}%
      \relax
      \hfill}%
   \egroup}

%D Linkedlists (not tested in mkvi):

% %D The next mechanism, linked lists, is quite old and
% %D is \MKIV'd for completeness. I will finish the
% %D configuration part when I need it.
% %D
% %D \starttyping
% %D \setupinteraction[state=start]
% %D \definelinkedlist[demo]
% %D \dorecurse{10}{\linkedlistelement[demo]{link \recurselevel} \page}
% %D \stoptyping
%
% \definesystemvariable {lk}   % LinK
%
% \installcommandhandler\??lk{linkedlist}\??lk
%
% \let\setupbutton\setuplinkedlists\setuplinkedlist
%
% \appendtoks
%     \clf_definelinkedlist{\currentlinkedlist}%
% \to \everydefinelinkedlist
%
% \def\setlinkedlistproperties#1#2#3#4#5#6%
%   {\def\currentlink {#1}%
%    \def\noflinks    {#2}%
%    \def\firstlink   {#3}%
%    \def\previouslink{#4}%
%    \def\nextlink    {#5}%
%    \def\lastlink    {#6}}
%
% \def\linkedlistelement[#1]#2% currently no view support
%   {\dontleavehmode\hbox\bgroup
%    #2%
%    \iflocation
%      \edef\currentlinkedlist{#1}%
%      \ifcsname\??lk\currentlinkedlist\s!parent\endcsname
%        \hskip\linkedlistparameter\c!distance\relax
%        \clf_addlinklistelement{\currentlinkedlist}%
%        \expanded{\ctxlatecommand{enhancelinkedlist("\currentlinkedlist",\currentlink)}}% can also be done at the lua end
%        \dogotosomepage  {\??lk\currentlinkedlist}\gotobegincharacter \firstlink
%        \ifnum\noflinks>\plustwo
%          \dogotosomepage{\??lk\currentlinkedlist}\gobackwardcharacter\previouslink
%          \dogotosomepage{\??lk\currentlinkedlist}\goforwardcharacter \nextlink
%        \fi
%        \dogotosomepage  {\??lk\currentlinkedlist}\gotoendcharacter   \lastlink
%      \else
%        \writestatus\m!interactions{no such linked list: \currentlinkedlist}%
%      \fi
%    \fi
%    \egroup}
%
% \setuplinkedlists
%   [\c!distance=.25em,
%    \c!width=\v!fit,
%    \c!location=\v!low,
%    \c!color=\interactionparameter\c!color,
%    \c!frame=\v!off,
%    \c!background=,
%    \c!backgroundcolor=]

\protect \endinput
