%D \module
%D   [       file=m-chart,
%D        version=2011.10.1, % 1998.10.10,
%D          title=\CONTEXT\ Modules,
%D       subtitle=Flow Charts,
%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.

% todo (if used):
%    flowsets
%    autoscaling (bodyfontsteps)
%    comment
%
% todo:
%    \useFLOWchart[name][parent][setting,setting][additional settings]
%    \useFLOWchart[name][parent][additional settings]
%    setups
%    mp instance with less files

\registerctxluafile{m-chart}{}

\unprotect

% todo: figure out a nice way to define the lot: share current and
% support current as name (nb: we need to set parent then)

\installcorenamespace {flowchart} % \def\??flch{@@flch} % chart
\installcorenamespace {flowline}  % \def\??flln{@@flln} % line
\installcorenamespace {flowshape} % \def\??flsh{@@flsh} % shape
\installcorenamespace {flowfocus} % \def\??flfc{@@flfc} % focus
\installcorenamespace {flowsets}  % \def\??flst{@@flst} % sets
\installcorenamespace {flowsplit} % \def\??flsp{@@flsp} % split

\installsimplecommandhandler \??flowchart {FLOWchart} \??flowchart % maybe just a setuphandler
\installsimplecommandhandler \??flowline  {FLOWline}  \??flowline  % maybe just a setuphandler
\installsimplecommandhandler \??flowshape {FLOWshape} \??flowshape % maybe just a setuphandler
\installsimplecommandhandler \??flowfocus {FLOWfocus} \??flowfocus % maybe just a setuphandler
\installsimplecommandhandler \??flowsets  {FLOWsets}  \??flowsets  % maybe just a setuphandler
\installsimplecommandhandler \??flowsplit {FLOWsplit} \??flowsplit % maybe just a setuphandler

\let\setupFLOWcharts\setupFLOWchart
\let\setupFLOWlines \setupFLOWline
\let\setupFLOWshapes\setupFLOWshape
\let\setupFLOWsets  \setupFLOWset

\setupFLOWcharts
  [\c!width=12\bodyfontsize,
   \c!height=7\bodyfontsize,
   \c!offset=\FLOWlineparameter\c!rulethickness,
   \c!dx=2\bodyfontsize,
   \c!dy=2\bodyfontsize,
   \c!nx=0,
   \c!ny=0,
   \c!x=1,
   \c!y=1,
   \c!labeloffset=.5\bodyfontsize,
   \c!commentoffset=.5\bodyfontsize,
   \c!exitoffset=\zeropoint,
 % \c!split=\v!no,
 % \c!option=,
 % \c!bodyfont=,
 % \c!dot=, % private option
 % \c!autofocus=,
 % \c!focus=,
 % \c!background=,
 % \c!framecolor=
 % \c!backgroundcolor=, % \s!white
   \c!rulethickness=.15\bodyfontsize, %\linewidth,
   \c!frame=\v!off]

\setupFLOWlines
  [\c!corner=\v!round,
   \c!arrow=\v!yes,
   \c!dash=\v!no,
   \c!radius=.375\bodyfontsize,
   \c!color=FLOWlinecolor,
   \c!rulethickness=.15\bodyfontsize,
   \c!offset=\zeropoint]

\setupFLOWshapes
  [\c!default=action,
   \c!framecolor=FLOWframecolor,
   \c!background=\v!color,
   \c!backgroundcolor=FLOWbackgroundcolor,
   \c!rulethickness=.15\bodyfontsize,
   \c!offset=.5\bodyfontsize]

\setupFLOWfocus
  [\c!framecolor=FLOWfocuscolor,
   \c!background=\FLOWshapeparameter\c!background,
   \c!backgroundcolor=\FLOWshapeparameter\c!backgroundcolor,
   \c!rulethickness=\FLOWshapeparameter\c!rulethickness,
   \c!offset=\FLOWshapeparameter\c!offset]

\setupFLOWsplit
  [\c!dx=0,
   \c!dy=0,
 % \c!command=,
 % \c!before=,
 % \c!after=,
   \c!nx=3,
   \c!ny=4]

\unexpanded\def\beforeFLOWsplit{\FLOWsplitparameter\c!before}
\unexpanded\def\afterFLOWsplit {\FLOWsplitparameter\c!after}
\unexpanded\def\handleFLOWsplit{\FLOWsplitparameter\c!command}

\definecolor [FLOWfocuscolor]      [s=.2]
\definecolor [FLOWlinecolor]       [s=.5]
\definecolor [FLOWframecolor]      [s=.7]
\definecolor [FLOWbackgroundcolor] [s=.9]

\newtoks\everyFLOWchart

\unexpanded\def\module_charts_process[#name]%
  {\ctxcommand{flow_start_chart("#name")}}

\unexpanded\def\startFLOWchart
  {\startnointerference
   \the\everyFLOWchart
   \dosingleempty\module_charts_start_chart}

\unexpanded\def\module_charts_start_chart[#name]%
  {\ctxcommand{flow_start_chart("#name")}}

\unexpanded\def\stopFLOWchart
  {\ctxcommand{flow_stop_chart()}%
   \stopnointerference}

\unexpanded\def\defineFLOWchart % for old times sake
  {\dodoubleempty\module_charts_FLOW_define}

\unexpanded\def\module_charts_FLOW_define[#name][#settings]#cells% todo: save settings
  {\startnointerference
   \the\everyFLOWchart
   \ctxcommand{flow_start_chart("#name")}%
   #cells%
   \ctxcommand{flow_stop_chart()}%
   \stopnointerference}

\unexpanded\def\startFLOWcell
  {\dodoubleempty\module_charts_start_cell}

\unexpanded\def\module_charts_start_cell[#1][#2]%
  {\begingroup
   \iffirstargument
     \setupFLOWshape[#1]%
   \fi
   \ifsecondargument
     \setupFLOWline[#2]%
   \fi
   \ctxcommand{flow_start_cell {
        shape = {
            rulethickness   = \number\dimexpr\FLOWshapeparameter\c!rulethickness,
            default         = "\FLOWshapeparameter\c!default",
            framecolor      = "\FLOWshapeparameter\c!framecolor",
            backgroundcolor = "\FLOWshapeparameter\c!backgroundcolor",
        },
        focus = {
            rulethickness   = \number\dimexpr\FLOWfocusparameter\c!rulethickness,
            framecolor      = "\FLOWfocusparameter\c!framecolor",
            backgroundcolor = "\FLOWfocusparameter\c!backgroundcolor",
        },
        line = {
            rulethickness   = \number\dimexpr\FLOWlineparameter\c!rulethickness,
            radius          = \number\dimexpr\FLOWlineparameter\c!radius,
            color           = "\FLOWlineparameter\c!color",
            corner          = "\FLOWlineparameter\c!corner",
            dash            = "\FLOWlineparameter\c!dash",
            arrow           = "\FLOWlineparameter\c!arrow",
            offset          = \number\dimexpr\FLOWlineparameter\c!offset,
        },
  } }%
  \endgroup}

\unexpanded\def\stopFLOWcell
  {\ctxcommand{flow_stop_cell()}}

\unexpanded\def\FLOWchart
  {\dodoubleempty\module_charts_process}

\def\module_charts_process[#name][#settings]%
  {\bgroup % \vbox removed
     \insidefloattrue
     \dontcomplain
     \setupFLOWchart[#settings]%
     \usebodyfontparameter\FLOWchartparameter
     \ctxcommand{flow_make_chart {
        chart = {
            name            = "#name",
            option          = "\FLOWchartparameter\c!option",
            backgroundcolor = "\FLOWchartparameter\c!backgroundcolor",
            width           = \number\dimexpr\FLOWchartparameter\c!width,
            height          = \number\dimexpr\FLOWchartparameter\c!height,
            dx              = \number\dimexpr\FLOWchartparameter\c!dx,
            dy              = \number\dimexpr\FLOWchartparameter\c!dy,
            offset          = \number\dimexpr\FLOWchartparameter\c!offset,
          % bodyfont        = "\FLOWchartparameter\c!bodyfont",
            dot             = "\FLOWchartparameter\c!dot", % private option
            hcompact        = "\FLOWchartparameter\c!hcompact", % undocumented option
            vcompact        = "\FLOWchartparameter\c!vcompact", % undocumented option
            focus           = "\FLOWchartparameter\c!focus",
            autofocus       = "\FLOWchartparameter\c!autofocus",
            nx              = "\FLOWchartparameter\c!nx",
            ny              = "\FLOWchartparameter\c!ny",
            x               = "\FLOWchartparameter\c!x",
            y               = "\FLOWchartparameter\c!y",
            labeloffset     = \number\dimexpr\FLOWchartparameter\c!labeloffset,
            commentoffset   = \number\dimexpr\FLOWchartparameter\c!commentoffset,
            exitoffset      = \number\dimexpr\FLOWchartparameter\c!exitoffset,
            split           = "\FLOWchartparameter\c!split",
        },
        shape = {
            rulethickness   = \number\dimexpr\FLOWshapeparameter\c!rulethickness,
            default         = "\FLOWshapeparameter\c!default",
            framecolor      = "\FLOWshapeparameter\c!framecolor",
            backgroundcolor = "\FLOWshapeparameter\c!backgroundcolor",
        },
        focus = {
            rulethickness   = \number\dimexpr\FLOWfocusparameter\c!rulethickness,
            framecolor      = "\FLOWfocusparameter\c!framecolor",
            backgroundcolor = "\FLOWfocusparameter\c!backgroundcolor",
        },
        line = {
            rulethickness   = \number\dimexpr\FLOWlineparameter\c!rulethickness,
            radius          = \number\dimexpr\FLOWlineparameter\c!radius,
            color           = "\FLOWlineparameter\c!color",
            corner          = "\FLOWlineparameter\c!corner",
            dash            = "\FLOWlineparameter\c!dash",
            arrow           = "\FLOWlineparameter\c!arrow",
            offset          = "\FLOWlineparameter\c!offset",
        },
        set = {
        },
        split = {
            nx              = \number\FLOWsplitparameter\c!nx,
            ny              = \number\FLOWsplitparameter\c!ny,
            dx              = \number\FLOWsplitparameter\c!dx,
            dy              = \number\FLOWsplitparameter\c!dy,
            command         = "",
            marking         = "\FLOWsplitparameter\c!marking",
            before          = "",
            after           = "",
        }
     } }%
  \egroup}

\unexpanded\def\FLOWcharts
  {\dodoubleempty\FLOW_charts}

\def\FLOW_charts[#name][#settings]%
  {\begingroup
   \setupFLOWchart[\c!split=\v!yes]%
   \setupFLOWsplit[#settings]%
   \FLOWchart[#name]%
   \endgroup}

\appendtoks
   \let\name       \FLOW_name
   \let\shape      \FLOW_shape
   \let\destination\FLOW_destination
   \let\focus      \FLOW_focus
   \let\overlay    \FLOW_overlay
   \let\location   \FLOW_location
   \let\text       \FLOW_text
   \let\label      \FLOW_label
   \let\comment    \FLOW_comment
   \let\exit       \FLOW_exit
   \let\connection \FLOW_connection
   \let\include    \FLOW_include
   \let\figure     \FLOW_figure
   %
   \let\connect    \FLOW_connection
   \let\locate     \FLOW_location
   %
   \let\includeFLOWchart\include
\to \everyFLOWchart

\unexpanded\def\FLOW_name              #name{\ctxcommand{flow_set_name("#name")}\ignorespaces}
\unexpanded\def\FLOW_shape            #shape{\ctxcommand{flow_set_shape("#shape")}\ignorespaces}
\unexpanded\def\FLOW_destination#destination{\ctxcommand{flow_set_destination("#destination")}\ignorespaces}
\unexpanded\def\FLOW_focus            #focus{\ctxcommand{flow_set_focus("#focus")}\ignorespaces}
\unexpanded\def\FLOW_overlay        #overlay{\ctxcommand{flow_set_overlay("#overlay")}\ignorespaces}
\unexpanded\def\FLOW_location      #location{\ctxcommand{flow_set_location("#location")}\ignorespaces}
\unexpanded\def\FLOW_figure          #figure{\ctxcommand{flow_set_figure("#figure")}\ignorespaces}

\unexpanded\def\FLOW_text      {\dosingleempty\module_charts_FLOW_text}
\unexpanded\def\FLOW_label     {\dosingleempty\module_charts_FLOW_label}
\unexpanded\def\FLOW_comment   {\dosingleempty\module_charts_FLOW_comment}
\unexpanded\def\FLOW_exit      {\dosingleempty\module_charts_FLOW_exit}
\unexpanded\def\FLOW_connection{\dodoubleempty\module_charts_FLOW_connection}
\unexpanded\def\FLOW_include   {\dodoubleempty\module_charts_FLOW_include}

\unexpanded\def\module_charts_FLOW_text                  [#align]#text{\ctxcommand{flow_set_text("#align",\!!bs\detokenize{#text}\!!es)}\ignorespaces}
\unexpanded\def\module_charts_FLOW_label              [#location]#text{\ctxcommand{flow_set_label("#location",\!!bs\detokenize{#text}\!!es)}\ignorespaces}
\unexpanded\def\module_charts_FLOW_comment            [#location]#text{\ctxcommand{flow_set_comment("#location",\!!bs\detokenize{#text}\!!es)}\ignorespaces}
\unexpanded\def\module_charts_FLOW_exit               [#location]#text{\ctxcommand{flow_set_exit("#location",\!!bs\detokenize{#text}\!!es)}\ignorespaces}
\unexpanded\def\module_charts_FLOW_connection[#location][#offset]#name{\ctxcommand{flow_set_connection("#location","#offset","#name")}\ignorespaces}

\unexpanded\def\module_charts_FLOW_include          [#name][#settings]{%
    \begingroup
        \getparameters[FLOWi][\c!x=1,\c!y=1,#settings]%
        \ctxcommand{flow_set_include("#name",\number\FLOWix,\number\FLOWiy,\!!bs\detokenize{#settings}\!!es)}%
    \endgroup
    \ignorespaces
}

\setvariables
  [flowcell:text]
  [n=0,
   x=1,
   y=1,
   text=,
   align=,
   set=\setups{flowcell:text:place}]

\def\FLOWx{\getvariable{flowcell:text}{x}} % compatibility (for Willi)
\def\FLOWy{\getvariable{flowcell:text}{y}} % compatibility (for Willi)

\defineoverlay
  [flowcell:figure]
  [\overlayfigure{\getvariable{flowcell:text}{figure}}]

\startsetups flowcell:text:place
    \begingroup
        \iftrialtypesetting
            \directsetup{flowcell:text:place:indeed}
        \else \iflocation
            \edef\flowcelldestination{\getvariable{flowcell:text}{destination}}%
            \ifx\flowcelldestination\empty
                \directsetup{flowcell:text:place:indeed}
            \else
                % tricky: scaling and moving around is not taken into account
                \letinteractionparameter\c!color\empty
                \letinteractionparameter\c!contrastcolor\empty
                \gotobox{\directsetup{flowcell:text:place:indeed}}[\flowcelldestination]
            \fi
        \else
            \directsetup{flowcell:text:place:indeed}
        \fi \fi
    \endgroup
\stopsetups

\startsetups flowcell:text:place:indeed
    \begingroup
        \directsetup{flowcell:text:user}
        \edef\flowcellfigure {\getvariable{flowcell:text}{figure}}%
        \edef\flowcelloverlay{\getvariable{flowcell:text}{overlay}}%
        \edef\flowcellalign  {\getvariable{flowcell:text}{align}}%
        \edef\flowcellindex  {\getvariable{flowcell:text}{n}}%
        \ifx\flowcellfigure\empty
            \ifx\flowcelloverlay\empty
                \expandcheckedcsname{flowcell:}\flowcellalign\empty
                    % {\getvariable{flowcell:text}{text}}
                      {\getFLOWtext\flowcellindex}
            \else
                \expandcheckedcsname{flowcell:}\flowcellalign\empty
                    [\c!background={\flowcelloverlay}]
                  % {\getvariable{flowcell:text}{text}}
                    {\getFLOWtext\flowcellindex}
            \fi
        \else
            \expandcheckedcsname{flowcell:}\flowcellalign\empty
                [\c!background=flowcell:figure]
              % {\getvariable{flowcell:text}{text}}
                {\getFLOWtext\flowcellindex}
        \fi
    \endgroup
\stopsetups

\unexpanded\def\doFLOWlabel#1#2#3%
  {\begingroup
   \def\FLOWx{#1}%
   \def\FLOWy{#2}%
   \strut
   \getFLOWtext{#3}%
   \endgroup}

\unexpanded\def\getFLOWtext#1%
  {\ifcase#1\else\ctxcommand{flow_get_text(#1)}\fi}

\defineframed % to be discussed: shape or global
  [flowcell:base]
  [\c!offset=\v!overlay, % no strut ?
   \c!frame=\FLOWchartparameter\c!frame,
   \c!background=\FLOWchartparameter\c!background,
   \c!backgroundcolor=\FLOWchartparameter\c!backgroundcolor,
  %\c!foregroundcolor=\FLOWshapeparameter\c!foregroundcolor,
   \c!align=\v!middle,
   \c!bottom=\vfill,
   \c!top=\vfill,
   \c!width=\FLOWchartparameter\c!width,
   \c!height=\FLOWchartparameter\c!height,
 % \c!rulethickness=\FLOWchartparameter\c!rulethickness,
   \c!rulethickness=\zeropoint, % comment for tracing
   \c!framecolor=\FLOWchartparameter\c!framecolor]

\defineframed[flowcell:]  [flowcell:base]
\defineframed[flowcell:l] [flowcell:base][\c!align=\v!flushleft]
\defineframed[flowcell:r] [flowcell:base][\c!align=\v!flushright]
\defineframed[flowcell:m] [flowcell:base][\c!align=\v!middle]
\defineframed[flowcell:c] [flowcell:base][\c!align=\v!middle]

\defineframed[flowcell:t] [flowcell:base][\c!top=]
\defineframed[flowcell:b] [flowcell:base][\c!bottom=]

\defineframed[flowcell:lt][flowcell:base][\c!top=,\c!align=\v!flushleft]
\defineframed[flowcell:rt][flowcell:base][\c!top=,\c!align=\v!flushright]
\defineframed[flowcell:mt][flowcell:base][\c!top=,\c!align=\v!middle]
\defineframed[flowcell:ct][flowcell:base][\c!top=,\c!align=\v!middle]

\defineframed[flowcell:lb][flowcell:base][\c!bottom=,\c!align=\v!flushleft]
\defineframed[flowcell:rb][flowcell:base][\c!bottom=,\c!align=\v!flushright]
\defineframed[flowcell:mb][flowcell:base][\c!bottom=,\c!align=\v!middle]
\defineframed[flowcell:cb][flowcell:base][\c!bottom=,\c!align=\v!middle]

% \startsetups flowcell:text:user
%     \setupframed
%       [flowcell:base]
%       [background=flowcell]
%     \definelayer
%       [flowcell]
%       [width=\namedframedparameter{flowcell:base}{width},
%        height=\namedframedparameter{flowcell:base}{height}]
%     \setlayerframed
%       [flowcell]
%       [preset=rightbottom,offset=-2.75ex]
%       [frame=off]
%       {\tx\FLOWx.\FLOWy}
% \stopsetups
%
% % or:
%
% \setupframed
%   [flowcell:base]
%   [background={flowcell-1,flowcell-2}]
%
% \defineoverlay
%   [flowcell-1]
%   [\directsetup{flowcell-1}]
%
% \definelayer
%   [flowcell-2]
%   [width=\overlaywidth,
%    height=\overlayheight]
%
% \startsetups flowcell-1
%     \setlayerframed
%       [flowcell-2]
%       [preset=rightbottom,offset=-2.75ex]
%       [frame=off]
%       {\tx\FLOWx.\FLOWy}
% \stopsetups

% %D \starttyping
% %D \setupFLOWsplit
% %D   [nx=5,ny=10,
% %D    dx=0,dy=0,
% %D    before=,
% %D    after=\page]
% %D
% %D \FLOWcharts[mybigflow]
% %D \stoptyping
% %D
% %D \starttyping
% %D \splitfloat
% %D   {\placefigure{What a big flowchart this is!}}
% %D   {\FLOWcharts[mybigflow]}
% %D \stoptyping

% \setupFLOWsplit
%    [nx=5,
%     ny=8,
%     dx=1,
%     dy=1,
%     command=\framed,
%     before=\page,
%     after=\page]
%
% \FLOWchart[demo]                     \page
% \FLOWchart[demo][split=yes]          \page
% \FLOWchart[demo][x=1,y=1,nx=5,ny=8]  \page
% \FLOWchart[demo][x=1,y=9,nx=5,ny=10] \page

\protect

\continueifinputfile{m-chart.mkvi}

\input chrt-xml.tex

\usemodule[abr-01]

\setupFLOWcharts[option=test]
\setupFLOWcharts[frame=on]
\setupinteraction[state=start]

% \setupFLOWcharts[dx=30pt,dy=30pt]

\startMPinclusions
    predefined_shapes[101] := fullcircle ;
\stopMPinclusions

\startFLOWchart[demo]
  \startFLOWcell[framecolor=darkgray]
    \name       {start}
    \location   {1,1}
    \shape      {action}
    \text       {start}
    \connection [bl] {one}
  \stopFLOWcell
  \startFLOWcell[framecolor=darkred]
    \name       {one}
%     \destination{CloseDocument}
    \location   {2,2}
%     \shape      {action}
    \shape      {101}
    \text       {first}
%     \label      [b] {\bfx bottom}
    \connection [rt] {two}
%     \exit       [l] {exit l}
%     \exit       [r] {exit r}
%     \exit       [t] {exit t}
%     \exit       [b] {exit b}
  \stopFLOWcell
  \setupFLOWshapes[framecolor=darkgray]
  \startFLOWcell % [foregroundcolor=white]
    \name       {two}
%     \destination{CloseDocument}
    \location   {3,3}
    \shape      {action}
    \text       {second}
    \figure     {cow.pdf}
%     \label      [l] {\bfx left}
%     \exit       [l] {exit l}
%     \exit       [r] {exit r}
%     \exit       [t] {exit t}
%     \exit       [b] {exit b}
  \stopFLOWcell
\stopFLOWchart

\starttext

\startTEXpage
%     \FLOWchart[convert-en]
%     \FLOWchart[conversion 1]
%     \FLOWchart[conversion 7]
%     \FLOWchart[conversion 9]
%     \FLOWchart[conversion 10]
    \FLOWchart[demo]

\stopTEXpage

\startTEXpage
    \FLOWchart[conversion 10]
\stopTEXpage

\stoptext
