%D \module
%D   [       file=mp-cell.mpiv, % mp-step.mpiv,
%D        version=2010.10.07,   % 2001.05.22,
%D          title=\CONTEXT\ \METAPOST\ graphics,
%D       subtitle=steps,
%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 licen-en.pdf for
%C details.

% maybe todo: step prefixes .. no save needed
% not todo  : make it unreadable by lots of suffix compaction

if known context_step : endinput ; fi ;

boolean context_step ; context_step := true ;

def initialize_step_variables =
    save
        text_fill_color, text_line_color, text_line_width, text_offset,
        cell_fill_color, cell_line_color, cell_line_width, cell_offset,
        line_line_color, line_line_width, line_alternative,
        line_distance, cell_distance_y, cell_distance_x,
        nofcells, chart_vertical, chart_align, chart_category ;

    string  chart_category      ; chart_category      := "" ;

    string  text_line_color     ; text_line_color     := "red" ;
    string  cell_line_color     ; cell_line_color     := "blue" ;
    string  line_line_color     ; line_line_color     := "green" ;

    string  text_fill_color     ; text_fill_color     := "white" ;
    string  cell_fill_color     ; cell_fill_color     := "white" ;

    numeric text_line_width     ; text_line_width     := 2pt ;
    numeric cell_line_width     ; cell_line_width     := 2pt ;
    numeric line_line_width     ; line_line_width     := 2pt  ;

    numeric text_offset         ; text_offset         := 4pt ;
    numeric cell_offset         ; cell_offset         := 4pt ;

    numeric line_distance       ; line_distance       := 10pt ; % between line and text
    numeric line_offset         ; line_offset         :=  4pt ; % between center and start of line
    numeric line_height         ; line_height         := 20pt ;

    numeric cell_distance_y     ; cell_distance_y     := 20pt ;
    numeric cell_distance_x     ; cell_distance_x     := 20pt ;

    numeric text_distance_set   ; text_distance_set   :=  4pt ;

    boolean chart_vertical      ; chart_vertical      := false ;
    boolean chart_align         ; chart_align         := false ;

    numeric nofcells            ; nofcells            := 0 ;

enddef ;

def step_cells (expr t, b) =
    nofcells := nofcells + 1 ;
    cells_t[nofcells] := texbox.d(chart_category,t) ;
    cells_b[nofcells] := texbox.d(chart_category,b) ;
    texts_t[nofcells] := nullpicture ;
    texts_m[nofcells] := nullpicture ;
    texts_b[nofcells] := nullpicture ;
enddef ;

def step_texts (expr t, m, b) =
    texts_t[nofcells] := texbox.d(chart_category,t) ;
    texts_m[nofcells] := texbox.d(chart_category,m) ;
    texts_b[nofcells] := texbox.d(chart_category,b) ;
enddef ;

def step_begin_cell =
    nofcells := nofcells + 1 ;
    cells_t[nofcells] := nullpicture ;
    cells_b[nofcells] := nullpicture ;
    texts_t[nofcells] := nullpicture ;
    texts_m[nofcells] := nullpicture ;
    texts_b[nofcells] := nullpicture ;
enddef ;

def step_end_cell =
enddef ;

% maybe: texbox.d

def step_cell_top (expr t, c, f, l, s) =
    cells_t[nofcells] := texbox.d(chart_category,t) ;
    cell_top_colors[nofcells] := c ;
    cell_top_fills [nofcells] := f ;
    cell_top_lines [nofcells] := l ;
    cell_top_shapes[nofcells] := s ;
enddef ;

def step_cell_bot (expr b, c, f, l, s) =
    cells_b[nofcells] := texbox.d(chart_category,b) ;
    cell_bot_colors[nofcells] := c ;
    cell_bot_fills [nofcells] := f ;
    cell_bot_lines [nofcells] := l ;
    cell_bot_shapes[nofcells] := s ;
enddef ;

def step_cell_ali (expr ca, cb, cc, c, f, l, s) =
    cells_a[nofcells][1] := texbox.d(chart_category,ca) ;
    cells_a[nofcells][2] := texbox.d(chart_category,cb) ;
    cells_a[nofcells][3] := texbox.d(chart_category,cc) ;
    cell_top_colors[nofcells] := c ;
    cell_top_fills [nofcells] := f ;
    cell_top_lines [nofcells] := l ;
    cell_top_shapes[nofcells] := s ;
enddef ;

def step_text_top (expr t, tc, tf, tl, ts, lc, ll, ls) =
    texts_t[nofcells] := texbox.d(chart_category,t) ;
    text_top_colors[nofcells] := tc ;
    text_top_fills [nofcells] := tf ;
    text_top_lines [nofcells] := tl ;
    text_top_shapes[nofcells] := ts ;
    line_top_colors[nofcells] := lc ;
    line_top_lines [nofcells] := ll ;
    line_top_shapes[nofcells] := ls ;
enddef ;

def step_text_mid (expr m, tc, tf, tl, ts, lc, ll, ls) =
    texts_m[nofcells] := texbox.d(chart_category,m) ;
    text_mid_colors[nofcells] := tc ;
    text_mid_fills [nofcells] := tf ;
    text_mid_lines [nofcells] := tl ;
    text_mid_shapes[nofcells] := ts ;
    line_mid_colors[nofcells] := lc ;
    line_mid_lines [nofcells] := ll ;
    line_mid_shapes[nofcells] := ls ;
enddef ;

def step_text_bot (expr b, tc, tf, tl, ts, lc, ll, ls) =
    texts_b[nofcells] := texbox.d(chart_category,b) ;
    text_bot_colors[nofcells] := tc ;
    text_bot_fills [nofcells] := tf ;
    text_bot_lines [nofcells] := tl ;
    text_bot_shapes[nofcells] := ts ;
    line_bot_colors[nofcells] := lc ;
    line_bot_lines [nofcells] := ll ;
    line_bot_shapes[nofcells] := ls ;
enddef ;

def step_begin_chart =
    begingroup ;
        initialize_step_variables ;
        save nofcells ; numeric nofcells ; nofcells := 0 ;
        save cells_t, cells_m, cells_b ; picture cells_t[], cells_m[], cells_b[] ;
        save texts_t, texts_m, texts_b ; picture texts_t[], texts_m[], texts_b[] ;
        save start_t, start_m, start_b ; numeric start_t[], start_m[], start_b[] ;
        save cells_a ; picture cells_a[][] ;

        save cell_top_colors ; string cell_top_colors[] ;
        save cell_bot_colors ; string cell_bot_colors[] ;
        save text_top_colors ; string text_top_colors[] ;
        save text_mid_colors ; string text_mid_colors[] ;
        save text_bot_colors ; string text_bot_colors[] ;

        save cell_top_fills  ; string cell_top_fills[] ;
        save cell_bot_fills  ; string cell_bot_fills[] ;
        save text_top_fills  ; string text_top_fills[] ;
        save text_mid_fills  ; string text_mid_fills[] ;
        save text_bot_fills  ; string text_bot_fills[] ;

        save cell_top_lines  ; numeric cell_top_lines[] ;
        save cell_bot_lines  ; numeric cell_bot_lines[] ;
        save text_top_lines  ; numeric text_top_lines[] ;
        save text_mid_lines  ; numeric text_mid_lines[] ;
        save text_bot_lines  ; numeric text_bot_lines[] ;

        save cell_top_shapes ; numeric cell_top_shapes[] ;
        save cell_bot_shapes ; numeric cell_bot_shapes[] ;
        save text_top_shapes ; numeric text_top_shapes[] ;
        save text_mid_shapes ; numeric text_mid_shapes[] ;
        save text_bot_shapes ; numeric text_bot_shapes[] ;

        save line_top_lines  ; numeric line_top_lines[] ;
        save line_mid_lines  ; numeric line_mid_lines[] ;
        save line_bot_lines  ; numeric line_bot_lines[] ;

        save line_top_colors ; string line_top_colors[] ;
        save line_mid_colors ; string line_mid_colors[] ;
        save line_bot_colors ; string line_bot_colors[] ;

        save line_top_shapes ; numeric line_top_shapes[] ;
        save line_mid_shapes ; numeric line_mid_shapes[] ;
        save line_bot_shapes ; numeric line_bot_shapes[] ;
enddef ;

def step_end_chart =
    % we could combine some loops but this is cleaner
    save dx, delta ; numeric dx, delta ;
    save p ; path p ;
    save one_row_only ; boolean one_row_only ;
    save cell_t, next_t, text_t ; picture cell_t, next_t, text_t ;
    save cell_m, next_m, text_m ; picture cell_m, next_m, text_m ;
    save cell_b, next_b, text_b ; picture cell_b, next_b, text_b ;
    save height_t, width_t, max_height_t, max_width_t ; numeric height_t, width_t, max_height_t, max_width_t ;
    save height_m, width_m, max_height_m, max_width_m ; numeric height_m, width_m, max_height_m, max_width_m ;
    save height_b, width_b, max_height_b, max_width_b ; numeric height_b, width_b, max_height_b, max_width_b ;
    save alternative ; numeric alternative ;
    % check rows
    one_row_only := true ;
    for i=1 upto nofcells :
        if bbwidth(cells_b[i]) > 0 :
            one_row_only := false ;
        fi ;
    endfor ;
    %
    if chart_align :
        save size, delta, width ; numeric size[], delta[], width[] ;
        for i=1 upto 3:
            size[i] = 0 ;
            for c=1 upto nofcells :
                size[i] := max(size[i],bbwidth(cells_a[c][i])) ;
            endfor ;
        endfor ;
        for c=1 upto nofcells :
            cells_t[c] := image (
                for i=1 upto 3:
                    width[i] := bbwidth(cells_a[c][i]);
                    delta[i] := size[i] - width[i] ;
                endfor;
                setbounds cells_a[c][1] to boundingbox(cells_a[c][1]) leftenlarged (delta[1]) ;
                setbounds cells_a[c][2] to boundingbox(cells_a[c][2]) leftenlarged (delta[2]/2)
                                                                      rightenlarged(delta[2]/2) ;
                setbounds cells_a[c][3] to boundingbox(cells_a[c][3]) rightenlarged(delta[3]) ;
                cells_a[c][1] := cells_a[c][1] shifted (- width[1]/2 - size[2]/2 - text_distance_set,0) ;
                cells_a[c][3] := cells_a[c][3] shifted (  width[3]/2 + size[2]/2 + text_distance_set,0) ;
                for i=1 upto 3:
                    draw cells_a[c][i] ;
                  % draw boundingbox cells_a[c][i] ;
                endfor ;
            ) ;
        endfor ;
    fi ;
    % swap and rotate
    if chart_vertical :
        if one_row_only :
            % deal with mid_texts
            max_width_t := max_width_m := max_width_b := 0 ;
            for i=1 upto nofcells :
                max_width_t := max(max_width_t,bbwidth(texts_t[i]));
                max_width_m := max(max_width_m,bbwidth(texts_m[i]));
                max_width_b := max(max_width_b,bbwidth(texts_b[i]));
            endfor ;
            if max_width_m > 0 :
                for i=1 upto nofcells :
                    text_t := texts_t[i] ; width_t := bbwidth(text_t) ;
                    text_m := texts_m[i] ; width_m := bbwidth(text_m) ;
                    text_b := texts_b[i] ; width_b := bbwidth(text_b) ;
                    if width_t < max_width_t :
                        setbounds text_t to boundingbox text_t
                            leftenlarged (max_width_t - width_t) ;
                    fi ;
                    if width_m < max_width_m :
                        setbounds text_m to boundingbox text_m
                            leftenlarged  ((max_width_m - width_m)/2)
                            rightenlarged ((max_width_m - width_m)/2) ;
                    fi ;
                    if width_b < max_width_b :
                        setbounds text_b to boundingbox text_b
                            rightenlarged (max_width_b - width_b) ;
                    fi ;
                    text_t := text_t shifted (- xpart llcorner text_t, 0) ;
                    text_m := text_m shifted (- xpart llcorner text_m, 0) ;
                    text_b := text_b shifted (- xpart llcorner text_b, 0) ;
                    texts_t[i] := image (
                        draw text_t ;
                        draw text_m shifted (max_width_t +                 text_distance_set,0) ;
                        draw text_b shifted (max_width_t + max_width_m + 2*text_distance_set,0) ;
                    ) rotated 90 ;
                    texts_m[i] := texts_b[i] := nullpicture ;
                    cells_t[i] := cells_t[i] rotated 90 ;
                endfor ;
            else :
                for i=1 upto nofcells :
                    cells_t[i] := cells_t[i] rotated 90 ;
                    texts_t[i] := texts_t[i] rotated 90 ;
                    texts_b[i] := texts_b[i] rotated 90 ;
                endfor ;
            fi ;
        else :
            for i=1 upto nofcells :
                % swaps so we need a scratch variable
                cell_t := cells_t[i] ;
                cell_b := cells_b[i] ;
                cells_t[i] := cell_b rotated 90 ;
                cells_b[i] := cell_t rotated 90 ;
                text_t := texts_t[i] ;
                text_b := texts_b[i] ;
                texts_t[i] := text_b rotated 90 ;
                texts_b[i] := text_t rotated 90 ;
            endfor ;
        fi ;
    fi ;
    % align horizontal
    for i=1 upto nofcells :
        width_t := bbwidth(cells_t[i]) ;
        width_b := bbwidth(cells_b[i]) ;
        if (width_t = 0) and (width_b = 0) :
            % skip
        elseif (width_t > 0) and (width_t < width_b) :
            delta := (width_b-width_t)/2 ;
            setbounds cells_t[i] to boundingbox cells_t[i] leftenlarged delta rightenlarged delta ;
        elseif (width_b > 0) and (width_t > width_b) :
            delta := (width_t-width_b)/2 ;
            setbounds cells_b[i] to boundingbox cells_b[i] leftenlarged delta rightenlarged delta ;
        fi ;
    endfor ;
    % analyze vertical
    max_height_t := 0 ;
    max_height_b := 0 ;
    for i=1 upto nofcells :
        height_t := bbheight(cells_t[i]) ;
        height_b := bbheight(cells_b[i]) ;
        if height_t > 0 :
            setbounds cells_t[i] to boundingbox cells_t[i] enlarged cell_offset ;
            height_t := height_t + 2 * cell_offset ;
        fi ;
        if height_b > 0 :
            setbounds cells_b[i] to boundingbox cells_b[i] enlarged cell_offset ;
            height_b := height_b + 2 * cell_offset ;
        fi ;
        if height_t > max_height_t :
            max_height_t := height_t ;
        fi
        if height_b > max_height_b :
            max_height_b := height_b ;
        fi ;
    endfor ;
    % align vertical
    for i=1 upto nofcells :
        height_t := bbheight(cells_t[i]) ;
        height_b := bbheight(cells_b[i]) ;
        if height_t > 0 :
            delta := (max_height_t-height_t)/2 ;
            setbounds cells_t[i] to boundingbox cells_t[i] topenlarged delta bottomenlarged delta ;
        fi ;
        if height_b > 0 :
            delta := (max_height_b-height_b)/2 ;
            setbounds cells_b[i] to boundingbox cells_b[i] topenlarged delta bottomenlarged delta ;
        fi ;
    endfor ;
    % position
    dx := 0 ;
    for i=1 upto nofcells :
        cells_t[i] := cells_t[i] shifted -llcorner cells_t[i] shifted (dx, 0) ;
        cells_b[i] := cells_b[i] shifted -llcorner cells_b[i] shifted (dx,-cell_distance_y-max_height_b) ;
        width_t := bbwidth(cells_t[i]) ;
        width_b := bbwidth(cells_b[i]) ;
        if width_t > 0 :
            dx := dx + cell_distance_x + width_t ;
        elseif width_b > 0 :
            dx := dx + cell_distance_x + width_b ;
        fi ;
    endfor ;
    % flush
    for i=1 upto nofcells :
        width_t := bbwidth(cells_t[i]) ;
        width_b := bbwidth(cells_b[i]) ;
        if width_t > 0 :
            drawpredefinedshape (
                if known cell_top_shapes[i] : cell_top_shapes[i] else : 24 fi,
                cells_t[i],
                if known cell_top_lines [i] : cell_top_lines [i] else : cell_line_width fi,
                if known cell_top_colors[i] : cell_top_colors[i] else : cell_line_color fi,
                if known cell_top_fills [i] : cell_top_fills [i] else : cell_fill_color fi
            ) ;
            draw cells_t[i] ;
        fi ;
        if width_b > 0 :
            drawpredefinedshape (
                if known cell_bot_shapes[i] : cell_bot_shapes[i] else : 24 fi,
                cells_b[i],
                if known cell_bot_lines [i] : cell_bot_lines [i] else : cell_line_width fi,
                if known cell_bot_colors[i] : cell_bot_colors[i] else : cell_line_color fi,
                if known cell_bot_fills [i] : cell_bot_fills [i] else : cell_fill_color fi
            ) ;
            draw cells_b[i] ;
        fi ;
    endfor ;
    %
    def midtopboundary    expr p = 0.5[ulcorner boundingbox p, urcorner boundingbox p] enddef ;
    def midbottomboundary expr p = 0.5[llcorner boundingbox p, lrcorner boundingbox p] enddef ;
    % draw top and bottom text boxes
    for i=1 upto nofcells-1 :
        if bbwidth(texts_t[i]) > 0 :
            setbounds texts_t[i] to boundingbox texts_t[i] enlarged text_offset ;
        fi ;
        if bbwidth(texts_b[i]) > 0 :
            setbounds texts_b[i] to boundingbox texts_b[i] enlarged text_offset ;
        fi ;
    endfor ;
    % arrows
    for i=1 upto nofcells-1 :
        pair t_a, t_b, t_c, b_a, b_b, b_c ;
        t_a := midtopboundary cells_t[i] ;
        t_b := midtopboundary cells_t[i+1] ;
        if one_row_only :
            b_a := midbottomboundary cells_t[i] ;
            b_b := midbottomboundary cells_t[i+1] ;
        else :
            b_a := midbottomboundary cells_b[i] ;
            b_b := midbottomboundary cells_b[i+1] ;
        fi ;
        t_c := (xpart 0.5[t_a,t_b], ypart t_a+line_height+line_distance) ;
        b_c := (xpart 0.5[b_a,b_b], ypart b_a-line_height-line_distance) ;
        texts_t[i] := thelabel.top(texts_t[i],t_c) ;
        texts_b[i] := thelabel.bot(texts_b[i],b_c) ;
    endfor ;
    %
    for i=1 upto nofcells-1 : % todo arrows when empty text
        text_t := texts_t[i] ;
        text_b := texts_b[i] ;
        cell_t := cells_t[start_t[i]] ;
        cell_b := cells_b[start_b[i]] ;
        next_t := cells_t[i+1] ;
        next_b := cells_b[i+1] ;
        if bbwidth(text_t) > 0 :
            if bbwidth(cell_t) > 0 :
                alternative := if known line_top_shapes[i] : line_top_shapes[i] else : 1 fi ;
                if alternative <> 0 :
                    if (alternative = 1) or (alternative = 2) or (alternative = 5) or (alternative = 6) :
                        drawarrow
                    elseif (alternative = 3) or (alternative = 7):
                        drawdblarrow
                    else :
                        draw
                    fi
                        if (alternative = 2) or (alternative = 6) :
                            reverse
                        fi
                            (
                                midtopboundary cell_t
                                    shifted (if i > 1 : line_offset else : 0 fi, cell_line_width) {up}  ..
                                midbottomboundary text_t
                                    shifted (0,-line_distance) ..
                                {down} midtopboundary next_t
                                    shifted (if i < nofcells - 1 : -line_offset else : 0 fi,cell_line_width)
                            )
                        withpen pencircle scaled
                            if known line_top_lines [i] : line_top_lines [i] else : line_line_width fi
                        withcolor
                            if known line_top_colors[i] : line_top_colors[i] else : line_line_color fi
                    if (alternative >= 5) and (alternative <= 8) :
                        dashed evenly scaled (
                            if known line_top_lines [i] : line_top_lines [i] else : line_line_width fi
                        )
                    fi
                    ;
                fi ;
            fi ;
        fi ;
        if bbwidth(text_b) > 0 :
            if one_row_only :
                cell_b := cell_t ;
                next_b := next_t ;
            fi ;
            if bbwidth(cell_b) > 0 :
                alternative := if known line_bot_shapes[i] : line_bot_shapes[i] else : 1 fi ;
                if alternative <> 0 :
                    if (alternative = 1) or (alternative = 2) or (alternative = 5) or (alternative = 6) :
                        drawarrow
                    elseif (alternative = 3) or (alternative = 7):
                        drawdblarrow
                    else :
                        draw
                    fi
                        if (alternative = 2) or (alternative = 6) :
                            reverse
                        fi
                            (
                                midbottomboundary cell_b
                                    shifted (if i > 1 : line_offset else : 0 fi, -cell_line_width) {down}  ..
                                midtopboundary text_b
                                    shifted (0, line_distance) ..
                                {up} midbottomboundary next_b
                                    shifted (if i < nofcells - 1 : -line_offset else : 0 fi,-cell_line_width)
                            )
                        withpen pencircle scaled
                            if known line_bot_lines [i] : line_bot_lines [i] else : line_line_width fi
                        withcolor
                            if known line_bot_colors[i] : line_bot_colors[i] else : line_line_color fi
                    if (alternative >= 5) and (alternative <= 8) :
                        dashed evenly scaled (
                            if known line_top_lines [i] : line_top_lines [i] else : line_line_width fi
                        )
                    fi
                    ;
                fi ;
            fi ;
        fi ;
    endfor ;
    % draw top and bottom text boxes
    for i=1 upto nofcells-1 :
        if bbwidth(texts_t[i]) > 0 :
            drawpredefinedshape (
                if known text_top_shapes[i] : text_top_shapes[i] else : 24 fi,
                texts_t[i],
                if known text_top_lines [i] : text_top_lines [i] else : text_line_width fi,
                if known text_top_colors[i] : text_top_colors[i] else : text_line_color fi,
                if known text_top_fills [i] : text_top_fills [i] else : text_fill_color fi
            ) ;
            draw texts_t[i] ;
        fi ;
        if bbwidth(texts_b[i]) > 0 :
            drawpredefinedshape (
                if known text_bot_shapes[i] : text_bot_shapes[i] else : 24 fi,
                texts_b[i],
                if known text_bot_lines [i] : text_bot_lines [i] else : text_line_width fi,
                if known text_bot_colors[i] : text_bot_colors[i] else : text_line_color fi,
                if known text_bot_fills [i] : text_bot_fills [i] else : text_fill_color fi
            ) ;
            draw texts_b[i] ;
        fi ;
    endfor ;
    if chart_vertical :
        % rotate back
        currentpicture := currentpicture rotated -90 ;
    fi ;
    endgroup ;
enddef ;

% no longer working .. will do someday
%
% start_begin_step ;
%     step_cells ("\strut test 0", "\strut test 0") ;
%     step_cells ("\strut test 1", "\vbox{\hsize3cm \strut oeps 1\crlf oeps 1}") ;
%     step_texts ("\strut 1",    "\strut 1") ;
%     step_cells ("\strut test 2", "\strut oeps 2 oeps 2") ;
%     step_cells ("\strut test X", "\strut test X") ;
%     step_texts ("\strut 2",    "\strut 2") ;
%     step_cells ("\strut test 3", "\strut oeps 3 oeps 3") ;
%     step_texts ("\strut 3",    "\strut 3") ;
%     step_cells ("\strut test 4", "\strut oeps 4 oeps 4") ;
%     step_texts ("\strut 4",    "\strut 4") ;
% stop_end_chart ;
