%D \module
%D   [       file=mp-apos.mpiv,
%D        version=2012.02.19, % was mp-core: 1999.08.01, anchoring
%D          title=\CONTEXT\ \METAPOST\ graphics,
%D       subtitle=anchored background macros,
%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.

if known context_apos : endinput ; fi ;

boolean context_apos ; context_apos := true ;

path    posboxes[],
        posregions[] ;

numeric multipages[],
        nofposboxes ;

nofposboxes := 0 ;

def boxlineoptions = withcolor .8blue  enddef ;
def boxfilloptions = withcolor .8white enddef ;

def connect_positions =
    if nofposboxes = 2 :
        pickup pencircle scaled boxlinewidth ;
        path pa ; pa := posboxes[1] enlarged boxlineoffset ;
        path pb ; pb := posboxes[2] enlarged boxlineoffset ;
        if pospages[1] = pospages[2] :
            draw posboxes[1]  boxlineoptions ;
            path pc ; pc := center pa {up} .. {down} center pb ;
            pair cc ; cc := (pc intersection_point pa) ;
            if intersection_found :
                pc := pc cutbefore cc ;
                cc := (pc intersection_point pb) ;
                if intersection_found :
                    pc := pc cutafter cc ;
                    drawarrow pc boxlineoptions ;
                    drawarrow reverse pc boxlineoptions ;
                fi ;
            fi ;
        elseif pospages[1] == RealPageNumber :
            draw posboxes[1] boxlineoptions ;
            path pc ; pc := center pa {up} ... {right} urcorner (posregions[1] enlarged (20pt,20pt)) ;
            pair cc ; cc := (pc intersection_point pa) ;
            if intersection_found :
                pc := pc cutbefore cc ;
                drawarrow pc boxlineoptions ;
            fi ;
        elseif pospages[2] == RealPageNumber :
            draw posboxes[2] boxlineoptions ;
            path pc ; pc := ulcorner (posregions[2] enlarged (20pt,20pt)) {right} ... {down} center pb ;
            pair cc ; cc := (pc intersection_point pb) ;
            if intersection_found :
                pc := pc cutafter cc ;
                drawarrow pc boxlineoptions ;
            fi ;
        fi ;
    fi ;
enddef ;

% anch-bar:

def anch_sidebars_draw (expr p_b_self, p_e_self, y_b_self, y_e_self, h_b_self, d_e_self,
        x, y, w, h, alternative, distance, linewidth, linecolor, topoffset, bottomoffset) =
    % beware, we anchor at (x,y)
    begingroup ;
    if alternative = 1 :
        interim linecap := rounded ;
    else :
        interim linecap := butt ;
    fi ;
    save a, b ; pair a, b ;
    if p_b_self = p_e_self :
        a := (-distance,y_b_self+h_b_self-y) ;
        b := (-distance,y_e_self-d_e_self-y) ;
    elseif RealPageNumber = p_b_self :
        a := (-distance,y_b_self+h_b_self-y) ;
        b := (-distance,0) ;
    elseif RealPageNumber = p_e_self :
        a := (-distance,h) ;
        b := (-distance,y_e_self-d_e_self-y) ;
    else :
        a := (-distance,h) ;
        b := (-distance,0) ;
    fi ;
    a := (xpart a, min(ypart a + topoffset,   h)) ;
    b := (xpart b, max(ypart b - bottomoffset,0)) ;
    draw
        a -- b
        if alternative = 1 :
            dashed (withdots scaled (linewidth/2))
        fi
        withpen pencircle scaled linewidth
        withcolor linecolor ;
    endgroup ;
enddef ;
