%D \module
%D   [       file=scrn-ini,
%D        version=2011.02.27,
%D          title=\CONTEXT\ Interaction Macros,
%D       subtitle=Initialization,
%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 / Initialization}

\unprotect

\registerctxluafile{scrn-ini}{autosuffix}

%D There is no interaction at all unless enabled by saying:
%D
%D \starttyping
%D \setupinteraction[state=start]
%D \stoptyping
%D
%D The other settings are:
%D
%D \showsetup{setupinteraction}

\installcorenamespace{interaction}

\installswitchcommandhandler \??interaction {interaction} \??interaction

\appendtoks
    \ifempty\previousinteraction
      \checkinteractionstate
    \orelse\ifx\currentinteraction\previousinteraction
      \checkinteractionstate
    \fi
\to \everysetupinteraction

\permanent\protected\def\checkinteractionstate
  {\ifcstok{\interactionparameter\c!state}\v!start
     \locationtrue
     \setsystemmode\v!interaction
   \else
     \locationfalse
     \resetsystemmode\v!interaction
   \fi}

\permanent\def\doifelselocation % expandable
  {\iflocation
     \expandafter\firstoftwoarguments
   \else
     \expandafter\secondoftwoarguments
   \fi}

\aliased\let\doiflocationelse\doifelselocation

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

\setupinteraction
  [\c!title=,
   \c!subtitle=,
   \c!author=,
   \c!keyword=,
   \c!date=]

\appendtoks
    \setupinteraction % todo: remember info at the lua end (already possible)
\to \everyjob

