%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.

\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

\let\lasttypesetbuffer\empty

\unexpanded\def\typesetbuffer
  {\bgroup
   \setconstant\c_grph_buffers_mode\plusone
   \dodoubleempty\grph_buffers_typeset}

\unexpanded\def\typesetbufferonly
  {\bgroup
   \setconstant\c_grph_buffers_mode\zerocount
   \dodoubleempty\grph_buffers_typeset}

\def\grph_buffers_typeset[#1][#2]% beware: this will mix up the mp graphics
  {\ifsecondargument
     \grph_buffers_typeset_indeed[#1][#2]%
   \else\iffirstargument
     \doifelseassignment{#1}
       {\grph_buffers_typeset_indeed[\jobname][#1]}%
       {\grph_buffers_typeset_indeed[#1][]}%
   \else
     \grph_buffers_typeset_indeed[\jobname][]
   \fi\fi}

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

\unexpanded\def\runbuffer % for now
  {\dotripleempty\grph_buffers_run_indeed}

\def\grph_buffers_run_indeed[#1][#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}

\unexpanded\def\definetypesetting{\dotripleempty\grph_typesetting_define}
\unexpanded\def\typesetfile      {\dotripleempty\grph_typesetting_process}

\def\grph_typesetting_define[#1][#2][#3]% <name> options settings-a
  {\doifsomething{#1}{\setuvalue{\??typesettingfile#1}{\grph_typesetting_process_indeed{#2}{#3}}}}

\def\grph_typesetting_process[#1][#2][#3]% <name> filename settings-b | filename options settings
  {\ifcsname\??typesettingfile#1\endcsname
     \lastnamedcs{#2}{#3}%
   \else\ifthirdargument % filename options settings
     \grph_typesetting_process_indeed{#2}{#3}{#1}{}%
   \else\ifsecondargument % filename settings
     \grph_typesetting_process_indeed{}{#2}{#1}{}%
   \fi\fi\fi}

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

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

%D Whatever ... hardly used ... but historic ... needs checking ...
%D will probably become m-fig-nn.mkiv .. or I will extend it cq. clean
%D it up when I needed it. After all, it's documented in old manuals.

\newcount\c_grph_steps_reference
\newdimen\d_grph_steps_x
\newdimen\d_grph_steps_y
\newbox  \b_grph_steps_colorbar

\let\placestopfigure\relax

\unexpanded\def\placestartfigure[#1][#2][#3]#4\placestopfigure[#5]%
  {\hpack
     {\setbox\scratchbox\hpack
        {\useexternalfigure[\s!dummy][#2][#3,#5]%
         \externalfigure[\s!dummy]}%
      \grph_steps_calculate
      \startpositioning
        \let\referring\grph_steps_one_referring
        \let\marking  \grph_steps_one_marking
        \let\remark   \grph_steps_one_remark
        \let\colorbar \grph_steps_one_colorbar
        \position(0,0){\box\scratchbox}%
        \linewidth\onepoint
        \setuppositioning
          [\c!unit=pt,
           \c!xscale=\withoutpt\the\d_grph_steps_x,
           \c!yscale=\withoutpt\the\d_grph_steps_y,
           \c!factor=1]%
        \ignorespaces#4%
        \let\referring\grph_steps_two_referring
        \let\marking  \grph_steps_two_marking
        \let\remark   \grph_steps_two_remark
        \let\colorbar \grph_steps_two_colorbar
        \ignorespaces#4%
      \stoppositioning
      \ifvoid\b_grph_steps_colorbar\else\box\b_grph_steps_colorbar\fi}} % not really needed

\unexpanded\def\grph_steps_one_referring(#1,#2)#3(#4,#5)#6[#7]%
  {\position(#1,#2){\grph_steps_goto(#4,#5){\externalfigureparameter\c!frames}[#7]}}

\unexpanded\def\grph_steps_one_marking(#1,#2)#3(#4,#5)#6[#7]%
  {\position(#1,#2){\grph_steps_this_is(#4,#5){\externalfigureparameter\c!frames}[#7]}}

\unexpanded\def\grph_steps_one_remark(#1,#2)#3(#4,#5)%    (x,y)(h,b)[...]{tekst}
  {\def\grph_steps_no_comment_indeed[##1]##2{}%
   \dosingleempty\grph_steps_no_comment_indeed}

\unexpanded\def\grph_steps_one_colorbar#1[#2]%
  {}

\unexpanded\def\grph_steps_two_referring(#1,#2)#3(#4,#5)#6[#7]%
  {}

\let\grph_steps_two_marking\grph_steps_two_referring

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

\unexpanded\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}

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

\unexpanded\def\startfigure % could be done nicer
  {\grabuntil{\e!stop\v!figure}\grph_steps_figure_process}

\let\stopfigure\relax

\def\grph_steps_figure_process#1%
  {\dotripleargument\grph_steps_figure_process_indeed#1\stopfigure}

\def\grph_steps_figure_process_indeed[#1][#2][#3]#4\stopfigure
  {\doifelse{\externalfigureparameter\c!option}\v!test
     {\teststartfigure[#1][#2][#3]#4\teststopfigure
      \letexternalfigureparameter\c!frames\v!on}
     {\letexternalfigureparameter\c!frames\v!off}%
   \setvalue{\??externalfigureinstance#1}%
     {\dosingleempty{\placestartfigure[#1][#2][#3]#4\placestopfigure}}}

\let\teststopfigure\relax

\unexpanded\def\teststartfigure[#1][#2][#3]#4\teststopfigure
  {\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
     \setbox0\vpack
       {\hsize240\points
        \startpositioning
          \grph_steps_calculate
          \position(0,0)
            {\box\scratchbox}%
          \position(0,0)
            {\basegrid
               [\c!nx=\externalfigureparameter\c!xmax,
                \c!dx=\withoutpt\the\d_grph_steps_x,
                \c!ny=\externalfigureparameter\c!ymax,
                \c!dy=\withoutpt\the\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=\withoutpt\the\d_grph_steps_x,
             \c!yscale=\withoutpt\the\d_grph_steps_y,
             \c!factor=1]%
          \linewidth\onepoint
          \ignorespaces#4\relax
        \stoppositioning
        \vfill}%
     \c_grph_steps_reference\zerocount
     \let\referring\grph_steps_four_referring
     \let\marking  \grph_steps_four_marking
     \let\remark   \grph_steps_four_remark
     \let\colorbar \grph_steps_four_colorbar
     \setbox2\vbox % \vpack ?
       {{\tfa\doifelsenothing{#1}{#2}{#1}}
        \blank
        \tfxx#4
        \vfilll}%
     \ifdim\ht0>\ht2
       \ht2\ht0
     \else
       \ht0\ht2
     \fi
     \hpack
       {\hskip3\emwidth
        \tpack{\vskip12\points\box0\vskip6\points}%
        \tpack{\vskip12\points\box2\vskip6\points}}%
   \endgroup}

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

\unexpanded\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

\unexpanded\def\grph_steps_four_referring
  {\grph_steps_text{\normalstartimath\rightarrow\normalstopimath}}

\unexpanded\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
     \divide\d_grph_steps_y \externalfigureparameter\c!ymax
     \d_grph_steps_x\d_grph_steps_y
     \scratchdimen\figurewidth
     \advance\scratchdimen\d_grph_steps_y
     \divide \scratchdimen\d_grph_steps_y
     \setexternalfigureparameter\c!xmax{\number\scratchdimen}%
   \else
     \d_grph_steps_x\figurewidth  \divide\d_grph_steps_x \externalfigureparameter\c!xmax\relax
     \d_grph_steps_y\figureheight \divide\d_grph_steps_y \externalfigureparameter\c!ymax\relax
   \fi}

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

% \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]}

\def\grph_steps_text#1(#2,#3)#4(#5,#6)#7[#8]%
  {\advance\c_grph_steps_reference\plusone
   \hbox % \hpack ?
     {\quad
      \thisissomeinternal\s!vwb
        {#8}%
      \gotosomeinternal\s!vwa{#8}\realfolio
        {\hbox to 1.5em{\the\c_grph_steps_reference\presetgoto\hfill}}%
      \quad#1 (#2,#3) (#5,#6) [#8]\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
