%D \module
%D   [       file=grph-fig,
%D        version=2006.08.26, % overhaul of 1997.03.31
%D          title=\CONTEXT\ Graphic Macros,
%D       subtitle=Figure Inclusion,
%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.

% This is (yet) untested in LMTX!

\unprotect

%D Used in the styledesign manual:
%
% beware in mkiv we don't have the typeset- prefix
%
% \setbuffer[typeset-b]\endbuffer
% \setbuffer[typeset-a]\endbuffer
%
% todo:
%
% \appendtoks \setbuffer[typeset-b]\endbuffer\to \everystarttext
% \appendtoks \setbuffer[typeset-a]\endbuffer\to \everystarttext

% we could use \typesetbuffer[*] to access the last one

\newconstant\c_grph_buffers_mode

\mutable\lettonothing\lasttypesetbuffer

\permanent\protected\def\typesetbuffer    {\bgroup\setconstant\c_grph_buffers_mode\plusone  \grph_buffers_typeset}
\permanent\protected\def\typesetbufferonly{\bgroup\setconstant\c_grph_buffers_mode\zerocount\grph_buffers_typeset}

\tolerant\permanent\protected\def\grph_buffers_typeset[#1]#*[#S#2]%
  {\ifparameters
     \grph_buffers_typeset_indeed\jobname\empty
   \or
     \ifhastok={#1}%
       \grph_buffers_typeset_indeed\jobname{#1}%
     \else
       \grph_buffers_typeset_indeed{#1}\empty
     \fi
   \or
     \grph_buffers_typeset_indeed{#1}{#2}%
   \fi}

\def\grph_buffers_typeset_indeed#1#2% we could use the via files
  {\iftok{#1}{*}\else
     \xdef\lasttypesetbuffer{\clf_typesetbuffer{#1}}%
   \fi
   \ifcase\c_grph_buffers_mode
     % typesetonly
   \or
     \externalfigure[\lasttypesetbuffer][#2]%
   \fi
   \egroup}

\tolerant\permanent\protected\def\runbuffer[#1]#*[#S#2]%
  {\xdef\lasttypesetbuffer{\clf_runbuffer{#1}{#2}}}

% For manuals and such:
%
% \definetypesetting [name] [options]   [settings-a]
%
% \typesetfile       [name] [file]      [settings-b]
% \typesetfile       [file] [options]   [settings-b]
% \typesetfile       [file] [settings-b]
% \typesetfile       [file]
%
% \enabletrackers[files.run]
% \starttext
%     \typesetfile[oepsoeps.tex][width=10cm,frame=on]
% \stoptext

\installcorenamespace{typesettingfile}

\tolerant\permanent\protected\def\definetypesetting[#1]#*[#2]#*[#S#3]% <name> options settings-a
  {\ifparameter#1\or
     \protected\frozen\instance\defcsname\??typesettingfile#1\endcsname{\grph_typesetting_process_indeed{#2}{#3}}%
   \fi}

\tolerant\permanent\protected\def\typesetfile[#1]#*[#S#2]#*[#S#3]% <name> filename settings-b | filename options settings
  {\ifcsname\??typesettingfile#1\endcsname
     \lastnamedcs{#2}{#3}%
   \orelse\ifparameter#3\or
     \grph_typesetting_process_indeed{#2}{#3}{#1}{}%
   \orelse\ifparameter#2\or % filename settings
     \grph_typesetting_process_indeed{}{#2}{#1}{}%
   \fi}

\defineexternalfigure[typesetting] % so one can set a frame and such

\lettonothing\m_typesetting_name

\def\grph_typesetting_process_indeed#1#2#3#4% options settings-a filename settings-b
  {\begingroup
   \edef\m_typesetting_name{\clf_runcontextjob{#3}{#1}}%
   \ifempty\m_typesetting_name \else
     \expanded{\externalfigure[\m_typesetting_name][typesetting]}[#2,#4]%
   \fi
   \endgroup}

%D Whatever ... hardly used ... but historic ... needs checking ... will probably
%D become m-fig-nn.mkiv .. or I will extend it cq. clean it up when I needed it;
%D after all, it's documented in old manuals.
%D
%D \stoptyping
%D \startfigure[mycow][t:/sources/cow.pdf][width=4cm]
%D   % one can also use layers
%D \stopfigure
%D
%D \externalfigure[mycow]
%D \stoptyping

\newinteger  \c_grph_steps_reference
\newdimension\d_grph_steps_x
\newdimension\d_grph_steps_y
\newbox      \b_grph_steps_colorbar

\ifdefined\referring\else \mutable\let\referring\relax \fi
\ifdefined\marking  \else \mutable\let\marking  \relax \fi
\ifdefined\remark   \else \mutable\let\remark   \relax \fi
\ifdefined\colorbar \else \mutable\let\colorbar \relax \fi

\tolerant\protected\def\grph_steps_place_figure#1#2#3#4#5%
  {\hbox\bgroup
     \setbox\scratchbox\hbox % somehow a space creeps in
       {% we can use \nospaces\plusone
        \useexternalfigure[\s!dummy][#2][#3,#5]%
        \externalfigure[\s!dummy]}%
     \setbox\scratchbox\hbox
       {\externalfigure[#2][#3,#5]}%
     \grph_steps_calculate
     \startpositioning
       \enforced\let\referring\grph_steps_one_referring
       \enforced\let\marking  \grph_steps_one_marking
       \enforced\let\remark   \grph_steps_one_remark
       \enforced\let\colorbar \grph_steps_one_colorbar
       \position(0,0){\box\scratchbox}%
       \linewidth\onepoint
       \setuppositioning
         [\c!unit=pt,%
          \c!xscale=\toscaled\d_grph_steps_x,%
          \c!yscale=\toscaled\d_grph_steps_y,%
          \c!factor=1]%
       \ignorespaces#4%
       \enforced\let\referring\grph_steps_two_referring
       \enforced\let\marking  \grph_steps_two_marking
       \enforced\let\remark   \grph_steps_two_remark
       \enforced\let\colorbar \grph_steps_two_colorbar
       \ignorespaces#4\removeunwantedspaces % or just grab #4 unspaced
     \stoppositioning
     \ifvoid\b_grph_steps_colorbar\else\box\b_grph_steps_colorbar\fi
   \egroup}

\protected\def\grph_steps_one_referring(#1,#2)#*(#3,#4)#*[#S#5]%
  {\position(#1,#2){\grph_steps_goto(#3,#4){\externalfigureparameter\c!frames}[#5]}}

\protected\def\grph_steps_one_marking(#1,#2)#*(#3,#4)#*[#S#5]%
  {\position(#1,#2){\grph_steps_this_is(#3,#4){\externalfigureparameter\c!frames}[#5]}}

\protected\def\grph_steps_one_remark   (#-,#-)#*(#-,#-)#*[#-]#*#:#-{}% (x,y)(h,b)[...]{tekst}
\protected\def\grph_steps_one_colorbar #-[#-]{}
\protected\def\grph_steps_two_referring(#-,#-)#*(#-,#-)#*[#-]{}

\let\grph_steps_two_marking\grph_steps_two_referring

\protected\def\grph_steps_two_remark
  {\grph_steps_comment\v!no}

\protected\def\grph_steps_two_colorbar#1[#2]
  {\begingroup
   \global\setbox\b_grph_steps_colorbar\vpack % \vbox ?
     {\forgetall
      \processcommalist[#2]\grph_colorbar_make_step}%
   \global\setbox\b_grph_steps_colorbar\vpack
     {\hskip2\emwidth\box\b_grph_steps_colorbar}%
   \global\wd\b_grph_steps_colorbar\zeropoint
   \endgroup}

\protected\def\grph_colorbar_make_step#1%
  {\blackrule[\c!color=#1,\c!width=2\emwidth,\c!height=\exheight,\c!depth=\zeropoint]%
   \endgraf}

\permanent\protected\def\startfigure[#1]#*[#2]#*[#3]#:#4\stopfigure
  {\ifcstok{\externalfigureparameter\c!option}\v!test
     \grph_steps_test_figure{#1}{#2}{#3}{#4}%
     \letexternalfigureparameter\c!frames\v!on
   \else
     \letexternalfigureparameter\c!frames\v!off
   \fi
   \tolerant\defcsname\??externalfigureinstance#1\endcsname[##1]%
     {\grph_steps_place_figure{#1}{#2}{#3}{#4}{##1}%
     % this is a dirty hack but ok for an old macro
     \enforced\let\figurestatus\plusone}}

\permanent\protected\lettonothing\stopfigure

\ifdefined\referring \else \let\referring\relax \fi
\ifdefined\marking   \else \let\marking  \relax \fi
\ifdefined\remark    \else \let\remark   \relax \fi
\ifdefined\colorbar  \else \let\colorbar \relax \fi

\protected\def\grph_steps_test_figure#1#2#3#4%
  {\begingroup
     \setbox\scratchbox\hpack
       {\useexternalfigure[\s!dummy][#2][\c!wfactor=\v!max]%
        \externalfigure[\s!dummy]}%
     \let\referring\grph_steps_three_referring
     \let\marking  \grph_steps_three_marking
     \let\remark   \grph_steps_three_remark
     \let\colorbar \grph_steps_three_colorbar
     \c_grph_steps_reference\zerocount
     \setbox\scratchboxone\vpack
       {\hsize240\points
        \startpositioning
          \grph_steps_calculate
          \position(0,0)
            {\box\scratchbox}%
          \position(0,0)
            {\basegrid
               [\c!nx=\externalfigureparameter\c!xmax,%
                \c!dx=\toscaled\d_grph_steps_x,%
                \c!ny=\externalfigureparameter\c!ymax,%
                \c!dy=\toscaled\d_grph_steps_y,%
                \c!xstep=1,%
                \c!ystep=1,%
                \c!scale=1,%
                \c!offset=\v!no,%
                \c!unit=pt]}%
          \setuppositioning
            [\c!unit=pt,%
             \c!xscale=\toscaled\d_grph_steps_x,%
             \c!yscale=\toscaled\d_grph_steps_y,%
             \c!factor=1]%
          \linewidth\onepoint
          \ignorespaces#4\removeunwantedspaces % or just grab #4 unspaced
        \stoppositioning
        \vfill}%
     \c_grph_steps_reference\zerocount
     \enforced\let\referring\grph_steps_four_referring
     \enforced\let\marking  \grph_steps_four_marking
     \enforced\let\remark   \grph_steps_four_remark
     \enforced\let\colorbar \grph_steps_four_colorbar
     \setbox\scratchboxtwo\vbox % \vpack ?
       {\forgetall
        \begingroup
        \tfa\ifempty{#1}#1\else#2\fi
        \endgroup
        \blank
        \tfxx#4%
        \vfilll}%
     \ifdim\ht\scratchboxone>\ht\scratchboxtwo
       \ht\scratchboxtwo\ht\scratchboxone
     \else
       \ht\scratchboxone\ht\scratchboxtwo
     \fi
     \hpack
       {\hskip3\emwidth
        \tpack{\vskip12\points\box\scratchboxone\vskip6\points}%
        \tpack{\vskip12\points\box\scratchboxtwo\vskip6\points}}%
   \endgroup}

\definesystemconstant{vwa}
\definesystemconstant{vwb}

\tolerant\protected\def\grph_steps_three_referring(#1,#2)#*(#3,#4)#*[#5]%
  {\advanceby\c_grph_steps_reference\plusone
   \position(#1,#2)
     {\hbox{\the\c_grph_steps_reference}}%
   \position(#1,#2)
     {\gotosomeinternal\s!vwb{#5}\realfolio
        {\grph_steps_marker(#3,#4)\v!on{\thisissomeinternal\s!vwa{#5}}}}}

\protected\def\grph_steps_three_remark
  {\grph_steps_comment\v!yes}

\let\grph_steps_three_marking \grph_steps_three_referring
\let\grph_steps_three_colorbar\grph_steps_one_colorbar

\protected\def\grph_steps_four_referring{\grph_steps_text{\normalstartimath\rightarrow\normalstopimath}}
\protected\def\grph_steps_four_marking  {\grph_steps_text{\normalstartimath\leftarrow \normalstopimath}}

\let\grph_steps_four_remark  \grph_steps_one_remark
\let\grph_steps_four_colorbar\grph_steps_one_colorbar

% Helpers:

\def\grph_steps_calculate
  {\ifnum0\externalfigureparameter\c!xmax=\zerocount
     \ifnum0\externalfigureparameter\c!ymax=\zerocount
       \setexternalfigureparameter\c!ymax{24}%
     \fi
     \d_grph_steps_y\figureheight
     \divideby\d_grph_steps_y \externalfigureparameter\c!ymax
     \d_grph_steps_x\d_grph_steps_y
     \scratchdimen\figurewidth
     \advanceby\scratchdimen\d_grph_steps_y
     \divideby \scratchdimen\d_grph_steps_y
     \setexternalfigureparameter\c!xmax{\number\scratchdimen}%
   \else
     \d_grph_steps_x\figurewidth  \divideby\d_grph_steps_x \externalfigureparameter\c!xmax\relax
     \d_grph_steps_y\figureheight \divideby\d_grph_steps_y \externalfigureparameter\c!ymax\relax
   \fi}

\tolerant\def\grph_steps_comment#1(#2,#3)#*(#4,#5)#*[#S#6]#*#:#7% {kader}(x,y)(h,b)[...]{tekst}
  {\position(#2,#3)%
     {\setnostrut
      \framed[\c!width=#4\d_grph_steps_x,\c!height=#5\d_grph_steps_y,\c!offset=\v!none,\c!frame=#1,#6]{#7}}}

% \def\grph_steps_figure#1%
%   {\position(0,0){\getvalue{#1}}}

\def\grph_steps_goto(#1,#2)#3[#4]%    (h,b)kader[ref]
  {\gotobox{\vpack{\grph_steps_area(#1,#2)#3{}}}[#4]}

\tolerant\def\grph_steps_text#1(#2,#3)#*(#4,#5)#*[#6]%
  {\advanceby\c_grph_steps_reference\plusone
   \hbox % \hpack ?
     {\quad
      \thisissomeinternal\s!vwb{#6}%
      \gotosomeinternal\s!vwa{#6}\realfolio{\hbox to 1.5\emwidth{\the\c_grph_steps_reference\hfill}}%
      \quad#1 (#2,#3) (#4,#5) [#6]\hfill}%
   \endgraf}

\def\grph_steps_this_is(#1,#2)#3[#4]%
  {\grph_steps_area(#1,#2){#3}{\dosetdirectpagereference{#4}}}

\def\grph_steps_area(#1,#2)#3#4% (h,b){kader}{tekst}
  {\bgroup
   \setnostrut
   \framed[\c!width=#1\d_grph_steps_x,\c!height=#2\d_grph_steps_y,\c!offset=\zeropoint,\c!frame=#3]{#4}%
   \egroup}

\def\grph_steps_marker(#1,#2)#3#4% (h,b){kader}{tekst}
  {\framed[\c!width=#1\d_grph_steps_x,\c!height=#2\d_grph_steps_y,\c!offset=\v!none,\c!frame=#3]{#4}}

\protect \endinput

% \startbuffer
% \definecolor [blue]   [c=1,m=.38,y=0,k=.64]
% \definecolor [yellow] [c=0,m=.28,y=1,k=.06]
%
% \definespotcolor [blue-100]   [blue]   [p=1]
% \definespotcolor [yellow-100] [yellow] [p=1]
%
% \definemultitonecolor [combicolor]   [blue=.12,yellow=.28] [c=.1,m=.1,y=.3,k=.1]
%
% \definemultitonecolor [combicolor-b] [blue=1]   [c=1,m=.38,y=0,k=.64] % force multitone
% \definemultitonecolor [combicolor-y] [yellow=1] [c=0,m=.28,y=1,k=.06] % force multitone
%
% \useexternalfigure[demo-a][mill.png]     [object=no,width=.2\textwidth]
% \useexternalfigure[demo-b][hacker-bw.jpg][object=no,width=.2\textwidth]
%
% \startbaselinecorrection \startcombination[4*1]
%   {\externalfigure[demo-a]}                     {no color}
%   {\externalfigure[demo-a][color=combicolor]}   {indexed duotone}
%   {\externalfigure[demo-a][color=combicolor-b]} {spot color}
%   {\externalfigure[demo-a][color=combicolor-y]} {spot color}
% \stopcombination \stopbaselinecorrection
%
% \startbaselinecorrection \startcombination[4*1]
%   {\externalfigure[demo-b]}                     {no color}
%   {\externalfigure[demo-b][color=combicolor]}   {indexed duotone}
%   {\externalfigure[demo-b][color=combicolor-b]} {spot color}
%   {\externalfigure[demo-b][color=combicolor-y]} {spot color}
% \stopcombination \stopbaselinecorrection
%
% \startbaselinecorrection \startcombination[4*1]
%   {\externalfigure[demo-a]}                     {no color}
%   {\externalfigure[demo-a][color=combicolor]}   {indexed duotone}
%   {\externalfigure[demo-a][color=blue-100]}     {spot color}
%   {\externalfigure[demo-a][color=yellow-100]}   {spot color}
% \stopcombination \stopbaselinecorrection
%
% \startbaselinecorrection \startcombination[4*1]
%   {\externalfigure[demo-b]}                     {no color}
%   {\externalfigure[demo-b][color=combicolor]}   {indexed duotone}
%   {\externalfigure[demo-b][color=blue-100]}     {spot color}
%   {\externalfigure[demo-b][color=yellow-100]}   {spot color}
% \stopcombination \stopbaselinecorrection
% \stopbuffer
%
% \getbuffer \typebuffer