% it makes no sense to create an environment as we will seldom have structured
% interactions so a general start-stop will do
%
% \appendtoks
%     \protected\frozen\instance\edefcsname        \currentinteraction\endcsname{\scrn_interaction_direct{\currentinteraction}}%
%     \protected\frozen\instance\edefcsname\e!start\currentinteraction\endcsname{\scrn_interaction_start {\currentinteraction}}%
%     \protected\frozen\instance\edefcsname\e!stop \currentinteraction\endcsname{\scrn_interaction_stop                       }%
% \to \everydefineinteraction
%
% \protected\def\scrn_interaction_direct#1%
%   {\cdef\currentinteraction{#1}}
%
% \protected\def\scrn_interaction_start#1%
%   {\pushmacro\currentinteraction
%    \cdef\currentinteraction{#1}}
%
% \protected\def\scrn_interaction_stop
%   {\popmacro\currentinteraction}
%
% \protected\def\setinteraction[#1]%
%   {\cdef\currentinteraction{#1}}
%
% \defineinteraction[\v!interaction]

\permanent\protected\def\startinteraction[#1]%
  {\pushmacro\currentinteraction
   \cdef\currentinteraction{#1}}

\permanent\protected\def\stopinteraction
  {\popmacro\currentinteraction}

\permanent\protected\def\setinteraction[#1]%
  {\cdef\currentinteraction{#1}}

\defineinteraction % keep it simple
  [\v!hidden]

\setupinteraction
  [\v!hidden]
  [\c!state=\v!start,
   \c!color=,
   \c!contrastcolor=,
   \c!style=,
   \c!click=\v!off]

%D As long as there a natural feeling of what can be considered
%D hyper active or not, we have to tell users where they can
%D possibly click. We've already seen a few macros that deal
%D with this visualization, something we definitely do not let
%D up to the viewer. One way of telling is using a distinctive
%D typeface, another way is using color.
%D
%D There are two colors involved: one for normal hyperlinks,
%D and one for those that point to the currentpage, the
%D contrast color.

\definecolor [interactioncolor]         [r=0,  g=.6, b=0]
\definecolor [interactioncontrastcolor] [r=.8, g=0,  b=0]

%D The next few macros are responsible for highlighting hyper
%D links. The first one, \type{\showlocation}, is used in those
%D situations where the typeface is handled by the calling
%D macro.

%D When we're dealing with pure page references, contrast
%D colors are used when we are already at the page mentioned.

% \permanent\protected\def\setlocationcolorspecified#1% not grouped !
%   {\ifnum#1=\plusone
%      \edef\p_contrastcolor{\interactionparameter\c!contrastcolor}%
%      \ifempty\p_contrastcolor
%        \useinteractioncolorparameter\c!color
%      \else
%        \useinteractioncolorparameter\c!contrastcolor
%      \fi
%    \else % we could just set and if > 0 set again
%      \useinteractioncolorparameter\c!color
%    \fi}

% \permanent\protected\def\setlocationattributesspecified#1% not grouped !
%   {\ifnum#1=\plusone
%      \edef\p_contrastcolor{\interactionparameter\c!contrastcolor}%
%      \ifempty\p_contrastcolor
%        \useinteractionstyleandcolor\c!style\c!color
%      \else
%        \useinteractionstyleandcolor\c!style\c!contrastcolor
%      \fi
%    \else % we could just set and if > 0 set again
%      \useinteractionstyleandcolor\c!style\c!color
%    \fi}

% \permanent\protected\def\setlocationcolorspecspecified#1#2% \resolver
%   {\ifnum#1=\plusone
%      \edef\p_contrastcolor{#2\c!contrastcolor}%
%      \ifempty\p_contrastcolor
%        \colo_helpers_activate{#2\c!color}%
%      \else
%        \colo_helpers_activate\p_contrastcolor
%      \fi
%    \else
%      \colo_helpers_activate{#2\c!color}%
%    \fi}

\newconditional\c_scrn_interaction_no_style
\newconditional\c_scrn_interaction_no_color
\newconditional\c_scrn_interaction_no_contrastcolor

\permanent\protected\def\resetinteractionstyleandcolor
  {\c_scrn_interaction_no_style\conditionaltrue
   \c_scrn_interaction_no_color\conditionaltrue
   \c_scrn_interaction_no_contrastcolor\conditionaltrue}

\permanent\protected\def\resetinteractionstyle
  {\c_scrn_interaction_no_style\conditionaltrue}

\permanent\protected\def\resetinteractioncolor
  {\c_scrn_interaction_no_color\conditionaltrue
   \c_scrn_interaction_no_contrastcolor\conditionaltrue}

\permanent\protected\def\resetinteractioncontrastcolor
  {\c_scrn_interaction_no_contrastcolor\conditionaltrue}

\permanent\protected\def\restoreinteractionstyleandcolor
  {\c_scrn_interaction_no_style\conditionalfalse
   \c_scrn_interaction_no_color\conditionalfalse
   \c_scrn_interaction_no_contrastcolor\conditionalfalse}

\appendtoks
    \iflocation
      \restoreinteractionstyleandcolor
    \fi
\to \everybeforepagebody

\permanent\protected\def\setlocationcolorspecified#1% not grouped !
  {\ifconditional\c_scrn_interaction_no_color
    % none of them
   \orunless\ifconditional\c_scrn_interaction_no_contrastcolor
     \useinteractioncolorparameter\c!color
   \orelse\ifnum#1=\plusone
     \edef\p_contrastcolor{\interactionparameter\c!contrastcolor}%
     \ifempty\p_contrastcolor
       \useinteractioncolorparameter\c!color
     \else
       \useinteractioncolorparameter\c!contrastcolor
     \fi
   \else % we could just set and if > 0 set again
     \useinteractioncolorparameter\c!color
   \fi}

\permanent\protected\def\setlocationattributesspecified#1% not grouped !
  {\ifconditional\c_scrn_interaction_no_color
      \ifconditional\c_scrn_interaction_no_style
      \else
        \useinteractionstyleparameter\c!style
      \fi
   \orelse\ifnum#1=\plusone
     \edef\p_contrastcolor{\interactionparameter\c!contrastcolor}%
     \ifempty\p_contrastcolor
       \useinteractionstyleandcolor\c!style\c!color
     \else
       \useinteractionstyleandcolor\c!style\c!contrastcolor
     \fi
   \else
       \ifconditional\c_scrn_interaction_no_color
         \useinteractionstyleparameter\c!style
       \orelse\ifconditional\c_scrn_interaction_no_style
         \useinteractioncolorparameter\c!color
       \else
         \useinteractionstyleandcolor\c!style\c!color
       \fi
   \fi}

\permanent\protected\def\setlocationcolorspecspecified#1#2% \resolver
  {\ifconditional\c_scrn_interaction_no_color
    % none of them
   \orunless\ifconditional\c_scrn_interaction_no_contrastcolor
     \colo_helpers_activate{#2\c!color}%
   \orelse\ifnum#1=\plusone
     \edef\p_contrastcolor{#2\c!contrastcolor}%
     \ifempty\p_contrastcolor
       \colo_helpers_activate{#2\c!color}%
     \else
       \colo_helpers_activate\p_contrastcolor
     \fi
   \else
     \colo_helpers_activate{#2\c!color}%
   \fi}

\permanent\protected\def\setlocationcolor     {\setlocationcolorspecified     \referencepagestate}
\permanent\protected\def\setlocationattributes{\setlocationattributesspecified\referencepagestate}
\permanent\protected\def\setlocationcolorspec {\setlocationcolorspecspecified \referencepagestate}

\permanent\protected\def\setlocationfont % not grouped !
  {\useinteractionstyleparameter\c!style}

\setupinteraction
  [\c!style=\v!bold,
   \c!color=interactioncolor,
   \c!contrastcolor=interactioncontrastcolor]

%D Identity

%D We want to delay the preroll as it needs fonts so we now actively fetch from
%D the \LUA\ end. So this one gets called by the time we need the value.

\def\scrn_identity_get#1%
  {\setidentityvariable{#1}{\prerolltostring{\interactionparameter{#1}}}}

% this comes before starttext

\def\scrn_identity_document#1#2%
  {\doifdocumentvariable{metadata:#1}{\setupinteraction[#2=\documentvariable{metadata:#1}]}}

\appendtoks % not interfaced i.e. english
    \scrn_identity_document   {title}\c!title
    \scrn_identity_document {subject}\c!subtitle
    \scrn_identity_document{subtitle}\c!subtitle
    \scrn_identity_document  {author}\c!author
    \scrn_identity_document {authors}\c!author
    \scrn_identity_document {keyword}\c!keyword
    \scrn_identity_document{keywords}\c!keyword
    \scrn_identity_document    {date}\c!date
\to \everystartdocument % or stop

\appendtoks
    \clf_setautoprefix{\interactionparameter\c!prefix}%
\to \everysetupinteraction

%D This setter is global, contrary to the interaction setting which is local. The values of
%D these keys are only looked at when we finalize the document.

\def\syst_set_metadata_from_dummy#1%
  {\ifempty{\dummyparameter#1}\else
     \global\expandafter\letinteractionparameter\expandafter#1\lastnamedcs
   \fi}

\permanent\protected\def\setupmetadata[#1]%
  {\begingroup
   \resetdummyparameter\c!title
   \resetdummyparameter\c!subtitle
   \resetdummyparameter\c!author
   \resetdummyparameter\c!keyword
   \resetdummyparameter\c!date
   \getdummyparameters[#1]%
   \syst_set_metadata_from_dummy\c!title
   \syst_set_metadata_from_dummy\c!subtitle
   \syst_set_metadata_from_dummy\c!author
   \syst_set_metadata_from_dummy\c!keyword
   \syst_set_metadata_from_dummy\c!date
   \endgroup}

\protect \endinput
