%D \module
%D   [       file=m-educat,
%D        version=2003.03.05,
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=Educational Extras,
%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 This module will collect a few macros cq.\ definitions
%D meant for educational use. Most of the code has been laying
%D around for some time and has been (or is still) used in
%D \PRAGMA\ projects. 

\unprotect 

\startvariables            dutch                     english
                           german                    czech
                           italian                   romanian

               answerarea: antwoordgebied            answerarea
                           answerarea                answerarea
                           answerarea                answerarea

\stopvariables

\startelements             dutch                     english
                           german                    czech
                           italian                   romanian

              answerspace: antwoordruimte            answerspace 
                           answerspace               answerspace 
                           answerspace               answerspace 
              answerlines: antwoordregels            answerlines 
                           answerlines               answerlines 
                           answerlines               answerlines 

\stopelements

\startcommands             dutch                     english
                           german                    czech
                           italian                   romanian

          setupanswerarea: stelantwoordgebiedin      setupanswerarea
                           setupanswerarea           setupanswerarea
                           setupanswerarea           setupanswerarea

\stopcommands

\definesystemvariable{iv}

\definecolor [answerareacolor] [s=.90]
\definecolor [answerlinecolor] [white]

\definetextbackground
  [\v!answerarea]
  [\c!plaats=\v!tekst,
   \c!n=0,
   \c!kader=\v!uit,
   \c!kaderkleur=answerlinecolor,       
   \c!lijndikte=2pt,
   \c!achtergrond=\v!kleur,
   \c!achtergrondkleur=answerareacolor, 
   \c!variant=2] % betweenline

% n=gedwongen
% m=extra auto

\def\setupanswerarea
  {\setuptextbackground[\v!answerarea]}

\setvalue\e!answerspace
  {\dosingleempty\doanswerspace}

\def\doanswerspace[#1]%
  {\begingroup
   \dontcomplain
   \setupanswerarea
     [\c!n=0,\c!m=,#1,\c!plaats=\v!tekst]%
   \doifelsenothing{\backgroundvariable\c!m}
     {\expandafter\donoanswerspace}
     {\expandafter\dodoanswerspace}%
   [#1]}

\def\donoanswerspace[#1]#2%
  {\setupthinrules
     [\c!variant=\backgroundvariable\c!variant,
      \c!kleur=\backgroundvariable\c!kaderkleur,
      \c!achtergrond=\backgroundvariable\c!achtergrond,
      \c!achtergrondkleur=\backgroundvariable\c!achtergrondkleur,
      \c!lijndikte=\backgroundvariable\c!lijndikte]%
   \doifelse{\backgroundvariable\c!n}{*}
     {\thinrule
      \par}
     {\scratchcounter0\backgroundvariable\c!n\relax
      % tricky, guess  
      \def\processisolatedword##1%
        {\setbox\scratchbox=\hbox{##1}%
         \vbox{\hsize\wd\scratchbox\thinrule
         \ifcase\scratchcounter\else
           \setbox\scratchbox=\hbox{\space}%
           \nobreak\hskip\zeropoint \!!minus \wd\scratchbox
           \vbox{\hsize\wd\scratchbox\thinrule}%
         \fi}}%
      \processisolatedwords{#2}\processisolatedword
      % so far 
      \ifcase\scratchcounter \else \ifnum\scratchcounter<3
        \nobreak \vbox{\hsize\scratchcounter em\thinrule}%
      \else % more
        \advance \scratchcounter -2
        \dorecurse\scratchcounter{\allowbreak\vbox{\hsize1em\thinrule}}%
        \nobreak \vbox{\hsize2em\thinrule}%
      \fi \fi}%
   \endgroup}

\def\dodoanswerspace[#1]#2% m case
  {\getvalue{\e!start\v!answerarea}%
   #2%
   \doifelse{\backgroundvariable\c!m}{*}
     {\hfill\strut
      \getvalue{\e!stop\v!answerarea}%
      \par}
     {\scratchcounter0\backgroundvariable\c!m\relax
      \ifcase\scratchcounter \else \ifnum\scratchcounter<3
        \nobreak \hbox to \scratchcounter em{\strut\hss}%
      \else % more
        \advance \scratchcounter -2
        \dorecurse\scratchcounter{\allowbreak\hbox to 1em{\strut\hss}}%
        \nobreak \hbox to 2em{\strut\hss}%
      \fi \fi
      \getvalue{\e!stop\v!answerarea}}%
   \endgroup}

\setvalue{\e!start\e!answerlines}%
  {\dosingleempty\dostartanswerlines}

\def\dostartanswerlines[#1]%
  {\begingroup
   \dontcomplain
   \setupanswerarea
     [\c!n=0,\c!m=,#1,\c!plaats=\v!tekst]%
   \doifnot{\backgroundvariable\c!optie}\v!aansluitend\softbreak
   \doifelsenothing{\backgroundvariable\c!m}
     {\expandafter\donostartanswerlines}
     {\expandafter\dodostartanswerlines}%
   [#1]}

\def\donostartanswerlines[#1]%
  {\setupthinrules
     [\c!variant=\backgroundvariable\c!variant,
      \c!kleur=\backgroundvariable\c!kaderkleur,
      \c!achtergrond=\backgroundvariable\c!achtergrond,
      \c!achtergrondkleur=\backgroundvariable\c!achtergrondkleur,
      \c!lijndikte=\backgroundvariable\c!lijndikte]%
   \thinrules[\c!n=\backgroundvariable\c!n]\par
   \endgroup
   \grabuntil{\e!stop\e!answerlines}}

\def\dodostartanswerlines[#1]%
  {\begingroup    
   \getvalue{\e!start\v!answerarea}%
   \ignorespaces}

\setvalue{\e!stop\e!answerlines}% 
  {\scratchcounter0\backgroundvariable\c!m\relax
   % a \softbreak is more efficient in pos dan \par 
   \ifcase\scratchcounter
     % nothing
   \or
     \softbreak 
   \else
     \softbreak 
     \advance \scratchcounter \minusone
     \dorecurse\scratchcounter{\strut\hfill\strut\softbreak}%
   \fi
   \strut\hfill\strut
   \getvalue{\e!stop\v!answerarea}%
   \par\endgroup\endgroup} 

\setvalue\e!answerlines
  {\dosingleempty\doanswerlines}

\def\doanswerlines[#1]#2%
  {\getvalue{\e!start\e!answerlines}[#1]%
   #2%
   \getvalue{\e!stop\e!answerlines}}

\protect \doifnotmode{demo}{\endinput}

%D Test materiaal.

\starttext

\startnotmode[answers]

  \setupanswerarea[level=+1]

\stopnotmode

\setupcolors[state=start]

test test test \answerspace [n=10] {Whow}. test test test
test tets test test \answerspace [n=10] {Whow}. test test
test test tets test test \answerspace [n=10] {Whow}. test
test test test tets test test \answerspace [n=10] {Whow}.
test test test test test test \answerspace [n=*] {Whow.} 

test test test test test test test \startanswerlines
[n=3] What A Junk Answer \stopanswerlines 

test test test test test test test \startanswerlines
[n=3,alternative=0] What A Junk Answer \stopanswerlines 

test test test test test test test \startanswerlines
[n=3,alternative=1] What A Junk Answer \stopanswerlines 

test test test test test test test \startanswerlines
[n=3,alternative=2] What A Junk Answer \stopanswerlines 

\startitemize[paragraph]
\item \startanswerlines [option=seried,n=2] xxx \stopanswerlines
\stopitemize

test test test \answerspace [m=10] {Whow}. test test test
test tets test test \answerspace [m=10] {Whow}. test test
test test tets test test \answerspace [m=10] {Whow}. test
test test test tets test test \answerspace [m=10] {Whow}.
test test test test test test \answerspace [m=*] {Whow.} 

test test test test test test test \startanswerlines
[m=2] What A Junk Answer \stopanswerlines 

test test test test test test test \startanswerlines
[m=2,alternative=0] What A Junk Answer \stopanswerlines 
 
test test test test test test test \startanswerlines
[m=2,alternative=1] What A Junk Answer \stopanswerlines 

test test test test test test test \startanswerlines
[m=2,alternative=2] What A Junk Answer \stopanswerlines 
 
\startitemize[paragraph]
\item \startanswerlines [option=seried,m=2] xxx \stopanswerlines
\stopitemize

\stoptext
