/*
    EHWIF.C

Started by DavidW, 5/12/91

Edit box extension to h layer of hwif

*/

#include <edit.g>
#include <edwin.g>
#include <scrimg.g>
#include <hwif.g>
#include <hwif.h>

GLDEF_D UWORD _ebControlFormat=FALSE;

#define EebH ((PR_EDWIN *)ebH)


#pragma save,ENTER_CALL

LOCAL_C INT DoEntersend(VOID *ebH,INT method,VOID *p1,VOID *p2,VOID *p3)
    {
    p_send5(ebH,method,p1,p2,p3);
    return(0);
    }
    
#pragma restore

LOCAL_C INT Entersend(VOID *ebH,INT method,VOID *p1,VOID *p2,VOID *p3)
    {
    INT ret;

    ret=p_enter6(DoEntersend,ebH,method,p1,p2,p3);
    if (ret<-1)
        uErrorValue(ret);
    return(ret);
    }

#define V (VOID*)

GLDEF_C INT hEBCompleteFormat(VOID *ebH)
    {
    PR_SCRIMG *scrimg;
    INT ret;

    scrimg=EebH->edwin.scrimg;
    if (!scrimg->scrimg.formatting)
        return(0);
    ret=Entersend(scrimg,O_SI_SET,0,0,0);
    p_send2(scrimg->scrimg.wrap,O_AO_CANCEL);
    return(ret);
    }

GLDEF_C INT hEBHandleKey(VOID *ebH,INT keycode,INT modifier)
    {
    INT ret;

    if (keycode==W_KEY_RETURN)
        modifier|=W_SHIFT_MODIFIER;
    ret=Entersend(ebH,O_WN_KEY,V keycode,V modifier,NULL);
    if (!ret && !_ebControlFormat)
        ret=hEBCompleteFormat(ebH);
    return(ret);
    }
    
GLDEF_C VOID hEBClose(VOID *ebH)
    {
    p_send2(ebH,O_DESTROY);
    }

GLDEF_C TEXT *hEBSenseText(VOID *ebH)
    {
    SE_EDWIN sense;

    p_send3(ebH,O_WN_SENSE,&sense);
    return(sense.buf);
    }

GLDEF_C INT hEBSetText(VOID *ebH,TEXT *pb,INT blen)
    {
    SE_EDWIN set;

    set.buf=pb;
    set.len=blen;
    return(Entersend(ebH,O_WN_SET,&set,NULL,NULL));
    }

GLDEF_C VOID hEBEmphasise(VOID *ebH,INT flag)
    {
    p_send3(ebH,O_WN_EMPHASISE,flag);
    }

GLDEF_C INT hEBSetSelect(VOID *ebH,INT aoff,INT coff)
    {
    SET_EDWIN set;

    set.flags=SET_EDWIN_CURSOR|SET_EDWIN_ANCHOR;
    set.anchor=aoff;
    set.cursor=coff;
    return(Entersend(ebH,O_EW_SET,&set,NULL,NULL));
    }

GLDEF_C INT hEBSenseSelect(VOID *ebH,UWORD *top)
    {
    return(p_send3(EebH->edwin.scrimg,O_SI_GET_SELECT,top));
    }

GLDEF_C INT hEBSetClipText(VOID *ebH,TEXT *pb,INT blen)
    {
    return(Entersend(EebH->edwin.clip,O_EP_SET_TEXT,pb,V blen,0));
    }

GLDEF_C TEXT *hEBSenseClipText(VOID *ebH)
    {
    TEXT *pb;

    p_send3(EebH->edwin.clip,O_EF_SENSE_BUF,&pb);
    return(pb);
    }

GLDEF_C INT hEBEvaluate(VOID *ebH)
    {
    return(Entersend(ebH,O_EW_EVALUATE,NULL,NULL,NULL));
    }

GLDEF_C INT hEBChangeWidth(VOID *ebH,INT width)
    {
    P_EXTENT ext;

    p_send3(ebH,O_EW_SENSE_SIZE,&ext);
    EebH->edwin.margins.right+=width-ext.width;
    ext.width=width;
    return(Entersend(ebH,O_EW_SET_SIZE,&ext,NULL,NULL));
    }

GLDEF_C INT hEBSetMargin(VOID *ebH,UINT right)
    {
    INT ret;
    UINT oright;

    oright=EebH->edwin.margins.right;
    EebH->edwin.margins.right=right;
    ret=Entersend(EebH->edwin.scrimg,
                        O_SI_STYLE_CHANGED,SCRIMG_STCHNG_DOC,NULL,NULL);
    if (ret<0)
        EebH->edwin.margins.right=oright;
    return(ret);
    }

