/* This Genie puts frames around boxes and sets the margins accordingly.
Written by Don Cox
 */

signal on error
signal on syntax
address command
call SafeEndEdit.rexx()
call ppm_AutoUpdate(0)
cr="0a"x

counter=0

collist = ppm_GetColorList()
collist = substr(collist, pos('0a'x, collist) +1) /* strip off initial line which is number of colours */

do forever
    box=ppm_ClickOnBox("Click on boxes to be framed")
    if box=0 then break
    counter=counter+1
    boxes.counter=box
    call ppm_SelectBox(box)
end

if counter=0 then exit_msg("No boxes selected")

form = "Thickness in Points"cr"Inside/Outside (I/O)"
manylines=ppm_GetForm("Frame settings",4,form)
if manylines="" then exit_msg("Operation Cancelled")

parse var manylines weight "0a"x posn
posn= upper(posn)
color=ppm_SelectFromList("Select Color",24,18,0,collist)
if color = "" then exit_msg("Aborted by User")

currentunits=ppm_GetUnits()
call ppm_SetUnits(1)


iw=weight/72   /* line weight in inches */
iw2=iw*2

do i=1 to counter
    box=boxes.i
    call ppm_SetBoxFrame(box,1)
    framedata = ppm_GetBoxFrameData(box)
    parse var framedata lc "0a"x fillcolor "0a"x lineweight "0a"x linepattern "0a"x fillpattern  

    call ppm_SetBoxFrameData(box,color,fillcolor,weight,linepattern,fillpattern)
    margins = ppm_GetBoxMargins(box)
    parse var margins mleft mtop mright mbottom

    ilineweight=lineweight/72
    boxtype = upper(word(ppm_GetBoxInfo(box), 1))
    if boxtype = "TEXT" then  /* extra margin for text */
        do
        mleft2=abs(mleft-ilineweight+iw)
        if mleft2=iw then mleft2=iw2
        if mleft2<iw then mleft2=iw2
        
        mtop2=abs(mtop-ilineweight+iw)
        if mtop2=iw then mtop2=iw2
        if mtop2<iw then mtop2=iw2
        
        mright2=abs(mright-ilineweight+iw)
        if mright2=iw then mright2=iw2
        if mright2<iw then mright2=iw2
       
        mbottom2=abs(mbottom-ilineweight+iw)
        if mbottom2=iw then mbottom2=iw2
        if mbottom2<iw then mbottom2=iw2
        end
    else
        do
        mleft2 = abs(mleft-ilineweight+iw)
        if mleft2<iw then mleft2=iw
        mtop2=abs(mtop-ilineweight+iw)
        if mtop2<iw then mtop2=iw
        mright2=abs(mright-ilineweight+iw)
        if mright2<iw then mright2=iw
        mbottom2=abs(mbottom-ilineweight+iw)
        if mbottom2<iw then mbottom2=iw
        end
    call ppm_SetBoxMargins(box,mleft2,mtop2,mright2,mbottom2)


   if posn="O" /* Letter O */
        then do
        where = ppm_GetBoxPosition(box)
        xwhere=word(where,1)
        ywhere=word(where,2)
        call ppm_SetBoxPosition(box,xwhere-iw,ywhere-iw)
        howbig = ppm_GetBoxSize(box)
        boxwidth = word(howbig,1)+iw2
        boxheight = word(howbig,2)+iw2
        call ppm_SetBoxSize(box,boxwidth,boxheight)
        
        end
    end
call ppm_SetUnits(currentunits)

call exit_msg()

end

error:
syntax:
    do
    exit_msg("Genie failed due to error: "errortext(rc))
    end

exit_msg:
    do
    parse arg message
    if message ~= "" then
    call ppm_Inform(1,message)
    call ppm_ClearStatus()
    call ppm_AutoUpdate(1)
    exit
    end