GLDEF_C UINT hEBSenseMargin(VOID *ebH)
    {
    return(EebH->edwin.margins.right);
    }

GLDEF_C INT hEBInsert(VOID *ebH,TEXT *pb,INT blen)
    {
    return(Entersend(ebH,O_EW_INSERT,pb,V blen,NULL));
    }

GLDEF_C INT hEBReplace(VOID *ebH,TEXT *replace,INT backwards)
    {
    return(Entersend(ebH,O_EW_REPLACE,replace,V backwards,NULL));
    }

GLDEF_C INT hEBCopy(VOID *ebH)
    {
    if (!(EebH->edwin.select))
        return(1);
    return(Entersend(ebH,O_EW_REPLACE_CLIP,NULL,NULL,NULL));
    }

GLDEF_C INT hEBPaste(VOID *ebH)
    {
    if (!(p_send2(EebH->edwin.clip,O_EP_SENSE_LEN)))
        return(1);
    return(Entersend(ebH,O_EW_PASTE_CLIP,NULL,NULL,NULL));
    }

GLDEF_C INT hEBFind(VOID *ebH,TEXT *str,INT flags)
    {
    return(p_send4(ebH,O_EW_FIND,str,flags));
    }

GLDEF_C INT hEBSenseChanged(VOID *ebH)
    {
    return(EebH->edwin.change);
    }

GLDEF_C VOID hEBClearChanged(VOID *ebH)
    {
    EebH->edwin.change=FALSE;
    }

GLDEF_C VOID hEBSetCWidth(VOID *ebH,INT cwidth)
    {
    SCRIMG_WIN win;

    p_send3(EebH->edwin.scrimg,O_SI_SENSE,&win);
    win.cwidth=cwidth;
    Entersend(EebH->edwin.scrimg,O_SI_SET,&win,NULL,NULL);
    }


GLDEF_C INT hEBShowSymbols(VOID *ebH,INT flag)
    {
    SCRLAY_STYLE style;

    p_send3(EebH->edwin.scrlay,O_SL_SENSE,&style);
    style.options=FALSE;
    if (flag)
        style.options=SCRLAY_SHOW_SPACES|SCRLAY_SHOW_LFS;
    p_send3(EebH->edwin.scrlay,O_SL_SET,&style);
    return(Entersend(EebH->edwin.scrimg,O_SI_STYLE_CHANGED,0,0,0));
    }

GLDEF_C VOID *hEBOpen(INT flags,H_EDIT_BOX *heb)
    {
    VOID *ebH;
    IN_EDWIN in;
    IN_EDWIN_X inx;
    PR_WIN win;

    ebH=uCheckHandle(p_new(CAT_HWIF_HWIM,C_EDWIN));
    if (!ebH)
        return(NULL);
    win.win.id=heb->win;
    in.vulen=heb->vulen;
    in.flags=flags|IN_EDWIN_VULEN_PIXELS
                        |IN_EDWIN_NO_AUTOSELECT|IN_EDWIN_POSITION_SUPPLIED;
    in.maxlen=heb->maxchars;
    in.contents[0]=0;
    inx.vislines=heb->vislines;
    inx.pos=heb->pos;
    inx.font=heb->font;
    inx.style=heb->style;
    inx.leading.total=heb->lead_tot;
    inx.leading.top=heb->lead_top;
    inx.clip=NULL;
    if (Entersend(ebH,O_WN_INIT,&in,&win,&inx))
        {
        hEBClose(ebH);
        return(NULL);
        }
    return(ebH);
    }

GLDEF_C VOID *hEBSenseDoc(VOID *ebH)
    {
    return(EebH->edwin.doc);
    }

GLDEF_C INT hEDCapacity(VOID *doc,UINT maxlen)
    {
    return(Entersend(doc,O_EP_CAPACITY,V (maxlen+1),NULL,NULL));
    }

GLDEF_C INT hEDInsert(VOID *doc,UINT pos,VOID *buf,UINT len)
    {
    return(Entersend(doc,O_EP_INSERT,V pos,buf,V len));
    }

GLDEF_C INT hEBDocChanged(VOID *ebH)
    {
    UINT doclen;

    doclen=p_send2(EebH->edwin.doc,O_EP_SENSE_LEN);
    EebH->edwin.clen=doclen+1;
    return(Entersend(EebH->edwin.scrimg,O_SI_DOC_CHANGED,V doclen,NULL,NULL));
    }

GLDEF_C INT hEBPosToXL(void *ebH,H_SCRLAY_PLX *plx)
    {
    return(p_send3(EebH->edwin.scrlay,O_SL_POS_TO_XL,plx));
    }
